@@ -4,7 +4,18 @@ The backend is deployed to AWS ECS (Elastic Container Service) with separate sta
44
55## Building and Pushing Docker Images
66
7- Use the ` docker-build.sh ` script to build multi-architecture images and push to AWS ECR:
7+ ### Automated Builds (Recommended)
8+
9+ Docker images are automatically built and pushed to AWS ECR via GitHub Actions:
10+
11+ - ** PR branches** (any branch except ` master ` ): Automatically builds and pushes to ` :staging ` tag
12+ - ** Master branch** : Automatically builds and pushes to ` :prod ` tag after CI checks pass
13+
14+ The automated builds use AWS OIDC for secure authentication (no long-lived credentials).
15+
16+ ### Manual Builds (Legacy)
17+
18+ For manual builds, use the ` docker-build.sh ` script:
819
920``` bash
1021# Build and push staging images
@@ -14,9 +25,7 @@ Use the `docker-build.sh` script to build multi-architecture images and push to
1425./docker-build.sh prod
1526```
1627
17- This creates:
18- - ` back-end:staging-amd64 ` and ` back-end:staging-arm64 ` images
19- - A multi-arch manifest at ` back-end:staging `
28+ This creates ARM64 images tagged as ` back-end:staging ` or ` back-end:prod ` .
2029
2130## Deploying to ECS
2231
@@ -74,3 +83,140 @@ aws logs tail /ecs/back-end-staging --follow
7483aws logs tail /ecs/back-end-production --follow
7584```
7685
86+ # GitHub Actions AWS OIDC Setup
87+
88+ The CI/CD pipeline uses AWS OIDC (OpenID Connect) for secure authentication to AWS without storing long-lived credentials. This follows AWS security best practices.
89+
90+ ## Prerequisites
91+
92+ - AWS account with ECR repository: ` 633607774026.dkr.ecr.us-east-2.amazonaws.com/back-end `
93+ - GitHub repository: ` operationcode/back-end `
94+ - AWS IAM permissions to create IAM roles and policies
95+
96+ ## Setup Instructions
97+
98+ ### 1. Create IAM OIDC Identity Provider
99+
100+ If not already configured, create an OIDC identity provider for GitHub:
101+
102+ ``` bash
103+ aws iam create-open-id-connect-provider \
104+ --url https://token.actions.githubusercontent.com \
105+ --client-id-list sts.amazonaws.com \
106+ --thumbprint-list 6938fd4d98bab03faadb97b34396831e3780aea1
107+ ```
108+
109+ ### 2. Create IAM Role for GitHub Actions
110+
111+ Create an IAM role that GitHub Actions can assume:
112+
113+ ``` bash
114+ # Create trust policy file
115+ cat > github-actions-trust-policy.json << EOF
116+ {
117+ "Version": "2012-10-17",
118+ "Statement": [
119+ {
120+ "Effect": "Allow",
121+ "Principal": {
122+ "Federated": "arn:aws:iam::633607774026:oidc-provider/token.actions.githubusercontent.com"
123+ },
124+ "Action": "sts:AssumeRoleWithWebIdentity",
125+ "Condition": {
126+ "StringEquals": {
127+ "token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
128+ },
129+ "StringLike": {
130+ "token.actions.githubusercontent.com:sub": "repo:operationcode/back-end:*"
131+ }
132+ }
133+ }
134+ ]
135+ }
136+ EOF
137+
138+ # Create the role
139+ aws iam create-role \
140+ --role-name GitHubActions-ECR-Push \
141+ --assume-role-policy-document file://github-actions-trust-policy.json \
142+ --description " Allows GitHub Actions to push Docker images to ECR"
143+ ```
144+
145+ ### 3. Attach ECR Permissions Policy
146+
147+ Create and attach a policy that allows pushing to ECR:
148+
149+ ``` bash
150+ # Create policy file
151+ cat > ecr-push-policy.json << EOF
152+ {
153+ "Version": "2012-10-17",
154+ "Statement": [
155+ {
156+ "Effect": "Allow",
157+ "Action": [
158+ "ecr:GetAuthorizationToken",
159+ "ecr:BatchCheckLayerAvailability",
160+ "ecr:GetDownloadUrlForLayer",
161+ "ecr:BatchGetImage",
162+ "ecr:PutImage",
163+ "ecr:InitiateLayerUpload",
164+ "ecr:UploadLayerPart",
165+ "ecr:CompleteLayerUpload"
166+ ],
167+ "Resource": "arn:aws:ecr:us-east-2:633607774026:repository/back-end"
168+ },
169+ {
170+ "Effect": "Allow",
171+ "Action": "ecr:GetAuthorizationToken",
172+ "Resource": "*"
173+ }
174+ ]
175+ }
176+ EOF
177+
178+ # Create the policy
179+ aws iam create-policy \
180+ --policy-name GitHubActions-ECR-Push-Policy \
181+ --policy-document file://ecr-push-policy.json
182+
183+ # Attach policy to role
184+ aws iam attach-role-policy \
185+ --role-name GitHubActions-ECR-Push \
186+ --policy-arn arn:aws:iam::633607774026:policy/GitHubActions-ECR-Push-Policy
187+ ```
188+
189+ ### 4. Configure GitHub Secret
190+
191+ Add the IAM role ARN as a GitHub repository secret using the GitHub CLI:
192+
193+ ``` bash
194+ # Ensure you're authenticated with GitHub CLI
195+ # If not already authenticated, run: gh auth login
196+
197+ # Set the secret (replace with your actual role ARN if different)
198+ gh secret set AWS_ROLE_ARN --body " arn:aws:iam::633607774026:role/GitHubActions-ECR-Push"
199+ ```
200+
201+ ** Note** : Make sure you're in the repository directory or specify the repo with ` --repo operationcode/back-end ` .
202+
203+ ### 5. Verify Setup
204+
205+ After setup, the GitHub Actions workflow will automatically:
206+ - Authenticate to AWS using OIDC
207+ - Build Docker images for ARM64 platform
208+ - Push images to ECR with appropriate tags (` :staging ` or ` :prod ` )
209+
210+ You can verify by:
211+ 1 . Pushing a commit to a non-master branch (should push ` :staging ` )
212+ 2 . Merging to master (should push ` :prod ` after tests pass)
213+ 3 . Checking ECR repository for new images
214+
215+ ## Security Best Practices
216+
217+ ✅ ** OIDC Authentication** : No long-lived AWS credentials stored in GitHub
218+ ✅ ** Least Privilege** : IAM role only has permissions needed for ECR push operations
219+ ✅ ** Repository Scoping** : Trust policy restricts access to ` operationcode/back-end ` repository
220+ ✅ ** Conditional Access** : Production builds only run after CI checks pass
221+ ✅ ** Build Caching** : Uses GitHub Actions cache to speed up builds
222+
0 commit comments