Skip to content

Commit ec9b51e

Browse files
committed
refactor(ci): extract common AMI build logic into reusable action and add actionlint
Consolidate duplicate AMI build steps across workflows into a shared composite action. Also introduces actionlint configuration for GitHub Actions validation for the modified workflows.
1 parent 793d9d9 commit ec9b51e

File tree

6 files changed

+168
-155
lines changed

6 files changed

+168
-155
lines changed

.github/actionlint.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
self-hosted-runner:
2+
labels:
3+
- blacksmith-2vcpu-ubuntu-2404
4+
- blacksmith-2vcpu-ubuntu-2404-arm
5+
- blacksmith-4vcpu-ubuntu-2404
6+
- large-linux-arm
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
name: Build AMI
2+
description: Build both stage 1 and stage 2 AMIs
3+
4+
inputs:
5+
postgres_version:
6+
description: 'PostgreSQL major version (e.g., 15)'
7+
required: true
8+
region:
9+
description: 'AWS region'
10+
required: true
11+
ami_regions:
12+
description: 'AMI regions as JSON array (e.g., ["us-east-1"])'
13+
required: true
14+
git_sha:
15+
description: 'Git SHA for this build'
16+
required: true
17+
18+
outputs:
19+
stage2_ami_id:
20+
description: 'The AMI ID of the stage 2 build'
21+
value: ${{ steps.build-stage2.outputs.stage2_ami_id }}
22+
postgres_release_version:
23+
description: 'The PostgreSQL release version'
24+
value: ${{ steps.generate-vars.outputs.version }}
25+
execution_id:
26+
description: 'The execution ID for this build'
27+
value: ${{ steps.set-execution-id.outputs.execution_id }}
28+
29+
runs:
30+
using: "composite"
31+
steps:
32+
- name: Set execution ID
33+
id: set-execution-id
34+
shell: bash
35+
run: |
36+
EXECUTION_ID="${{ github.run_id }}-${{ inputs.postgres_version }}"
37+
echo "EXECUTION_ID=$EXECUTION_ID" >> $GITHUB_ENV
38+
echo "execution_id=$EXECUTION_ID" >> $GITHUB_OUTPUT
39+
40+
- name: Generate common-nix.vars.pkr.hcl
41+
id: generate-vars
42+
shell: bash
43+
run: |
44+
PG_VERSION=$(nix run nixpkgs#yq -- '.postgres_release["postgres${{ inputs.postgres_version }}"]' ansible/vars.yml)
45+
PG_VERSION=$(echo "$PG_VERSION" | tr -d '"')
46+
echo 'postgres-version = "'$PG_VERSION'"' > common-nix.vars.pkr.hcl
47+
echo "" >> common-nix.vars.pkr.hcl
48+
git add -f common-nix.vars.pkr.hcl
49+
echo "version=$PG_VERSION" >> $GITHUB_OUTPUT
50+
51+
- name: Build AMI stage 1
52+
shell: bash
53+
env:
54+
POSTGRES_MAJOR_VERSION: ${{ inputs.postgres_version }}
55+
AWS_MAX_ATTEMPTS: 10
56+
AWS_RETRY_MODE: adaptive
57+
run: |
58+
nix run .#build-ami -- stage1 \
59+
-var "git-head-version=${{ inputs.git_sha }}" \
60+
-var "packer-execution-id=${{ env.EXECUTION_ID }}" \
61+
-var "ansible_arguments=-e postgresql_major=${{ inputs.postgres_version }}" \
62+
-var "region=${{ inputs.region }}" \
63+
-var 'ami_regions=${{ inputs.ami_regions }}' \
64+
-var-file="development-arm.vars.pkr.hcl" \
65+
-var-file="common-nix.vars.pkr.hcl" \
66+
amazon-arm64-nix.pkr.hcl
67+
68+
- name: Build AMI stage 2
69+
id: build-stage2
70+
shell: bash
71+
env:
72+
POSTGRES_MAJOR_VERSION: ${{ inputs.postgres_version }}
73+
PACKER_EXECUTION_ID: ${{ env.EXECUTION_ID }}
74+
AWS_MAX_ATTEMPTS: 10
75+
AWS_RETRY_MODE: adaptive
76+
run: |
77+
nix run .#build-ami -- stage2 \
78+
-var "git-head-version=${{ inputs.git_sha }}" \
79+
-var "packer-execution-id=${{ env.EXECUTION_ID }}" \
80+
-var "postgres_major_version=${{ inputs.postgres_version }}" \
81+
-var-file="development-arm.vars.pkr.hcl" \
82+
-var-file="common-nix.vars.pkr.hcl" \
83+
-var "postgres-version=${{ env.EXECUTION_ID }}" \
84+
-var 'ami_regions=${{ inputs.ami_regions }}' \
85+
-var "force-deregister=true" \
86+
-var "git_sha=${{ inputs.git_sha }}" \
87+
stage2-nix-psql.pkr.hcl

.github/workflows/ami-release-nix-single.yml

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ jobs:
2727
uses: supabase/postgres/.github/actions/shared-checkout@HEAD
2828
with:
2929
ref: ${{ github.event.inputs.branch }}
30+
3031
- name: aws-creds
3132
uses: aws-actions/configure-aws-credentials@v4
3233
with:
@@ -38,56 +39,32 @@ jobs:
3839
- name: Get current branch SHA
3940
id: get_sha
4041
run: |
41-
echo "sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
42+
echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
4243
4344
- name: Install nix
44-
uses: cachix/install-nix-action@v27
45+
uses: ./.github/actions/nix-install-ephemeral
4546
with:
46-
install_url: https://releases.nixos.org/nix/nix-2.29.1/install
47-
extra_nix_config: |
48-
substituters = https://cache.nixos.org https://nix-postgres-artifacts.s3.amazonaws.com
49-
trusted-public-keys = nix-postgres-artifacts:dGZlQOvKcNEjvT7QEAJbcV6b6uk7VF/hWMjhYleiaLI= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=
50-
51-
- name: Set PostgreSQL version environment variable
52-
run: |
53-
echo "POSTGRES_MAJOR_VERSION=${{ github.event.inputs.postgres_version }}" >> $GITHUB_ENV
54-
echo "EXECUTION_ID=${{ github.run_id }}-${{ matrix.postgres_version }}" >> $GITHUB_ENV
47+
push-to-cache: 'true'
5548

56-
- name: Generate common-nix.vars.pkr.hcl
57-
run: |
58-
PG_VERSION=$(nix run nixpkgs#yq -- '.postgres_release["postgres'${{ env.POSTGRES_MAJOR_VERSION }}'"]' ansible/vars.yml)
59-
PG_VERSION=$(echo "$PG_VERSION" | tr -d '"') # Remove any surrounding quotes
60-
echo 'postgres-version = "'$PG_VERSION'"' > common-nix.vars.pkr.hcl
61-
# Ensure there's a newline at the end of the file
62-
echo "" >> common-nix.vars.pkr.hcl
63-
64-
- name: Build AMI stage 1
65-
env:
66-
POSTGRES_MAJOR_VERSION: ${{ env.POSTGRES_MAJOR_VERSION }}
67-
run: |
68-
GIT_SHA=${{ steps.get_sha.outputs.sha }}
69-
nix run github:supabase/postgres/${GIT_SHA}#packer -- init amazon-arm64-nix.pkr.hcl
70-
nix run github:supabase/postgres/${GIT_SHA}#packer -- build -var "git-head-version=${GIT_SHA}" -var "packer-execution-id=${EXECUTION_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
71-
72-
- name: Build AMI stage 2
73-
env:
74-
POSTGRES_MAJOR_VERSION: ${{ env.POSTGRES_MAJOR_VERSION }}
75-
run: |
76-
GIT_SHA=${{ steps.get_sha.outputs.sha }}
77-
nix run github:supabase/postgres/${GIT_SHA}#packer -- init stage2-nix-psql.pkr.hcl
78-
POSTGRES_MAJOR_VERSION=${{ env.POSTGRES_MAJOR_VERSION }}
79-
nix run github:supabase/postgres/${GIT_SHA}#packer -- build -var "git_sha=${GIT_SHA}" -var "git-head-version=${GIT_SHA}" -var "packer-execution-id=${EXECUTION_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
49+
- name: Build AMI
50+
id: build-ami
51+
uses: ./.github/actions/build-ami
52+
with:
53+
postgres_version: ${{ github.event.inputs.postgres_version }}
54+
region: us-east-1
55+
ami_regions: '["us-east-1"]'
56+
git_sha: ${{ steps.get_sha.outputs.sha }}
8057

8158
- name: Grab release version
8259
id: process_release_version
8360
run: |
84-
VERSION=$(cat common-nix.vars.pkr.hcl | sed -e 's/postgres-version = "\(.*\)"/\1/g')
85-
echo "version=$VERSION" >> $GITHUB_OUTPUT
61+
VERSION="${{ steps.build-ami.outputs.postgres_release_version }}"
62+
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
8663
8764
- name: Create nix flake revision tarball
8865
run: |
8966
GIT_SHA=${{ steps.get_sha.outputs.sha }}
90-
MAJOR_VERSION=${{ env.POSTGRES_MAJOR_VERSION }}
67+
MAJOR_VERSION=${{ github.event.inputs.postgres_version }}
9168
9269
mkdir -p "/tmp/pg_upgrade_bin/${MAJOR_VERSION}"
9370
echo "$GIT_SHA" >> "/tmp/pg_upgrade_bin/${MAJOR_VERSION}/nix_flake_version"
@@ -105,7 +82,7 @@ jobs:
10582
ansible-playbook -i localhost \
10683
-e "ami_release_version=${{ steps.process_release_version.outputs.version }}" \
10784
-e "internal_artifacts_bucket=${{ secrets.ARTIFACTS_BUCKET }}" \
108-
-e "postgres_major_version=${{ env.POSTGRES_MAJOR_VERSION }}" \
85+
-e "postgres_major_version=${{ github.event.inputs.postgres_version }}" \
10986
manifest-playbook.yml
11087
11188
- name: Upload nix flake revision to s3 staging
@@ -126,7 +103,7 @@ jobs:
126103
ansible-playbook -i localhost \
127104
-e "ami_release_version=${{ steps.process_release_version.outputs.version }}" \
128105
-e "internal_artifacts_bucket=${{ secrets.PROD_ARTIFACTS_BUCKET }}" \
129-
-e "postgres_major_version=${{ env.POSTGRES_MAJOR_VERSION }}" \
106+
-e "postgres_major_version=${{ github.event.inputs.postgres_version }}" \
130107
manifest-playbook.yml
131108
132109
- name: Upload nix flake revision to s3 prod
@@ -155,10 +132,12 @@ jobs:
155132
- name: Cleanup resources after build
156133
if: ${{ always() }}
157134
run: |
135+
EXECUTION_ID="${{ steps.build-ami.outputs.execution_id }}"
158136
aws ec2 describe-instances --filters "Name=tag:packerExecutionId,Values=${EXECUTION_ID}" --query "Reservations[].Instances[].InstanceId" --output text | xargs -r aws ec2 terminate-instances --instance-ids
159137
160138
- name: Cleanup resources on build cancellation
161139
if: ${{ cancelled() }}
162140
run: |
141+
EXECUTION_ID="${{ steps.build-ami.outputs.execution_id }}"
163142
aws ec2 describe-instances --filters "Name=tag:packerExecutionId,Values=${EXECUTION_ID}" --query "Reservations[].Instances[].InstanceId" --output text | xargs -r aws ec2 terminate-instances --instance-ids
164143

.github/workflows/ami-release-nix.yml

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,13 @@ jobs:
2525
uses: supabase/postgres/.github/actions/shared-checkout@HEAD
2626

2727
- name: Install nix
28-
uses: cachix/install-nix-action@v27
29-
with:
30-
install_url: https://releases.nixos.org/nix/nix-2.29.1/install
31-
extra_nix_config: |
32-
substituters = https://cache.nixos.org https://nix-postgres-artifacts.s3.amazonaws.com
33-
trusted-public-keys = nix-postgres-artifacts:dGZlQOvKcNEjvT7QEAJbcV6b6uk7VF/hWMjhYleiaLI= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=
28+
uses: ./.github/actions/nix-install-ephemeral
3429

3530
- name: Set PostgreSQL versions
3631
id: set-versions
3732
run: |
38-
VERSIONS=$(nix run nixpkgs#yq -- '.postgres_major[]' ansible/vars.yml | nix run nixpkgs#jq -- -R -s -c 'split("\n")[:-1]')
39-
echo "postgres_versions=$VERSIONS" >> $GITHUB_OUTPUT
33+
VERSIONS=$(nix run nixpkgs#yq -- -r '.postgres_major[]' ansible/vars.yml | nix run nixpkgs#jq -- -R -s -c 'split("\n")[:-1]')
34+
echo "postgres_versions=$VERSIONS" >> "$GITHUB_OUTPUT"
4035
4136
build:
4237
needs: prepare
@@ -51,6 +46,9 @@ jobs:
5146
steps:
5247
- name: Checkout Repo
5348
uses: supabase/postgres/.github/actions/shared-checkout@HEAD
49+
with:
50+
push-to-cache: 'true'
51+
5452
- name: aws-creds
5553
uses: aws-actions/configure-aws-credentials@v4
5654
with:
@@ -60,12 +58,7 @@ jobs:
6058
role-duration-seconds: 7200
6159

6260
- name: Install nix
63-
uses: cachix/install-nix-action@v27
64-
with:
65-
install_url: https://releases.nixos.org/nix/nix-2.29.1/install
66-
extra_nix_config: |
67-
substituters = https://cache.nixos.org https://nix-postgres-artifacts.s3.amazonaws.com
68-
trusted-public-keys = nix-postgres-artifacts:dGZlQOvKcNEjvT7QEAJbcV6b6uk7VF/hWMjhYleiaLI= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=
61+
uses: ./.github/actions/nix-install-ephemeral
6962

7063
- name: Run checks if triggered manually
7164
if: ${{ github.event_name == 'workflow_dispatch' }}
@@ -76,47 +69,25 @@ jobs:
7669
exit 1
7770
fi
7871
79-
- name: Set PostgreSQL version environment variable
80-
run: |
81-
echo "POSTGRES_MAJOR_VERSION=${{ matrix.postgres_version }}" >> $GITHUB_ENV
82-
echo "EXECUTION_ID=${{ github.run_id }}-${{ matrix.postgres_version }}" >> $GITHUB_ENV
83-
84-
- name: Generate common-nix.vars.pkr.hcl
85-
run: |
86-
PG_VERSION=$(nix run nixpkgs#yq -- '.postgres_release["postgres'${{ matrix.postgres_version }}'"]' ansible/vars.yml)
87-
PG_VERSION=$(echo "$PG_VERSION" | tr -d '"') # Remove any surrounding quotes
88-
echo 'postgres-version = "'$PG_VERSION'"' > common-nix.vars.pkr.hcl
89-
# Ensure there's a newline at the end of the file
90-
echo "" >> common-nix.vars.pkr.hcl
91-
92-
- name: Build AMI stage 1
93-
env:
94-
POSTGRES_MAJOR_VERSION: ${{ env.POSTGRES_MAJOR_VERSION }}
95-
run: |
96-
GIT_SHA=${{github.sha}}
97-
nix run github:supabase/postgres/${GIT_SHA}#packer -- init amazon-arm64-nix.pkr.hcl
98-
# why is postgresql_major defined here instead of where the _three_ other postgresql_* variables are defined?
99-
nix run github:supabase/postgres/${GIT_SHA}#packer -- build -var "git-head-version=${GIT_SHA}" -var "packer-execution-id=${EXECUTION_ID}" -var-file="development-arm.vars.pkr.hcl" -var-file="common-nix.vars.pkr.hcl" -var "ansible_arguments=-e postgresql_major=${POSTGRES_MAJOR_VERSION}" -var "region=us-east-1" -var 'ami_regions=["us-east-1"]' amazon-arm64-nix.pkr.hcl
100-
101-
- name: Build AMI stage 2
102-
env:
103-
POSTGRES_MAJOR_VERSION: ${{ env.POSTGRES_MAJOR_VERSION }}
104-
run: |
105-
GIT_SHA=${{github.sha}}
106-
nix run github:supabase/postgres/${GIT_SHA}#packer -- init stage2-nix-psql.pkr.hcl
107-
POSTGRES_MAJOR_VERSION=${{ env.POSTGRES_MAJOR_VERSION }}
108-
nix run github:supabase/postgres/${GIT_SHA}#packer -- build -var "git_sha=${GIT_SHA}" -var "git-head-version=${GIT_SHA}" -var "packer-execution-id=${EXECUTION_ID}" -var "postgres_major_version=${POSTGRES_MAJOR_VERSION}" -var-file="development-arm.vars.pkr.hcl" -var-file="common-nix.vars.pkr.hcl" -var "region=us-east-1" -var 'ami_regions=["us-east-1"]' stage2-nix-psql.pkr.hcl
72+
- name: Build AMI
73+
id: build-ami
74+
uses: ./.github/actions/build-ami
75+
with:
76+
postgres_version: ${{ matrix.postgres_version }}
77+
region: us-east-1
78+
ami_regions: '["us-east-1"]'
79+
git_sha: ${{ github.sha }}
10980

11081
- name: Grab release version
11182
id: process_release_version
11283
run: |
113-
VERSION=$(cat common-nix.vars.pkr.hcl | sed -e 's/postgres-version = "\(.*\)"/\1/g')
114-
echo "version=$VERSION" >> $GITHUB_OUTPUT
84+
VERSION="${{ steps.build-ami.outputs.postgres_release_version }}"
85+
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
11586
11687
- name: Create nix flake revision tarball
11788
run: |
11889
GIT_SHA=${{github.sha}}
119-
MAJOR_VERSION=${{ env.POSTGRES_MAJOR_VERSION }}
90+
MAJOR_VERSION=${{ matrix.postgres_version }}
12091
12192
mkdir -p "/tmp/pg_upgrade_bin/${MAJOR_VERSION}"
12293
echo "$GIT_SHA" >> "/tmp/pg_upgrade_bin/${MAJOR_VERSION}/nix_flake_version"
@@ -134,7 +105,7 @@ jobs:
134105
ansible-playbook -i localhost \
135106
-e "ami_release_version=${{ steps.process_release_version.outputs.version }}" \
136107
-e "internal_artifacts_bucket=${{ secrets.ARTIFACTS_BUCKET }}" \
137-
-e "postgres_major_version=${{ env.POSTGRES_MAJOR_VERSION }}" \
108+
-e "postgres_major_version=${{ matrix.postgres_version }}" \
138109
manifest-playbook.yml
139110
140111
- name: Upload nix flake revision to s3 staging
@@ -155,7 +126,7 @@ jobs:
155126
ansible-playbook -i localhost \
156127
-e "ami_release_version=${{ steps.process_release_version.outputs.version }}" \
157128
-e "internal_artifacts_bucket=${{ secrets.PROD_ARTIFACTS_BUCKET }}" \
158-
-e "postgres_major_version=${{ env.POSTGRES_MAJOR_VERSION }}" \
129+
-e "postgres_major_version=${{ matrix.postgres_version }}" \
159130
manifest-playbook.yml
160131
161132
- name: Upload nix flake revision to s3 prod
@@ -184,9 +155,11 @@ jobs:
184155
- name: Cleanup resources after build
185156
if: ${{ always() }}
186157
run: |
158+
EXECUTION_ID="${{ steps.build-ami.outputs.execution_id }}"
187159
aws ec2 --region us-east-1 describe-instances --filters "Name=tag:packerExecutionId,Values=${EXECUTION_ID}" --query "Reservations[].Instances[].InstanceId" --output text | xargs -r aws ec2 terminate-instances --region us-east-1 --instance-ids
188160
189161
- name: Cleanup resources on build cancellation
190162
if: ${{ cancelled() }}
191163
run: |
164+
EXECUTION_ID="${{ steps.build-ami.outputs.execution_id }}"
192165
aws ec2 --region us-east-1 describe-instances --filters "Name=tag:packerExecutionId,Values=${EXECUTION_ID}" --query "Reservations[].Instances[].InstanceId" --output text | xargs -r aws ec2 terminate-instances --region us-east-1 --instance-ids

0 commit comments

Comments
 (0)