1- name : Deploy Infrastructure with Terraform
2- run-name : Deploy AWS infrastructure for ${{ inputs.environment }}
1+ name : Deploy infrastructure
2+ run-name : Deploy infrastructure for ${{ inputs.environment }}
33
44on :
55 workflow_dispatch :
66 inputs :
77 environment :
8- description : " Deployment environment"
8+ description : Deployment environment
99 required : true
1010 type : choice
1111 options :
1515 - test
1616 - preview
1717 - training
18- # - production
18+ - production
19+ docker_sha :
20+ description : " Docker image sha to deploy. This is used only if no existing task definition is found"
21+ required : false
22+ type : string
1923 workflow_call :
2024 inputs :
2125 environment :
22- description : " Deployment environment"
26+ description : Deployment environment
2327 required : true
2428 type : string
2529
2630env :
2731 aws_role : ${{ inputs.environment == 'production'
28- && 'arn:aws:iam::820242920762:role/GitHubActionsRole '
32+ && 'arn:aws:iam::820242920762:role/GithubDeployMavisAndInfrastructure '
2933 || 'arn:aws:iam::393416225559:role/GithubDeployMavisAndInfrastructure' }}
3034 tf_dir : terraform/app
3135
3236jobs :
33- TerraformPlan :
37+ plan :
38+ name : Terraform plan
3439 runs-on : ubuntu-latest
3540 permissions :
3641 id-token : write
@@ -47,36 +52,24 @@ jobs:
4752 with :
4853 terraform_version : 1.10.5
4954 - name : Install AWS Cli
50- run : |
51- sudo snap install --classic aws-cli
55+ run : sudo snap install --classic aws-cli
5256 - name : Check if any deployments are running
5357 working-directory : ${{ env.tf_dir }}
5458 run : |
55- set -e
56- terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade
57- APPLICATION_NAME=$(terraform output -raw codedeploy_application_name)
58- echo "Application Name: $APPLICATION_NAME"
59- APPLICATION_GROUP=$(terraform output -raw codedeploy_deployment_group_name)
60- echo "Deployment Group Name: $APPLICATION_GROUP"
61- running_deployment=$(aws deploy list-deployments --application-name $APPLICATION_NAME \
62- --deployment-group-name $APPLICATION_GROUP --include-only-statuses InProgress \
63- --query 'deployments[0]' --output text)
64- if [ "$running_deployment" != "None" ]; then
65- echo "A mavis deployment for ${{ inputs.environment }} is currently running: $running_deployment"
66- echo "Aborting infrastructure deployment"
67- exit 1
68- fi
69- - name : Get saved image digest
59+ ../scripts/check-for-running-deployments.sh ${{ inputs.environment }}
60+ - name : Get image digest
7061 working-directory : ${{ env.tf_dir }}
7162 run : |
72- DIGEST=$(terraform state show aws_ecs_task_definition.task_definition | grep -oP '(?<=mavis/webapp@)sha256:[0-9a-z]{64}')
73- if [ -z "$DIGEST" ]; then
74- echo "Image digest not found in the currently deployed task definition"
75- echo "Aborting infrastructure deployment"
76- exit 1
63+ DIGEST="${{ inputs.docker_sha }}"
64+ if terraform state list | grep -q 'aws_ecs_task_definition.task_definition'; then
65+ DIGEST=$(terraform state show aws_ecs_task_definition.task_definition | grep -oP '(?<=mavis/webapp@)sha256:[0-9a-z]{64}')
66+ echo "Existing task definition found, using image digest from the state: $DIGEST"
67+ elif [ -z "$DIGEST" ]; then
68+ echo "Aborting infrastructure deployment: Missing existing task definition or image digest input parameter"
69+ else
70+ echo "No existing task definition found: Using image digest from the input parameter: $DIGEST"
7771 fi
7872 echo "DIGEST=$DIGEST" >> $GITHUB_ENV
79- echo "Image digest in terraform state: $DIGEST"
8073 - name : Terraform Plan
8174 id : plan
8275 working-directory : ${{ env.tf_dir }}
9386 with :
9487 name : tfplan_infrastructure
9588 path : ${{ runner.temp }}/tfplan
96- ApplyUpdate :
89+
90+ apply :
91+ name : Terraform apply
9792 runs-on : ubuntu-latest
98- needs : TerraformPlan
93+ needs : plan
9994 environment : ${{ inputs.environment }}
10095 permissions :
10196 id-token : write
0 commit comments