3939 description : " Target environment"
4040 required : true
4141 type : choice
42- options : [staging, prod]
42+ options : [dev, staging, prod]
4343 default : staging
4444
4545permissions :
@@ -62,6 +62,136 @@ concurrency:
6262 # 1a. Build + push staging images
6363 # ---------------------------------------------------------------------------
6464jobs :
65+ build-dev :
66+ name : " Build & Push [dev] (${{ matrix.service }})"
67+ runs-on : ubuntu-latest
68+ timeout-minutes : 20
69+ if : >
70+ github.event_name == 'workflow_dispatch'
71+ && inputs.environment == 'dev'
72+ environment : dev
73+ strategy :
74+ matrix :
75+ service : [backend, frontend]
76+ fail-fast : true
77+ permissions :
78+ contents : read
79+ id-token : write
80+ outputs :
81+ registry : ${{ steps.ecr-login.outputs.registry }}
82+ steps :
83+ - uses : actions/checkout@v4
84+ with :
85+ ref : ${{ github.sha }}
86+
87+ - name : Authenticate with AWS
88+ uses : ./.github/actions/aws-auth
89+ with :
90+ role-arn : ${{ secrets.AWS_ROLE_TO_ASSUME }}
91+ aws-region : ${{ secrets.AWS_REGION }}
92+
93+ - name : Log in to Amazon ECR
94+ id : ecr-login
95+ uses : aws-actions/amazon-ecr-login@v2
96+
97+ - name : Build, tag & push
98+ uses : ./.github/actions/docker-build
99+ with :
100+ context : ./${{ matrix.service }}
101+ dockerfile : ./${{ matrix.service }}/Dockerfile
102+ image-name : mypythonproject1/${{ matrix.service }}
103+ registry : ${{ steps.ecr-login.outputs.registry }}
104+ tags : |
105+ type=raw,value=dev
106+ type=sha,prefix=dev-,format=short
107+ build-args : |
108+ BUILD_ENV=dev
109+ GIT_SHA=${{ github.sha }}
110+ platforms : linux/amd64
111+ scan : " true"
112+ scan-severity : " CRITICAL,HIGH"
113+ scan-exit-code : " 0"
114+ cache-scope : ${{ matrix.service }}-dev
115+
116+ infra-dev :
117+ name : " Infra Apply [dev]"
118+ runs-on : ubuntu-latest
119+ timeout-minutes : 5
120+ needs : [build-dev]
121+ environment : dev
122+ steps :
123+ - name : Dispatch terraform apply to infra repo
124+ uses : actions/github-script@v7
125+ with :
126+ github-token : ${{ secrets.INFRA_DEPLOY_TOKEN }}
127+ script : |
128+ await github.rest.actions.createWorkflowDispatch({
129+ owner: context.repo.owner,
130+ repo: 'mypythonproject1-infra',
131+ workflow_id: 'terraform-plan-apply.yml',
132+ ref: 'main',
133+ inputs: {
134+ environment: 'dev',
135+ action: 'apply',
136+ },
137+ });
138+ console.log('Dispatched terraform-plan-apply on mypythonproject1-infra (dev)');
139+
140+ - name : Wait for infra apply
141+ run : |
142+ echo "Waiting 10 minutes for Terraform apply to complete..."
143+ sleep 600
144+
145+ deploy-dev :
146+ name : " ECS Deploy [dev] (${{ matrix.service }})"
147+ runs-on : ubuntu-latest
148+ timeout-minutes : 20
149+ needs : [infra-dev]
150+ environment : dev
151+ strategy :
152+ matrix :
153+ service : [backend, frontend]
154+ fail-fast : false
155+ permissions :
156+ id-token : write
157+ steps :
158+ - name : Authenticate with AWS
159+ uses : aws-actions/configure-aws-credentials@v4
160+ with :
161+ role-to-assume : ${{ secrets.AWS_ROLE_TO_ASSUME }}
162+ aws-region : ${{ secrets.AWS_REGION }}
163+
164+ - name : Force new ECS deployment
165+ run : |
166+ CLUSTER="mypythonproject1-dev-cluster"
167+ SERVICE="mypythonproject1-dev-${{ matrix.service }}-service"
168+ aws ecs update-service \
169+ --cluster "$CLUSTER" \
170+ --service "$SERVICE" \
171+ --force-new-deployment \
172+ --region "${{ secrets.AWS_REGION }}"
173+
174+ - name : Wait for service stability
175+ timeout-minutes : 15
176+ run : |
177+ CLUSTER="mypythonproject1-dev-cluster"
178+ SERVICE="mypythonproject1-dev-${{ matrix.service }}-service"
179+ aws ecs wait services-stable \
180+ --cluster "$CLUSTER" \
181+ --services "$SERVICE" \
182+ --region "${{ secrets.AWS_REGION }}"
183+
184+ smoke-test-dev :
185+ name : " Smoke Test [dev]"
186+ needs : [deploy-dev]
187+ uses : ./.github/workflows/_smoke-test.yml
188+ with :
189+ environment : dev
190+ app-url : ${{ vars.APP_URL }}
191+ warmup-seconds : 15
192+ deploy-sha : ${{ github.sha }}
193+ secrets : inherit
194+
65195 build-staging :
66196 name : " Build & Push [staging] (${{ matrix.service }})"
67197 runs-on : ubuntu-latest
80210 id-token : write
81211 outputs :
82212 registry : ${{ steps.ecr-login.outputs.registry }}
83- image-backend : ${{ steps.meta.outputs.tags }}
84213 steps :
85214 - uses : actions/checkout@v4
86215 with :
@@ -125,22 +254,22 @@ jobs:
125254 needs : [build-staging]
126255 environment : staging
127256 steps :
128- - name : Dispatch terraform- apply to infra repo
257+ - name : Dispatch terraform apply to infra repo
129258 uses : actions/github-script@v7
130259 with :
131260 github-token : ${{ secrets.INFRA_DEPLOY_TOKEN }}
132261 script : |
133262 await github.rest.actions.createWorkflowDispatch({
134263 owner: context.repo.owner,
135264 repo: 'mypythonproject1-infra',
136- workflow_id: 'terraform-apply.yml',
265+ workflow_id: 'terraform-plan- apply.yml',
137266 ref: 'main',
138267 inputs: {
139268 environment: 'staging',
140- image_tag : 'staging ',
269+ action : 'apply ',
141270 },
142271 });
143- console.log('Dispatched terraform-apply on mypythonproject1-infra (staging)');
272+ console.log('Dispatched terraform-plan- apply on mypythonproject1-infra (staging)');
144273
145274 # Give Terraform apply ~10 min to complete before ECS deploy
146275 - name : Wait for infra apply
@@ -316,23 +445,22 @@ jobs:
316445 needs : [build-production]
317446 environment : prod
318447 steps :
319- - name : Dispatch terraform- apply to infra repo
448+ - name : Dispatch terraform apply to infra repo
320449 uses : actions/github-script@v7
321450 with :
322451 github-token : ${{ secrets.INFRA_DEPLOY_TOKEN }}
323452 script : |
324- const tag = context.ref.replace('refs/tags/', '');
325453 await github.rest.actions.createWorkflowDispatch({
326454 owner: context.repo.owner,
327455 repo: 'mypythonproject1-infra',
328- workflow_id: 'terraform-apply.yml',
456+ workflow_id: 'terraform-plan- apply.yml',
329457 ref: 'main',
330458 inputs: {
331- environment: 'production ',
332- image_tag: tag ,
459+ environment: 'prod ',
460+ action: 'apply' ,
333461 },
334462 });
335- console.log(` Dispatched terraform-apply on mypythonproject1-infra (production, tag=${tag})` );
463+ console.log(' Dispatched terraform-plan- apply on mypythonproject1-infra (prod)' );
336464
337465 - name : Wait for infra apply
338466 run : |
0 commit comments