@@ -150,3 +150,136 @@ jobs:
150150 run : |
151151 aws ec2 describe-instances --filters "Name=tag:packerExecutionId,Values=${GITHUB_RUN_ID}" --query "Reservations[].Instances[].InstanceId" --output text | xargs -r aws ec2 terminate-instances --instance-ids
152152
153+ name : Release Single AMI Nix
154+
155+ on :
156+ workflow_dispatch :
157+ inputs :
158+ postgres_version :
159+ description : ' PostgreSQL major version to build (e.g. 15)'
160+ required : true
161+ type : string
162+ branch :
163+ description : ' Branch to run the workflow from'
164+ required : true
165+ type : string
166+ default : ' main'
167+
168+ permissions :
169+ contents : write
170+ id-token : write
171+
172+ jobs :
173+ build :
174+ runs-on : arm-runner
175+ timeout-minutes : 150
176+
177+ steps :
178+ - name : Checkout Repo
179+ uses : actions/checkout@v3
180+ with :
181+ ref : ${{ github.event.inputs.branch }}
182+
183+ - name : Get current branch SHA
184+ id : get_sha
185+ run : |
186+ echo "sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
187+ - uses : DeterminateSystems/nix-installer-action@main
188+
189+ - name : Set PostgreSQL version environment variable
190+ run : echo "POSTGRES_MAJOR_VERSION=${{ github.event.inputs.postgres_version }}" >> $GITHUB_ENV
191+
192+ - name : Generate common-nix.vars.pkr.hcl
193+ run : |
194+ PG_VERSION=$(sudo nix run nixpkgs#yq -- '.postgres_release["postgres'${{ env.POSTGRES_MAJOR_VERSION }}'"]' ansible/vars.yml)
195+ PG_VERSION=$(echo "$PG_VERSION" | tr -d '"') # Remove any surrounding quotes
196+ echo 'postgres-version = "'$PG_VERSION'"' > common-nix.vars.pkr.hcl
197+ # Ensure there's a newline at the end of the file
198+ echo "" >> common-nix.vars.pkr.hcl
199+ - name : Build AMI stage 1
200+ env :
201+ POSTGRES_MAJOR_VERSION : ${{ env.POSTGRES_MAJOR_VERSION }}
202+ run : |
203+ packer init amazon-arm64-nix.pkr.hcl
204+ GIT_SHA=${{ steps.get_sha.outputs.sha }}
205+ 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=-e postgresql_major=${POSTGRES_MAJOR_VERSION}" amazon-arm64-nix.pkr.hcl
206+ - name : Build AMI stage 2
207+ env :
208+ POSTGRES_MAJOR_VERSION : ${{ env.POSTGRES_MAJOR_VERSION }}
209+ run : |
210+ packer init stage2-nix-psql.pkr.hcl
211+ GIT_SHA=${{ steps.get_sha.outputs.sha }}
212+ POSTGRES_MAJOR_VERSION=${{ env.POSTGRES_MAJOR_VERSION }}
213+ packer build -var "git_sha=${GIT_SHA}" -var "git-head-version=${GIT_SHA}" -var "packer-execution-id=${GITHUB_RUN_ID}" -var "postgres_major_version=${POSTGRES_MAJOR_VERSION}" -var-file="development-arm.vars.pkr.hcl" -var-file="common-nix.vars.pkr.hcl" stage2-nix-psql.pkr.hcl
214+ - name : Grab release version
215+ id : process_release_version
216+ run : |
217+ VERSION=$(cat common-nix.vars.pkr.hcl | sed -e 's/postgres-version = "\(.*\)"/\1/g')
218+ echo "version=$VERSION" >> $GITHUB_OUTPUT
219+ - name : Create nix flake revision tarball
220+ run : |
221+ GIT_SHA=${{ steps.get_sha.outputs.sha }}
222+ MAJOR_VERSION=${{ env.POSTGRES_MAJOR_VERSION }}
223+ mkdir -p "/tmp/pg_upgrade_bin/${MAJOR_VERSION}"
224+ echo "$GIT_SHA" >> "/tmp/pg_upgrade_bin/${MAJOR_VERSION}/nix_flake_version"
225+ tar -czf "/tmp/pg_binaries.tar.gz" -C "/tmp/pg_upgrade_bin" .
226+ - name : configure aws credentials - staging
227+ uses : aws-actions/configure-aws-credentials@v4
228+ with :
229+ role-to-assume : ${{ secrets.DEV_AWS_ROLE }}
230+ aws-region : " us-east-1"
231+
232+ - name : Upload software manifest to s3 staging
233+ run : |
234+ cd ansible
235+ ansible-playbook -i localhost \
236+ -e "ami_release_version=${{ steps.process_release_version.outputs.version }}" \
237+ -e "internal_artifacts_bucket=${{ secrets.ARTIFACTS_BUCKET }}" \
238+ -e "postgres_major_version=${{ env.POSTGRES_MAJOR_VERSION }}" \
239+ manifest-playbook.yml
240+ - name : Upload nix flake revision to s3 staging
241+ run : |
242+ 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
243+ - name : configure aws credentials - prod
244+ uses : aws-actions/configure-aws-credentials@v4
245+ with :
246+ role-to-assume : ${{ secrets.PROD_AWS_ROLE }}
247+ aws-region : " us-east-1"
248+
249+ - name : Upload software manifest to s3 prod
250+ run : |
251+ cd ansible
252+ ansible-playbook -i localhost \
253+ -e "ami_release_version=${{ steps.process_release_version.outputs.version }}" \
254+ -e "internal_artifacts_bucket=${{ secrets.PROD_ARTIFACTS_BUCKET }}" \
255+ -e "postgres_major_version=${{ env.POSTGRES_MAJOR_VERSION }}" \
256+ manifest-playbook.yml
257+
258+ - name : Upload nix flake revision to s3 prod
259+ run : |
260+ 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
261+ - name : Create release
262+ uses : softprops/action-gh-release@v2
263+ with :
264+ name : ${{ steps.process_release_version.outputs.version }}
265+ tag_name : ${{ steps.process_release_version.outputs.version }}
266+ target_commitish : ${{ steps.get_sha.outputs.sha }}
267+
268+ - name : Slack Notification on Failure
269+ if : ${{ failure() }}
270+ uses : rtCamp/action-slack-notify@v2
271+ env :
272+ SLACK_WEBHOOK : ${{ secrets.SLACK_NOTIFICATIONS_WEBHOOK }}
273+ SLACK_USERNAME : ' gha-failures-notifier'
274+ SLACK_COLOR : ' danger'
275+ SLACK_MESSAGE : ' Building Postgres AMI failed'
276+ SLACK_FOOTER : ' '
277+
278+ - name : Cleanup resources after build
279+ if : ${{ always() }}
280+ run : |
281+ aws ec2 describe-instances --filters "Name=tag:packerExecutionId,Values=${GITHUB_RUN_ID}" --query "Reservations[].Instances[].InstanceId" --output text | xargs -r aws ec2 terminate-instances --instance-ids
282+ - name : Cleanup resources on build cancellation
283+ if : ${{ cancelled() }}
284+ run : |
285+ aws ec2 describe-instances --filters "Name=tag:packerExecutionId,Values=${GITHUB_RUN_ID}" --query "Reservations[].Instances[].InstanceId" --output text | xargs -r aws ec2 terminate-instances --instance-ids
0 commit comments