TF Deploy Mavis on poc #24
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: TF Deploy Mavis | |
run-name: TF Deploy Mavis on ${{ inputs.environment }} | |
on: | |
workflow_dispatch: | |
inputs: | |
environment: | |
description: "Deployment environment" | |
required: true | |
type: choice | |
options: | |
- qa | |
- poc | |
- copilotmigration | |
- test | |
- preview | |
- training | |
# - production | |
image_tag: | |
description: "Docker image tag" | |
required: false | |
type: string | |
env: | |
aws-role: ${{ inputs.environment == 'production' | |
&& 'arn:aws:iam::820242920762:role/GitHubActionsRole' | |
|| 'arn:aws:iam::393416225559:role/GithubDeployMavisAndInfrastructure' }} | |
tf-dir: terraform/app | |
jobs: | |
PlanUpdate: | |
runs-on: ubuntu-latest | |
permissions: | |
id-token: write | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Configure AWS Credentials | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
role-to-assume: ${{ env.aws-role }} | |
aws-region: eu-west-2 | |
- name: Login to ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v2 | |
- name: pull docker image | |
run: | | |
DOCKER_IMAGE="${{ steps.login-ecr.outputs.registry }}/mavis/webapp:${{ inputs.image_tag || github.sha }}" | |
docker pull "$DOCKER_IMAGE" | |
echo "DOCKER_IMAGE=$DOCKER_IMAGE" >> $GITHUB_ENV | |
- name: Extract image digest | |
run: | | |
DOCKER_DIGEST=$(docker inspect --format='{{index .RepoDigests 0}}' "$DOCKER_IMAGE") | |
DIGEST="${DOCKER_DIGEST#*@}" | |
echo "DIGEST=$DIGEST" >> $GITHUB_ENV | |
- name: Install terraform | |
uses: hashicorp/setup-terraform@v3 | |
with: | |
terraform_version: 1.10.5 | |
- name: Update the task definition | |
id: plan | |
working-directory: ${{ env.tf-dir }} | |
run: | | |
terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" | |
terraform plan -var="image_digest=$DIGEST" -target=aws_ecs_task_definition.task_definition \ | |
-target=aws_s3_object.appspec_object -var-file="env/${{ inputs.environment }}.tfvars" \ | |
-out=${{ runner.temp }}/tfplan | tee ${{ runner.temp }}/tf_stdout | |
- name: Validate the changes | |
run: | | |
./terraform/scripts/check_task_definition.sh ${{ runner.temp }}/tf_stdout | |
- name: Upload artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: tfplan | |
path: ${{ runner.temp }}/tfplan | |
DeployUpdate: | |
runs-on: ubuntu-latest | |
needs: PlanUpdate | |
environment: ${{ inputs.environment }} | |
permissions: | |
id-token: write | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Configure AWS Credentials | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
role-to-assume: ${{ env.aws-role }} | |
aws-region: eu-west-2 | |
- name: Download artifact | |
uses: actions/download-artifact@v4 | |
with: | |
name: tfplan | |
path: ${{ runner.temp }}/artifact | |
- name: Install terraform | |
uses: hashicorp/setup-terraform@v3 | |
with: | |
terraform_version: 1.10.5 | |
- name: Apply the changes | |
working-directory: ${{ env.tf-dir }} | |
run: | | |
terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" | |
terraform apply ${{ runner.temp }}/artifact/tfplan | |
echo "s3_bucket=$(terraform output -raw s3_bucket)" >> ${{ runner.temp }}/CODEDEPLOY_ENV | |
echo "s3_key=$(terraform output -raw s3_key)" >> ${{ runner.temp }}/CODEDEPLOY_ENV | |
echo "application=$(terraform output -raw codedeploy_application_name)" >> ${{ runner.temp }}/CODEDEPLOY_ENV | |
echo "application_group=$(terraform output -raw codedeploy_deployment_group_name)" >> ${{ runner.temp }}/CODEDEPLOY_ENV | |
- name: Upload artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: CODEDEPLOY_ENV | |
path: ${{ runner.temp }}/CODEDEPLOY_ENV | |
TriggerCodeDeploy: | |
runs-on: ubuntu-latest | |
needs: DeployUpdate | |
environment: ${{ inputs.environment }} | |
permissions: | |
id-token: write | |
steps: | |
- name: Download artifact | |
uses: actions/download-artifact@v4 | |
with: | |
name: CODEDEPLOY_ENV | |
path: ${{ runner.temp }}/artifact | |
- name: Configure AWS Credentials | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
role-to-assume: ${{ env.aws-role }} | |
aws-region: eu-west-2 | |
- name: Install AWS CLI | |
run: | | |
sudo snap install --classic aws-cli | |
- name: Trigger CodeDeploy deployment | |
run: | | |
source ${{ runner.temp }}/artifact/CODEDEPLOY_ENV | |
deployment_id=$(aws deploy create-deployment \ | |
--application-name $application --deployment-group-name $application_group \ | |
--s3-location bucket=appspec-bucket-poc,key=appspec.yaml,bundleType=yaml \ | |
| jq -r .deploymentId) | |
echo "Deployment started: $deployment_id" | |
echo "deployment_id=$deployment_id" >> $GITHUB_ENV | |
- name: Wait up to 30 minutes for deployment to complete | |
run: | | |
aws deploy wait deployment-successful --deployment-id $deployment_id | |
echo "Deployment successful" |