|
| 1 | +# See terraform/aws/curvenote/README.md |
| 2 | +name: Terraform aws-curvenote |
| 3 | + |
| 4 | +on: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - main |
| 8 | + paths: |
| 9 | + - "terraform/aws/curvenote/**" |
| 10 | + - .github/workflows/terraform-deploy-aws-curvenote.yml |
| 11 | + workflow_dispatch: |
| 12 | + |
| 13 | +# Only allow one workflow to run at a time |
| 14 | +concurrency: terraform-deploy-aws-curvenote |
| 15 | + |
| 16 | +env: |
| 17 | + TFPLAN: aws-curvenote.tfplan |
| 18 | + AWS_DEPLOYMENT_ROLE: arn:aws:iam::166088433508:role/binderhub-github-oidc-mybinderorgdeploy-terraform |
| 19 | + AWS_REGION: us-east-2 |
| 20 | + WORKDIR: ./terraform/aws/curvenote |
| 21 | + |
| 22 | +jobs: |
| 23 | + terraform-plan: |
| 24 | + runs-on: ubuntu-22.04 |
| 25 | + timeout-minutes: 10 |
| 26 | + # These permissions are needed to interact with GitHub's OIDC Token endpoint. |
| 27 | + permissions: |
| 28 | + id-token: write |
| 29 | + contents: read |
| 30 | + defaults: |
| 31 | + run: |
| 32 | + working-directory: ${{ env.WORKDIR }} |
| 33 | + outputs: |
| 34 | + apply: ${{ steps.terraform-plan.outputs.apply }} |
| 35 | + |
| 36 | + steps: |
| 37 | + - uses: actions/checkout@v3 |
| 38 | + |
| 39 | + - name: Configure AWS credentials |
| 40 | + uses: aws-actions/configure-aws-credentials@v2 |
| 41 | + with: |
| 42 | + role-to-assume: ${{ env.AWS_DEPLOYMENT_ROLE }} |
| 43 | + aws-region: ${{ env.AWS_REGION }} |
| 44 | + role-session-name: terraform-plan |
| 45 | + |
| 46 | + # Capture the console output of terraform plan to a file, so we can include |
| 47 | + # it as a job summary in the Actions view: |
| 48 | + # https://github.blog/2022-05-09-supercharging-github-actions-with-job-summaries/ |
| 49 | + - name: Terraform plan |
| 50 | + id: terraform-plan |
| 51 | + run: | |
| 52 | + terraform init |
| 53 | + terraform plan -out="${TFPLAN}" -detailed-exitcode -no-color | tee tfplan.stdout |
| 54 | + # Get the exit code of the terraform plan command, not the tee command. |
| 55 | + EXIT_CODE="${PIPESTATUS[0]}" |
| 56 | + if [ $EXIT_CODE -eq 0 ]; then |
| 57 | + echo "No changes" |
| 58 | + echo "apply=false" >> "$GITHUB_OUTPUT" |
| 59 | + elif [ $EXIT_CODE -eq 2 ]; then |
| 60 | + echo "Changes found" |
| 61 | + echo "apply=true" >> "$GITHUB_OUTPUT" |
| 62 | + else |
| 63 | + echo "Terraform plan failed" |
| 64 | + exit $EXIT_CODE |
| 65 | + fi |
| 66 | +
|
| 67 | + # Skip the first bit of the terraform plan stdout |
| 68 | + # https://unix.stackexchange.com/a/205680 |
| 69 | + - name: Set job summary |
| 70 | + if: steps.terraform-plan.outputs.apply == 'true' |
| 71 | + run: | |
| 72 | + echo '### Terraform Plan summary' >> $GITHUB_STEP_SUMMARY |
| 73 | + echo '```' >> $GITHUB_STEP_SUMMARY |
| 74 | + sed -n '/Terraform will perform the following/,$p' tfplan.stdout >> $GITHUB_STEP_SUMMARY |
| 75 | + echo '```' >> $GITHUB_STEP_SUMMARY |
| 76 | +
|
| 77 | + - name: Install age |
| 78 | + if: steps.terraform-plan.outputs.apply == 'true' |
| 79 | + run: | |
| 80 | + sudo apt-get update -y -q |
| 81 | + sudo apt-get install -y -q age |
| 82 | +
|
| 83 | + - name: Encrypt plan |
| 84 | + if: steps.terraform-plan.outputs.apply == 'true' |
| 85 | + run: | |
| 86 | + echo ${{ secrets.TFPLAN_ARTIFACT_SECRET_KEY }} > tfplan.key |
| 87 | + age --identity tfplan.key --encrypt --output "${TFPLAN}.enc" "${TFPLAN}" |
| 88 | +
|
| 89 | + - name: Upload plan |
| 90 | + if: steps.terraform-plan.outputs.apply == 'true' |
| 91 | + uses: actions/upload-artifact@v3 |
| 92 | + with: |
| 93 | + name: ${{ env.TFPLAN }} |
| 94 | + path: ${{ env.WORKDIR }}/${{ env.TFPLAN }}.enc |
| 95 | + if-no-files-found: error |
| 96 | + |
| 97 | + terraform-apply: |
| 98 | + needs: |
| 99 | + - terraform-plan |
| 100 | + runs-on: ubuntu-22.04 |
| 101 | + timeout-minutes: 60 |
| 102 | + # This environment requires approval before the deploy is run. |
| 103 | + environment: aws-curvenote |
| 104 | + # These permissions are needed to interact with GitHub's OIDC Token endpoint. |
| 105 | + permissions: |
| 106 | + id-token: write |
| 107 | + contents: read |
| 108 | + defaults: |
| 109 | + run: |
| 110 | + working-directory: ${{ env.WORKDIR }} |
| 111 | + if: needs.terraform-plan.outputs.apply == 'true' |
| 112 | + |
| 113 | + steps: |
| 114 | + - uses: actions/checkout@v3 |
| 115 | + |
| 116 | + - name: Configure AWS credentials |
| 117 | + uses: aws-actions/configure-aws-credentials@v2 |
| 118 | + with: |
| 119 | + role-to-assume: ${{ env.AWS_DEPLOYMENT_ROLE }} |
| 120 | + aws-region: ${{ env.AWS_REGION }} |
| 121 | + role-session-name: terraform-apply |
| 122 | + |
| 123 | + - name: Download plan |
| 124 | + uses: actions/download-artifact@v3 |
| 125 | + with: |
| 126 | + name: ${{ env.TFPLAN }} |
| 127 | + path: ${{ env.WORKDIR }} |
| 128 | + |
| 129 | + - name: Install age |
| 130 | + run: | |
| 131 | + sudo apt-get update -y -q |
| 132 | + sudo apt-get install -y -q age |
| 133 | +
|
| 134 | + - name: Decrypt plan |
| 135 | + run: | |
| 136 | + echo ${{ secrets.TFPLAN_ARTIFACT_SECRET_KEY }} > tfplan.key |
| 137 | + age --identity tfplan.key --decrypt --output "${TFPLAN}" "${TFPLAN}.enc" |
| 138 | +
|
| 139 | + - name: Terraform apply |
| 140 | + run: | |
| 141 | + terraform init |
| 142 | + terraform apply "${TFPLAN}" |
0 commit comments