88env :
99 AWS_REGION : ${{ secrets.AWS_REGION }}
1010 AWS_IAM_ROLE_ARN : ${{ secrets.AWS_IAM_ROLE_ARN }}
11- TERRAFORM_DIR : ./01-tf
11+ TERRAFORM_DIR : ${{ vars.TERRAFORM_DIR }}
1212 TERRAFORM_VERSION : ' 1.12.0'
1313
1414jobs :
1717 runs-on : ubuntu-latest
1818 permissions :
1919 contents : read
20- id-token : write
20+ id-token : write # Required for OIDC with AWS
2121
2222 steps :
2323 - name : Checkout code
@@ -39,27 +39,44 @@ jobs:
3939 run : terraform init -input=false
4040 working-directory : ${{ env.TERRAFORM_DIR }}
4141
42- - name : Terraform Apply
43- id : apply
42+ # First Apply: Create ECR and other infrastructure
43+ - name : Terraform Apply (ECR and initial setup)
44+ id : apply_infra
4445 run : terraform apply -auto-approve -input=false
4546 working-directory : ${{ env.TERRAFORM_DIR }}
4647
47- - name : Get ECR Repository URL
48- id : get_ecr_url
49- run : echo "ECR_REPOSITORY_URL=$(terraform output -raw ecr_repository_url)" >> $GITHUB_ENV
48+ - name : Get ECR Details from Terraform Output
49+ id : get_ecr_details
50+ run : |
51+ ECR_FULL_REPO_URL=$(terraform output -raw ecr_repository_url)
52+ ECR_REGISTRY=$(echo "$ECR_FULL_REPO_URL" | cut -d'/' -f1)
53+ echo "ECR_REGISTRY=$ECR_REGISTRY" >> $GITHUB_ENV
5054 working-directory : ${{ env.TERRAFORM_DIR }}
5155
5256 - name : Login to Amazon ECR
5357 id : login-ecr
5458 uses : docker/login-action@v3
5559 with :
56- registry : ${{ env.ECR_REPOSITORY_URL }}
60+ registry : ${{ env.ECR_REGISTRY }} # Use the registry extracted
5761
5862 - name : Build, tag, and push image to Amazon ECR
5963 id : build-image
64+ env :
65+ ECR_REPOSITORY_URL : ${{ env.ECR_REPOSITORY_URL }}
66+ IMAGE_TAG : ${{ github.sha }}
6067 uses : docker/build-push-action@v5
6168 with :
6269 context : .
6370 file : ./Dockerfile
6471 push : true
65- tags : ${{ env.ECR_REPOSITORY_URL }}:latest
72+ tags : | # Allow multiple tags
73+ ${{ env.ECR_REPOSITORY_URL }}:${{ env.IMAGE_TAG }}
74+ ${{ env.ECR_REPOSITORY_URL }}:latest
75+
76+ # Second Apply: Update ECS with new image
77+ - name : Terraform Apply (Update ECS with new image)
78+ id : apply_ecs_update
79+ env :
80+ TF_VAR_app_image_uri : ' ${{ env.ECR_REPOSITORY_URL }}:${{ github.sha }}'
81+ run : terraform apply -auto-approve -input=false
82+ working-directory : ${{ env.TERRAFORM_DIR }}
0 commit comments