|
| 1 | +name: Release AMI Nix |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - develop |
| 7 | + paths: |
| 8 | + - '.github/workflows/ami-release-nix.yml' |
| 9 | + - 'common-nix.vars.pkr.hcl' |
| 10 | + workflow_dispatch: |
| 11 | + |
| 12 | +jobs: |
| 13 | + build: |
| 14 | + strategy: |
| 15 | + matrix: |
| 16 | + include: |
| 17 | + - runner: arm-runner |
| 18 | + arch: arm64 |
| 19 | + ubuntu_release: focal |
| 20 | + ubuntu_version: 20.04 |
| 21 | + mcpu: neoverse-n1 |
| 22 | + runs-on: ${{ matrix.runner }} |
| 23 | + timeout-minutes: 150 |
| 24 | + permissions: |
| 25 | + contents: write |
| 26 | + packages: write |
| 27 | + id-token: write |
| 28 | + |
| 29 | + steps: |
| 30 | + - name: Checkout Repo |
| 31 | + uses: actions/checkout@v3 |
| 32 | + |
| 33 | + - name: Run checks if triggered manually |
| 34 | + if: ${{ github.event_name == 'workflow_dispatch' }} |
| 35 | + # Update `ci.yaml` too if changing constraints. |
| 36 | + run: | |
| 37 | + SUFFIX=$(sed -E 's/postgres-version = "[0-9\.]+(.*)"/\1/g' common-nix.vars.pkr.hcl) |
| 38 | + if [[ -z $SUFFIX ]] ; then |
| 39 | + echo "Version must include non-numeric characters if built manually." |
| 40 | + exit 1 |
| 41 | + fi |
| 42 | +
|
| 43 | + # extensions are build in nix prior to this step |
| 44 | + # so we can just use the binaries from the nix store |
| 45 | + # for postgres, extensions and wrappers |
| 46 | + |
| 47 | + - name: Build AMI stage 1 |
| 48 | + run: | |
| 49 | + packer init amazon-arm64-nix.pkr.hcl |
| 50 | + GIT_SHA=${{github.sha}} |
| 51 | + packer build -var "git-head-version=${GIT_SHA}" -var "packer-execution-id=${GITHUB_RUN_ID}" -var-file="development-arm.vars.pkr.hcl" -var-file="common-nix.vars.pkr.hcl" -var "ansible_arguments=" amazon-arm64-nix.pkr.hcl |
| 52 | +
|
| 53 | + - name: Build AMI stage 2 |
| 54 | + run: | |
| 55 | + packer init stage2-nix-psql.pkr.hcl |
| 56 | + GIT_SHA=${{github.sha}} |
| 57 | + packer build -var "git-head-version=${GIT_SHA}" -var "packer-execution-id=${GITHUB_RUN_ID}" -var-file="development-arm.vars.pkr.hcl" -var-file="common-nix.vars.pkr.hcl" stage2-nix-psql.pkr.hcl |
| 58 | +
|
| 59 | + - name: Grab release version |
| 60 | + id: process_release_version |
| 61 | + run: | |
| 62 | + VERSION=$(sed -e 's/postgres-version = "\(.*\)"/\1/g' common-nix.vars.pkr.hcl) |
| 63 | + echo "version=$VERSION" >> "$GITHUB_OUTPUT" |
| 64 | +
|
| 65 | + - name: Create nix flake revision tarball |
| 66 | + run: | |
| 67 | + GIT_SHA=${{github.sha}} |
| 68 | + MAJOR_VERSION=$(echo "${{ steps.process_release_version.outputs.version }}" | cut -d. -f1) |
| 69 | +
|
| 70 | + mkdir -p "/tmp/pg_upgrade_bin/${MAJOR_VERSION}" |
| 71 | + echo "$GIT_SHA" >> "/tmp/pg_upgrade_bin/${MAJOR_VERSION}/nix_flake_version" |
| 72 | + tar -czf "/tmp/pg_binaries.tar.gz" -C "/tmp/pg_upgrade_bin" . |
| 73 | +
|
| 74 | + - name: configure aws credentials - staging |
| 75 | + uses: aws-actions/configure-aws-credentials@v4 |
| 76 | + with: |
| 77 | + role-to-assume: ${{ secrets.DEV_AWS_ROLE }} |
| 78 | + aws-region: "us-east-1" |
| 79 | + |
| 80 | + - name: Upload software manifest to s3 staging |
| 81 | + run: | |
| 82 | + cd ansible |
| 83 | + ansible-playbook -i localhost \ |
| 84 | + -e "ami_release_version=${{ steps.process_release_version.outputs.version }}" \ |
| 85 | + -e "internal_artifacts_bucket=${{ secrets.ARTIFACTS_BUCKET }}" \ |
| 86 | + manifest-playbook.yml |
| 87 | +
|
| 88 | + - name: Upload nix flake revision to s3 staging |
| 89 | + run: | |
| 90 | + aws s3 cp /tmp/pg_binaries.tar.gz s3://${{ secrets.ARTIFACTS_BUCKET }}/upgrades/postgres/supabase-postgres-${{ steps.process_release_version.outputs.version }}/20.04.tar.gz |
| 91 | +
|
| 92 | + #Our self hosted github runner already has permissions to publish images |
| 93 | + #but they're limited to only that; |
| 94 | + #so if we want s3 access we'll need to config credentials with the below steps |
| 95 | + # (which overwrites existing perms) after the ami build |
| 96 | + |
| 97 | + - name: configure aws credentials - prod |
| 98 | + uses: aws-actions/configure-aws-credentials@v4 |
| 99 | + with: |
| 100 | + role-to-assume: ${{ secrets.PROD_AWS_ROLE }} |
| 101 | + aws-region: "us-east-1" |
| 102 | + |
| 103 | + - name: Upload software manifest to s3 prod |
| 104 | + run: | |
| 105 | + cd ansible |
| 106 | + ansible-playbook -i localhost \ |
| 107 | + -e "ami_release_version=${{ steps.process_release_version.outputs.version }}" \ |
| 108 | + -e "internal_artifacts_bucket=${{ secrets.PROD_ARTIFACTS_BUCKET }}" \ |
| 109 | + manifest-playbook.yml |
| 110 | + |
| 111 | + - name: Upload nix flake revision to s3 prod |
| 112 | + run: | |
| 113 | + aws s3 cp /tmp/pg_binaries.tar.gz s3://${{ secrets.PROD_ARTIFACTS_BUCKET }}/upgrades/postgres/supabase-postgres-${{ steps.process_release_version.outputs.version }}/20.04.tar.gz |
| 114 | +
|
| 115 | + - name: Create release |
| 116 | + uses: softprops/action-gh-release@v1 |
| 117 | + with: |
| 118 | + name: ${{ steps.process_release_version.outputs.version }} |
| 119 | + tag_name: ${{ steps.process_release_version.outputs.version }} |
| 120 | + target_commitish: ${{github.sha}} |
| 121 | + |
| 122 | + - name: Slack Notification on Failure |
| 123 | + if: ${{ failure() }} |
| 124 | + uses: rtCamp/action-slack-notify@v2 |
| 125 | + env: |
| 126 | + SLACK_WEBHOOK: ${{ secrets.SLACK_NOTIFICATIONS_WEBHOOK }} |
| 127 | + SLACK_USERNAME: 'gha-failures-notifier' |
| 128 | + SLACK_COLOR: 'danger' |
| 129 | + SLACK_MESSAGE: 'Building Postgres AMI failed' |
| 130 | + SLACK_FOOTER: '' |
| 131 | + |
| 132 | + - name: Cleanup resources on build cancellation |
| 133 | + if: ${{ cancelled() }} |
| 134 | + run: | |
| 135 | + aws ec2 describe-instances --filters "Name=tag:packerExecutionId,Values=${GITHUB_RUN_ID}" --query "Reservations[].Instances[].InstanceId" --output text | xargs -n 1 -I {} aws ec2 terminate-instances --instance-ids {} |
0 commit comments