1616 - preview
1717 - training
1818 - production
19- image_tag :
20- description : Docker image tag
21- required : false
22- type : string
2319 server_types :
2420 description : Server types to deploy
2521 required : true
3430 environment :
3531 required : true
3632 type : string
37- image_tag :
38- required : false
39- type : string
4033 server_types :
4134 required : true
4235 type : string
4841 aws-role : ${{ inputs.environment == 'production'
4942 && 'arn:aws:iam::820242920762:role/GithubDeployMavisAndInfrastructure'
5043 || 'arn:aws:iam::393416225559:role/GithubDeployMavisAndInfrastructure' }}
51- terraform-working-directory : terraform/app
5244
5345jobs :
54- plan-changes :
55- name : Plan task definition changes
56- runs-on : ubuntu-latest
57- permissions :
58- id-token : write
59- steps :
60- - name : Checkout code
61- uses : actions/checkout@v4
62- - name : Configure AWS Credentials
63- uses : aws-actions/configure-aws-credentials@v4
64- with :
65- role-to-assume : ${{ env.aws-role }}
66- aws-region : eu-west-2
67- - name : Login to ECR
68- id : login-ecr
69- uses : aws-actions/amazon-ecr-login@v2
70- - name : pull docker image
71- run : |
72- DOCKER_IMAGE="${{ steps.login-ecr.outputs.registry }}/mavis/webapp:${{ inputs.image_tag || github.sha }}"
73- docker pull "$DOCKER_IMAGE"
74- echo "DOCKER_IMAGE=$DOCKER_IMAGE" >> $GITHUB_ENV
75- - name : Extract image digest
76- run : |
77- DOCKER_DIGEST=$(docker inspect --format='{{index .RepoDigests 0}}' "$DOCKER_IMAGE")
78- DIGEST="${DOCKER_DIGEST#*@}"
79- echo "DIGEST=$DIGEST" >> $GITHUB_ENV
80- - name : Install terraform
81- uses : hashicorp/setup-terraform@v3
82- with :
83- terraform_version : 1.10.5
84- - name : Update the task definition
85- id : plan
86- working-directory : ${{ env.terraform-working-directory }}
87- run : |
88- terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade
89- terraform plan -var-file="env/${{ inputs.environment }}.tfvars" \
90- -var="image_digest=$DIGEST" -out=${{ runner.temp }}/tfplan | tee ${{ runner.temp }}/tf_stdout
91- - name : Validate the changes
92- run : |
93- ./terraform/scripts/check_task_definition.sh ${{ runner.temp }}/tf_stdout
94- - name : Upload artifact
95- uses : actions/upload-artifact@v4
96- with :
97- name : tfplan_app-${{ inputs.environment }}
98- path : ${{ runner.temp }}/tfplan
99-
100- apply-changes :
101- name : Apply task definition changes
46+ prepare-deployment :
47+ name : Prepare deployment
10248 runs-on : ubuntu-latest
103- needs : plan-changes
104- environment : ${{ inputs.environment }}
10549 permissions :
10650 id-token : write
10751 steps :
@@ -112,26 +56,25 @@ jobs:
11256 with :
11357 role-to-assume : ${{ env.aws-role }}
11458 aws-region : eu-west-2
115- - name : Download artifact
116- uses : actions/download-artifact@v4
117- with :
118- name : tfplan_app-${{ inputs.environment }}
119- path : ${{ runner.temp }}
12059 - name : Install terraform
12160 uses : hashicorp/setup-terraform@v3
12261 with :
12362 terraform_version : 1.10.5
124- - name : Apply the changes
125- working-directory : ${{ env.terraform-working-directory }}
63+ - name : Get terraform output
64+ id : terraform-output
65+ working-directory : terraform/app
12666 run : |
127- terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade
128- terraform apply ${{ runner.temp }}/tfplan
129- echo "s3_bucket=$(terraform output -raw s3_bucket)" >> ${{ runner.temp }}/DEPLOYMENT_ENVS
130- echo "s3_key=$(terraform output -raw s3_key)" >> ${{ runner.temp }}/DEPLOYMENT_ENVS
131- echo "application=$(terraform output -raw codedeploy_application_name)" >> ${{ runner.temp }}/DEPLOYMENT_ENVS
132- echo "application_group=$(terraform output -raw codedeploy_deployment_group_name)" >> ${{ runner.temp }}/DEPLOYMENT_ENVS
133- echo "ecs_variables=$(terraform output -json ecs_variables | sed 's/\"/\\"/g')" >> ${{ runner.temp }}/DEPLOYMENT_ENVS
134- - name : Upload artifact
67+ terraform init -backend-config=env/${{ inputs.environment }}-backend.hcl -reconfigure
68+ terraform output -json | jq -r '
69+ "s3_bucket=" + .s3_bucket.value,
70+ "s3_key=" + .s3_key.value,
71+ "application=" + .codedeploy_application_name.value,
72+ "application_group=" + .codedeploy_deployment_group_name.value,
73+ "cluster_name=" + .ecs_variables.value.cluster_name,
74+ "good_job_service=" + .ecs_variables.value.good_job.service_name,
75+ "good_job_task_definition=" + .ecs_variables.value.good_job.task_definition.arn
76+ ' > ${{ runner.temp }}/DEPLOYMENT_ENVS
77+ - name : Upload Artifact
13578 uses : actions/upload-artifact@v4
13679 with :
13780 name : DEPLOYMENT_ENVS-${{ inputs.environment }}
14083 create-web-deployment :
14184 name : Create web deployment
14285 runs-on : ubuntu-latest
143- needs : apply-changes
86+ needs : prepare-deployment
14487 if : inputs.server_types == 'web' || inputs.server_types == 'all'
14588 environment : ${{ inputs.environment }}
14689 permissions :
@@ -150,41 +93,39 @@ jobs:
15093 uses : actions/download-artifact@v4
15194 with :
15295 name : DEPLOYMENT_ENVS-${{ inputs.environment }}
153- path : ${{ runner.temp }}/artifact
96+ path : ${{ runner.temp }}
15497 - name : Configure AWS Credentials
15598 uses : aws-actions/configure-aws-credentials@v4
15699 with :
157100 role-to-assume : ${{ env.aws-role }}
158101 aws-region : eu-west-2
159- - name : Install AWS CLI
160- run : sudo snap install --classic aws-cli
161102 - name : Trigger CodeDeploy deployment
162103 run : |
163- source ${{ runner.temp }}/artifact/ DEPLOYMENT_ENVS
104+ source ${{ runner.temp }}/DEPLOYMENT_ENVS
164105 deployment_id=$(aws deploy create-deployment \
165106 --application-name "$application" --deployment-group-name "$application_group" \
166107 --s3-location bucket="$s3_bucket",key="$s3_key",bundleType=yaml | jq -r .deploymentId)
167108 echo "Deployment started: $deployment_id"
168109 echo "deployment_id=$deployment_id" >> $GITHUB_ENV
169110 - name : Wait up to 30 minutes for deployment to complete
170111 run : |
171- aws deploy wait deployment-successful --deployment-id $deployment_id
112+ aws deploy wait deployment-successful --deployment-id " $deployment_id"
172113 echo "Deployment successful"
173114
174115 create-good-job-deployment :
175116 name : Create good-job deployment
176117 runs-on : ubuntu-latest
177- needs : apply-changes
178- if : inputs.server_types == 'good-job' || inputs.server_types == 'all'
118+ needs : prepare-deployment
119+ if : inputs.server_types == 'good-job' || inputs.server_types == 'all'
179120 environment : ${{ inputs.environment }}
180121 permissions :
181122 id-token : write
182123 steps :
183- - name : Download artifact
124+ - name : Download Artifact
184125 uses : actions/download-artifact@v4
185126 with :
186127 name : DEPLOYMENT_ENVS-${{ inputs.environment }}
187- path : ${{ runner.temp }}/artifact
128+ path : ${{ runner.temp }}
188129 - name : Configure AWS Credentials
189130 uses : aws-actions/configure-aws-credentials@v4
190131 with :
@@ -194,26 +135,20 @@ jobs:
194135 run : sudo snap install --classic aws-cli
195136 - name : Trigger ECS Deployment
196137 run : |
197- source ${{ runner.temp }}/artifact/DEPLOYMENT_ENVS
198- echo "$ecs_variables"
199- cluster_name=$(echo "$ecs_variables" | jq -r '.cluster_name')
200- service_name=$(echo "$ecs_variables" | jq -r '.good_job.service_name')
201- task_definition=$(echo "$ecs_variables" | jq -r '.good_job.task_definition.arn')
202- DEPLOYMENT_ID=$(aws ecs update-service --cluster $cluster_name --service $service_name \
203- --task-definition $task_definition --force-new-deployment \
138+ source ${{ runner.temp }}/DEPLOYMENT_ENVS
139+ DEPLOYMENT_ID=$(aws ecs update-service --cluster $cluster_name --service $good_job_service \
140+ --task-definition $good_job_task_definition --force-new-deployment \
204141 --query 'service.deployments[?rolloutState==`IN_PROGRESS`].[id][0]' --output text)
205142 echo "Deployment started: $DEPLOYMENT_ID"
206143 echo "deployment_id=$DEPLOYMENT_ID" >> $GITHUB_ENV
207144 - name : Wait for deployment to complete
208145 run : |
209- source ${{ runner.temp }}/artifact/DEPLOYMENT_ENVS
210- cluster_name=$(echo "$ecs_variables" | jq -r '.cluster_name')
211- service_name=$(echo "$ecs_variables" | jq -r '.good_job.service_name')
146+ source ${{ runner.temp }}/DEPLOYMENT_ENVS
212147 DEPLOYMENT_STATE=IN_PROGRESS
213148 while [ "$DEPLOYMENT_STATE" == "IN_PROGRESS" ]; do
214149 echo "Waiting for deployment to complete..."
215150 sleep 30
216- DEPLOYMENT_STATE="$(aws ecs describe-services --cluster $cluster_name --services $service_name \
151+ DEPLOYMENT_STATE="$(aws ecs describe-services --cluster $cluster_name --services $good_job_service \
217152 --query "services[0].deployments[?id == \`$deployment_id\`].[rolloutState][0]" --output text)"
218153 done
219154 if [ "$DEPLOYMENT_STATE" != "COMPLETED" ]; then
0 commit comments