From 5d812c7e354e274b72aa312078cea8df4475cf0f Mon Sep 17 00:00:00 2001 From: James Griffiths Date: Fri, 17 Jan 2025 13:48:14 +0000 Subject: [PATCH] Deploy --- .github/workflows/_deploy-shared.yml | 35 ++++++++++++++++-- .github/workflows/_terraform-shared.yml | 36 ++++++++++++++----- .../build-and-deploy-all-environments.yml | 6 ++++ 3 files changed, 67 insertions(+), 10 deletions(-) diff --git a/.github/workflows/_deploy-shared.yml b/.github/workflows/_deploy-shared.yml index d1b80a5f2..004600ca2 100644 --- a/.github/workflows/_deploy-shared.yml +++ b/.github/workflows/_deploy-shared.yml @@ -6,6 +6,15 @@ on: GITHUB_ACTIONS_ENVIRONMENT: required: true type: string + EB_APP_NAME: + required: true + type: string + EB_ENVIRONMENT_NAME: + required: true + type: string + EB_CODE_BUCKET: + required: true + type: string secrets: AWS_ACCESS_KEY_ID: required: true @@ -17,13 +26,35 @@ env: TERRAFORM_STATE_BUCKET: uk-gov-equality-hub-terraform-state-bucket jobs: - terraform: - name: 'Terraform (${{ inputs.GITHUB_ACTIONS_ENVIRONMENT }})' + deploy: + name: 'Deploy (${{ inputs.GITHUB_ACTIONS_ENVIRONMENT }})' runs-on: ubuntu-latest environment: ${{ inputs.GITHUB_ACTIONS_ENVIRONMENT }} + env: + ZIP_FILE_NAME: "run${{ github.run_id }}_${{ inputs.GITHUB_ACTIONS_ENVIRONMENT }}_attempt${{ github.run_attempt }}.zip" + EB_APP_NAME: "${{ inputs.TERRAFORM_SERVICE_NAME }}__${{ inputs.TERRAFORM_ENVIRONMENT_NAME }}__Elastic_Beanstalk_Application" + EB_ENVIRONMENT_NAME: "${{ inputs.TERRAFORM_SERVICE_NAME_HYPHENS }}--${{ inputs.TERRAFORM_ENVIRONMENT_NAME }}--EB-Env" + EB_VERSION_LABEL: "v_run${{ github.run_id }}_${{ inputs.GITHUB_ACTIONS_ENVIRONMENT }}_attempt${{ github.run_attempt }}" + steps: - name: 'Download build zip from GitHub Actions artifacts' uses: actions/download-artifact@v4 with: name: build-zip + + - name: 'Copy the zip file to AWS S3' + run: | + aws s3 cp ./build.zip "s3://${{ env.EB_CODE_BUCKET }}/${{ env.ZIP_FILE_NAME }}" + + - name: 'Create Elastic Beanstalk application version' + run: | + aws elasticbeanstalk create-application-version --application-name "${{ env.EB_APP_NAME }}" --version-label "${{ env.EB_VERSION_LABEL }}" --source-bundle "S3Bucket=${{ env.EB_CODE_BUCKET }},S3Key=${{ env.ZIP_FILE_NAME }}" + + - name: 'Deploy new version to Elastic Beanstalk instances' + run: | + aws elasticbeanstalk update-environment --application-name "${{ env.EB_APP_NAME }}" --environment-name "${{ env.EB_ENVIRONMENT_NAME }}" --version-label "${{ env.EB_VERSION_LABEL }}" + + - name: 'Wait for the Elastic Beanstalk environment to finish updating (to prevent us trying to deploy two changes at once)' + run: | + aws elasticbeanstalk wait environment-updated --application-name "${{ env.EB_APP_NAME }}" --environment-name "${{ env.EB_ENVIRONMENT_NAME }}" --version-label "${{ env.EB_VERSION_LABEL }}" diff --git a/.github/workflows/_terraform-shared.yml b/.github/workflows/_terraform-shared.yml index 2e5044c74..27da9dbe9 100644 --- a/.github/workflows/_terraform-shared.yml +++ b/.github/workflows/_terraform-shared.yml @@ -47,6 +47,10 @@ on: required: false EHRC_API_TOKEN: required: true + outputs: + main_app_elastic_beanstalk_application_name: ${{ jobs.terraform.outputs.main_app_elastic_beanstalk_application_name }} + main_app_elastic_beanstalk_environment_name: ${{ jobs.terraform.outputs.main_app_elastic_beanstalk_environment_name }} + main_app_elastic_beanstalk_code_s3_bucket: ${{ jobs.terraform.outputs.main_app_elastic_beanstalk_code_s3_bucket }} env: AWS_ACCESS_KEY_ID: ${{ secrets.TERRAFORM_AWS_ACCESS_KEY_ID }} @@ -60,8 +64,10 @@ jobs: runs-on: ubuntu-latest environment: ${{ inputs.GITHUB_ACTIONS_ENVIRONMENT }} -# outputs: -# main_app_elastic_beanstalk_code_s3_bucket: ${{ steps.step_elastic_beanstalk_code_s3_bucket.outputs.test }} + outputs: + main_app_elastic_beanstalk_application_name: ${{ steps.step_elastic_beanstalk_application_name.outputs.test }} + main_app_elastic_beanstalk_environment_name: ${{ steps.step_elastic_beanstalk_environment_name.outputs.test }} + main_app_elastic_beanstalk_code_s3_bucket: ${{ steps.step_elastic_beanstalk_code_s3_bucket.outputs.test }} steps: - name: 'Checkout Code' @@ -99,9 +105,23 @@ jobs: run: | aws s3 cp "s3://${{ env.TERRAFORM_STATE_BUCKET }}/${{ inputs.TERRAFORM_STATE_FILE }}" ./current_tf_state.json -# - id: step_elastic_beanstalk_code_s3_bucket -# name: 'Find the name of the Elastic Beanstalk application code versions S3 bucket' -# run: | -# echo test=$(jq -r '.resources[] | select(.name == "main_app_elastic_beanstalk_code_s3_bucket").instances[].attributes.bucket' current_tf_state.json) >> main_app_elastic_beanstalk_code_s3_bucket.txt -# cat main_app_elastic_beanstalk_code_s3_bucket.txt -# cat main_app_elastic_beanstalk_code_s3_bucket.txt >> $GITHUB_OUTPUT + - id: step_elastic_beanstalk_application_name + name: 'Find the name of the Elastic Beanstalk application name' + run: | + echo test=$(jq -r '.resources[] | select(.name == "main_app_elastic_beanstalk_application").instances[].attributes.name' current_tf_state.json) >> main_app_elastic_beanstalk_application_name.txt + cat main_app_elastic_beanstalk_application_name.txt + cat main_app_elastic_beanstalk_application_name.txt >> $GITHUB_OUTPUT + + - id: step_elastic_beanstalk_environment_name + name: 'Find the name of the Elastic Beanstalk environment name' + run: | + echo test=$(jq -r '.resources[] | select(.name == "main_app_elastic_beanstalk_environment").instances[].attributes.name' current_tf_state.json) >> main_app_elastic_beanstalk_environment_name.txt + cat main_app_elastic_beanstalk_environment_name.txt + cat main_app_elastic_beanstalk_environment_name.txt >> $GITHUB_OUTPUT + + - id: step_elastic_beanstalk_code_s3_bucket + name: 'Find the name of the Elastic Beanstalk application code versions S3 bucket' + run: | + echo test=$(jq -r '.resources[] | select(.name == "main_app_elastic_beanstalk_code_s3_bucket").instances[].attributes.bucket' current_tf_state.json) >> main_app_elastic_beanstalk_code_s3_bucket.txt + cat main_app_elastic_beanstalk_code_s3_bucket.txt + cat main_app_elastic_beanstalk_code_s3_bucket.txt >> $GITHUB_OUTPUT diff --git a/.github/workflows/build-and-deploy-all-environments.yml b/.github/workflows/build-and-deploy-all-environments.yml index d5585d562..8f77162bc 100644 --- a/.github/workflows/build-and-deploy-all-environments.yml +++ b/.github/workflows/build-and-deploy-all-environments.yml @@ -156,6 +156,9 @@ jobs: uses: ./.github/workflows/_deploy-shared.yml with: GITHUB_ACTIONS_ENVIRONMENT: dev + EB_APP_NAME: ${{ needs.terraform_dev.outputs.main_app_elastic_beanstalk_application_name }} + EB_ENVIRONMENT_NAME: ${{ needs.terraform_dev.outputs.main_app_elastic_beanstalk_environment_name }} + EB_CODE_BUCKET: ${{ needs.terraform_dev.outputs.main_app_elastic_beanstalk_code_s3_bucket }} secrets: AWS_ACCESS_KEY_ID: ${{ secrets.TERRAFORM_AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.TERRAFORM_AWS_SECRET_ACCESS_KEY }} @@ -204,6 +207,9 @@ jobs: uses: ./.github/workflows/_deploy-shared.yml with: GITHUB_ACTIONS_ENVIRONMENT: prod + EB_APP_NAME: ${{ needs.terraform_prod.outputs.main_app_elastic_beanstalk_application_name }} + EB_ENVIRONMENT_NAME: ${{ needs.terraform_prod.outputs.main_app_elastic_beanstalk_environment_name }} + EB_CODE_BUCKET: ${{ needs.terraform_prod.outputs.main_app_elastic_beanstalk_code_s3_bucket }} secrets: AWS_ACCESS_KEY_ID: ${{ secrets.TERRAFORM_AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.TERRAFORM_AWS_SECRET_ACCESS_KEY }}