|
| 1 | +name: Deploy Account-wide infrastructure |
| 2 | +run-name: Account-wide infra deployment to ${{ inputs.environment }} of ${{ inputs.branch_name }} by ${{ github.actor }} |
| 3 | + |
| 4 | +on: |
| 5 | + workflow_dispatch: |
| 6 | + inputs: |
| 7 | + environment: |
| 8 | + description: "Environment to deploy to" |
| 9 | + required: true |
| 10 | + default: "account-dev" |
| 11 | + type: environment |
| 12 | + branch_name: |
| 13 | + description: Branch to deploy |
| 14 | + required: true |
| 15 | + |
| 16 | +permissions: |
| 17 | + id-token: write |
| 18 | + contents: read |
| 19 | + actions: write |
| 20 | + |
| 21 | +jobs: |
| 22 | + check-selected-environment: |
| 23 | + name: Check Workflow Env |
| 24 | + runs-on: codebuild-nhsd-nrlf-ci-build-project-${{ github.run_id }}-${{ github.run_attempt }} |
| 25 | + steps: |
| 26 | + - name: Validate environment |
| 27 | + env: |
| 28 | + IS_VALID_ENV: ${{ startsWith(inputs.environment, 'account-') }} |
| 29 | + run: | |
| 30 | + echo "valid workflow environment selected:" $IS_VALID_ENV |
| 31 | + if [[ $IS_VALID_ENV == true ]]; then |
| 32 | + exit 0 |
| 33 | + fi |
| 34 | + echo "This workflow can only be run with 'account-*' environments as it deploys account-specific infrastructure" |
| 35 | + exit 1 |
| 36 | +
|
| 37 | + terraform-plan: |
| 38 | + name: Terraform Plan - ${{ inputs.environment }} |
| 39 | + environment: ${{ inputs.environment }} |
| 40 | + needs: [check-selected-environment] |
| 41 | + runs-on: codebuild-nhsd-nrlf-ci-build-project-${{ github.run_id }}-${{ github.run_attempt }} |
| 42 | + |
| 43 | + steps: |
| 44 | + - name: Git clone - ${{ inputs.branch_name }} |
| 45 | + uses: actions/checkout@v4 |
| 46 | + with: |
| 47 | + ref: ${{ inputs.branch_name }} |
| 48 | + |
| 49 | + - name: Setup environment |
| 50 | + run: | |
| 51 | + echo "${HOME}/.asdf/bin" >> $GITHUB_PATH |
| 52 | + poetry install --no-root |
| 53 | +
|
| 54 | + - name: Configure Management Credentials |
| 55 | + uses: aws-actions/configure-aws-credentials@7474bc4690e29a8392af63c5b98e7449536d5c3a #v4.3.1 |
| 56 | + with: |
| 57 | + aws-region: eu-west-2 |
| 58 | + role-to-assume: ${{ secrets.MGMT_ROLE_ARN }} |
| 59 | + role-session-name: github-actions-ci-${{ inputs.environment }}-${{ github.run_id }} |
| 60 | + |
| 61 | + - name: Retrieve Server Certificates |
| 62 | + env: |
| 63 | + ACCOUNT_NAME: ${{ vars.ACCOUNT_NAME }} |
| 64 | + run: | |
| 65 | + make truststore-pull-all-for-account ACCOUNT=${ACCOUNT_NAME} |
| 66 | +
|
| 67 | + - name: Terraform Init |
| 68 | + env: |
| 69 | + ACCOUNT_NAME: ${{ vars.ACCOUNT_NAME }} |
| 70 | + run: | |
| 71 | + terraform -chdir=terraform/account-wide-infrastructure/${ACCOUNT_NAME} init |
| 72 | + terraform -chdir=terraform/account-wide-infrastructure/${ACCOUNT_NAME} workspace new ${ACCOUNT_NAME} || \ |
| 73 | + terraform -chdir=terraform/account-wide-infrastructure/${ACCOUNT_NAME} workspace select ${ACCOUNT_NAME} |
| 74 | +
|
| 75 | + - name: Terraform Plan |
| 76 | + env: |
| 77 | + ACCOUNT_NAME: ${{ vars.ACCOUNT_NAME }} |
| 78 | + ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }} |
| 79 | + run: | |
| 80 | + terraform -chdir=terraform/account-wide-infrastructure/${ACCOUNT_NAME} plan \ |
| 81 | + -var assume_account=${ACCOUNT_ID} \ |
| 82 | + -var assume_role=terraform \ |
| 83 | + -out tfplan |
| 84 | +
|
| 85 | + - name: Save Terraform Plan |
| 86 | + env: |
| 87 | + ACCOUNT_NAME: ${{ vars.ACCOUNT_NAME }} |
| 88 | + run: | |
| 89 | + terraform -chdir=terraform/account-wide-infrastructure/${ACCOUNT_NAME} show -no-color tfplan > terraform/account-wide-infrastructure/$ACCOUNT_NAME/tfplan.txt |
| 90 | +
|
| 91 | + aws s3 cp terraform/account-wide-infrastructure/$ACCOUNT_NAME/tfplan s3://nhsd-nrlf--mgmt--github-ci-logging/acc-$ACCOUNT_NAME/${{ github.run_id }}/tfplan |
| 92 | + aws s3 cp terraform/account-wide-infrastructure/$ACCOUNT_NAME/tfplan.txt s3://nhsd-nrlf--mgmt--github-ci-logging/acc-$ACCOUNT_NAME/${{ github.run_id }}/tfplan.txt |
| 93 | +
|
| 94 | + terraform-apply: |
| 95 | + name: Terraform Apply - ${{ inputs.environment }} |
| 96 | + needs: [terraform-plan] |
| 97 | + runs-on: codebuild-nhsd-nrlf-ci-build-project-${{ github.run_id }}-${{ github.run_attempt }} |
| 98 | + environment: ${{ inputs.environment }} |
| 99 | + |
| 100 | + steps: |
| 101 | + - name: Git clone - ${{ inputs.branch_name }} |
| 102 | + uses: actions/checkout@v4 |
| 103 | + with: |
| 104 | + ref: ${{ inputs.branch_name }} |
| 105 | + |
| 106 | + - name: Setup environment |
| 107 | + run: | |
| 108 | + echo "${HOME}/.asdf/bin" >> $GITHUB_PATH |
| 109 | + poetry install --no-root |
| 110 | +
|
| 111 | + - name: Configure Management Credentials |
| 112 | + uses: aws-actions/configure-aws-credentials@7474bc4690e29a8392af63c5b98e7449536d5c3a #v4.3.1 |
| 113 | + with: |
| 114 | + aws-region: eu-west-2 |
| 115 | + role-to-assume: ${{ secrets.MGMT_ROLE_ARN }} |
| 116 | + role-session-name: github-actions-ci-${{ inputs.environment }}-${{ github.run_id}} |
| 117 | + |
| 118 | + - name: Download Terraform Plan artifact |
| 119 | + env: |
| 120 | + ACCOUNT_NAME: ${{ vars.ACCOUNT_NAME }} |
| 121 | + run: aws s3 cp s3://nhsd-nrlf--mgmt--github-ci-logging/acc-$ACCOUNT_NAME/${{ github.run_id }}/tfplan terraform/account-wide-infrastructure/${ACCOUNT_NAME}/tfplan |
| 122 | + |
| 123 | + - name: Retrieve Server Certificates |
| 124 | + env: |
| 125 | + ACCOUNT_NAME: ${{ vars.ACCOUNT_NAME }} |
| 126 | + run: | |
| 127 | + make truststore-pull-all-for-account ACCOUNT=${ACCOUNT_NAME} |
| 128 | +
|
| 129 | + - name: Terraform Init |
| 130 | + env: |
| 131 | + ACCOUNT_NAME: ${{ vars.ACCOUNT_NAME }} |
| 132 | + run: | |
| 133 | + terraform -chdir=terraform/account-wide-infrastructure/${ACCOUNT_NAME} init |
| 134 | + terraform -chdir=terraform/account-wide-infrastructure/${ACCOUNT_NAME} workspace new ${ACCOUNT_NAME} || \ |
| 135 | + terraform -chdir=terraform/account-wide-infrastructure/${ACCOUNT_NAME} workspace select ${ACCOUNT_NAME} |
| 136 | +
|
| 137 | + - name: Terraform Apply |
| 138 | + env: |
| 139 | + ACCOUNT_NAME: ${{ vars.ACCOUNT_NAME }} |
| 140 | + ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }} |
| 141 | + run: | |
| 142 | + terraform -chdir=terraform/account-wide-infrastructure/${ACCOUNT_NAME} apply tfplan |
| 143 | +
|
| 144 | + - name: Update environment config version |
| 145 | + env: |
| 146 | + ACCOUNT_NAME: ${{ vars.ACCOUNT_NAME }} |
| 147 | + run: | |
| 148 | + deployed_version=$(terraform -chdir=terraform/account-wide-infrastructure/${ACCOUNT_NAME} output --raw version) |
| 149 | + echo $deployed_version |
0 commit comments