Skip to content

Commit

Permalink
Deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgriff committed Jan 17, 2025
1 parent f958aca commit 5d812c7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
35 changes: 33 additions & 2 deletions .github/workflows/_deploy-shared.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}"
36 changes: 28 additions & 8 deletions .github/workflows/_terraform-shared.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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'
Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions .github/workflows/build-and-deploy-all-environments.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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 }}

0 comments on commit 5d812c7

Please sign in to comment.