Skip to content

Commit c63da56

Browse files
committed
initial tf and github workflow
1 parent 351fe31 commit c63da56

File tree

11 files changed

+227
-0
lines changed

11 files changed

+227
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: Build & Push RimWorld Hay Calc
2+
on:
3+
# Build on every merge to main *and* on version tags like v1.2.3
4+
push:
5+
branches: [ main ]
6+
tags: [ 'v*' ]
7+
paths:
8+
- 'frontend/**'
9+
- 'Dockerfile'
10+
- '.github/workflows/build-and-deploy.yml'
11+
12+
env:
13+
AWS_REGION: us-east-1
14+
ECR_REPOSITORY: rimworld-hay-calc
15+
# This can be set in repo → Settings → Environments → prod → vars
16+
ECR_REGISTRY: ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com
17+
18+
jobs:
19+
build:
20+
runs-on: ubuntu-latest
21+
permissions:
22+
id-token: write # ✅ OIDC
23+
contents: read
24+
25+
steps:
26+
- name: 🛎️ Check out code
27+
uses: actions/checkout@v4
28+
29+
# ---------- ① Configure AWS creds via OIDC ----------
30+
- name: 🔐 Configure AWS credentials
31+
uses: aws-actions/configure-aws-credentials@v4
32+
with:
33+
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/GitHubOIDCDeploy
34+
aws-region: ${{ env.AWS_REGION }}
35+
36+
# ---------- ② Log in to ECR ----------
37+
- name: 🔑 Login to Amazon ECR
38+
uses: aws-actions/amazon-ecr-login@v2
39+
40+
# ---------- ③ Set image tag ----------
41+
- name: 🏷️ Define image tag
42+
id: meta
43+
run: |
44+
if [[ "${GITHUB_REF_TYPE}" == "tag" ]]; then
45+
echo "IMAGE_TAG=${GITHUB_REF_NAME}" >> $GITHUB_OUTPUT
46+
else
47+
echo "IMAGE_TAG=sha-${GITHUB_SHA::7}" >> $GITHUB_OUTPUT
48+
fi
49+
- name: 💬 Show tag
50+
run: echo "Pushing tag ${{ steps.meta.outputs.IMAGE_TAG }}"
51+
52+
# ---------- ④ Build & push (uses Docker layer cache) ----------
53+
- name: 🐳 Build & push image
54+
uses: docker/build-push-action@v5
55+
with:
56+
context: .
57+
file: ./Dockerfile # still at repo root
58+
push: true
59+
tags: |
60+
${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ steps.meta.outputs.IMAGE_TAG }}
61+
${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:latest
62+
cache-from: type=registry,ref=${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:buildcache
63+
cache-to: type=registry,ref=${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:buildcache,mode=max

.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?
25+
26+
.terraform/*
27+
.terraform.tfstate*

Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# ---------- build stage ----------
2+
FROM node:20-slim AS builder
3+
WORKDIR /app
4+
COPY frontend/package*.json ./frontend/
5+
WORKDIR /app/frontend
6+
RUN npm ci
7+
COPY frontend/ .
8+
RUN npm run build # outputs to /app/dist
9+
10+
# ---------- runtime stage ----------
11+
FROM nginx:1.27-alpine
12+
COPY --from=builder /app/frontend/dist /usr/share/nginx/html
13+
# Optional: replace default.conf to add /healthz
14+
HEALTHCHECK CMD wget -qO- http://localhost/ || exit 1
15+
EXPOSE 80

infra/envs/dev/backend.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
terraform {
2+
backend "s3" {
3+
bucket = "rimworld-hay-calc-tfstate"
4+
key = "dev/terraform.tfstate"
5+
region = "us-east-1"
6+
encrypt = true
7+
}
8+
}

infra/envs/dev/main.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module "rimworld_hay_calc" {
2+
source = "../.." # <- two levels up to the shared root module
3+
4+
# Pass through any vars that differ per-env
5+
aws_region = var.aws_region
6+
github_repo = var.github_repo
7+
project_name = var.project_name
8+
}
9+
10+
# Re-expose useful outputs
11+
output "ecr_repo_url" { value = module.rimworld_hay_calc.ecr_repo_url }
12+
output "github_oidc_role" { value = module.rimworld_hay_calc.github_oidc_role }

infra/envs/dev/terraform.tfvars

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
github_repo = "vonschtirlitz/rimworld-hay-calc"
2+
aws_region = "us-east-1"
3+
project_name = "rimworld-hay-calc"

infra/envs/dev/variables.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
variable "aws_region" { type = string }
2+
variable "github_repo" { type = string }
3+
variable "project_name" { type = string }

infra/main.tf

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
########################
2+
# ECR REPOSITORY #
3+
########################
4+
resource "aws_ecr_repository" "app" {
5+
name = "${var.project_name}"
6+
image_tag_mutability = "MUTABLE"
7+
force_delete = false
8+
9+
lifecycle {
10+
prevent_destroy = true
11+
}
12+
}
13+
14+
########################
15+
# GITHUB OIDC ROLE #
16+
########################
17+
data "aws_caller_identity" "current" {}
18+
data "aws_iam_policy_document" "github_trust" {
19+
statement {
20+
effect = "Allow"
21+
principals {
22+
type = "Federated"
23+
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:oidc-provider/token.actions.githubusercontent.com"]
24+
}
25+
actions = ["sts:AssumeRoleWithWebIdentity"]
26+
27+
condition {
28+
test = "StringEquals"
29+
variable = "token.actions.githubusercontent.com:aud"
30+
values = ["sts.amazonaws.com"]
31+
}
32+
33+
condition {
34+
test = "StringLike"
35+
variable = "token.actions.githubusercontent.com:sub"
36+
values = ["repo:${var.github_repo}:*"]
37+
}
38+
}
39+
}
40+
41+
resource "aws_iam_role" "github_deploy" {
42+
name = "${var.project_name}-github-oidc"
43+
assume_role_policy = data.aws_iam_policy_document.github_trust.json
44+
}
45+
46+
resource "aws_iam_role_policy_attachment" "ecr_power" {
47+
role = aws_iam_role.github_deploy.name
48+
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryPowerUser"
49+
}

infra/outputs.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "ecr_repo_url" {
2+
value = aws_ecr_repository.app.repository_url
3+
}
4+
5+
output "github_oidc_role" {
6+
value = aws_iam_role.github_deploy.arn
7+
}

infra/variables.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
variable "aws_region" {
2+
type = string
3+
description = "AWS region to deploy into"
4+
default = "us-east-1"
5+
}
6+
7+
variable "project_name" {
8+
type = string
9+
description = "Used for namespacing resources"
10+
default = "rimworld-hay-calc"
11+
}
12+
13+
variable "global_tags" {
14+
type = map(string)
15+
default = {
16+
Project = "RimworldHayCalc"
17+
}
18+
}
19+
20+
variable "github_repo" {
21+
description = "owner/repo used in the GitHub OIDC subject"
22+
type = string
23+
}

0 commit comments

Comments
 (0)