Skip to content

Commit b4d8dfc

Browse files
committed
feat: add branch-based versioning for PR AMI builds (#1902)
* feat: add branch-based versioning for PR AMI builds Manually create unique Postgres version names in branch often leads to version conflicts with the base branch versions. These conflicts force developers to deal with manual conflict resolution and unnecessary rebuilds. To address this, this change implement automatic branch-based versioning for AMI builds triggered via workflow_dispatch on non-develop and non-release branches. The branch name is sanitized and appended to the Postgres version string. Example: Branch 'multi-version-ext/pg-partman' produces postgres version 'multi-version-ext-pg-partman' * feat: add notice message for published AMI version Display the published postgres AMI version using GitHub Actions `::notice` annotation. * feat: run actionlint on GitHub Actions workflows Starting to lint GitHub Actions workflows with actionlint. * fix: generate a unique AMI version GitHub run_id is appended to the version suffix to ensure uniqueness. It also enables to track the AMI back to the specific workflow run that created it using url like: https://github.com/supabase/postgres/actions/runs/<run_id>
1 parent 578be5d commit b4d8dfc

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

.github/actionlint.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
self-hosted-runner:
2+
labels:
3+
- blacksmith-2vcpu-ubuntu-2404-arm
4+
- blacksmith-4vcpu-ubuntu-2404

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
id: set-versions
3737
run: |
3838
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
39+
echo "postgres_versions=$VERSIONS" >> "$GITHUB_OUTPUT"
4040
4141
build:
4242
needs: prepare
@@ -67,25 +67,21 @@ jobs:
6767
substituters = https://cache.nixos.org https://nix-postgres-artifacts.s3.amazonaws.com
6868
trusted-public-keys = nix-postgres-artifacts:dGZlQOvKcNEjvT7QEAJbcV6b6uk7VF/hWMjhYleiaLI= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=
6969
70-
- name: Run checks if triggered manually
71-
if: ${{ github.event_name == 'workflow_dispatch' }}
72-
run: |
73-
SUFFIX=$(nix run nixpkgs#yq -- ".postgres_release[\"postgres${{ matrix.postgres_version }}\"]" ansible/vars.yml | sed -E 's/[0-9\.]+(.*)$/\1/')
74-
if [[ -z "$SUFFIX" ]] ; then
75-
echo "Version must include non-numeric characters if built manually."
76-
exit 1
77-
fi
78-
7970
- name: Set PostgreSQL version environment variable
8071
run: |
81-
echo "POSTGRES_MAJOR_VERSION=${{ matrix.postgres_version }}" >> $GITHUB_ENV
82-
echo "EXECUTION_ID=${{ github.run_id }}-${{ matrix.postgres_version }}" >> $GITHUB_ENV
72+
echo "POSTGRES_MAJOR_VERSION=${{ matrix.postgres_version }}" >> "$GITHUB_ENV"
73+
echo "EXECUTION_ID=${{ github.run_id }}-${{ matrix.postgres_version }}" >> "$GITHUB_ENV"
8374
8475
- name: Generate common-nix.vars.pkr.hcl
8576
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
77+
PG_VERSION="$(nix run nixpkgs#yq -- -r '.postgres_release["postgres'${{ matrix.postgres_version }}'"]' ansible/vars.yml)"
78+
BRANCH_NAME="$(echo "${{ github.ref }}" | sed 's|refs/heads/||')"
79+
if [[ "${{ github.event_name }}" == "workflow_dispatch" && "$BRANCH_NAME" != "develop" && "$BRANCH_NAME" != release/* ]]; then
80+
SUFFIX="${BRANCH_NAME//[^a-zA-Z0-9._-]/-}-${{ github.run_id }}"
81+
PG_VERSION="${PG_VERSION}-${SUFFIX}"
82+
echo "Added branch suffix to version: $SUFFIX"
83+
fi
84+
echo "postgres-version = \"$PG_VERSION\"" > common-nix.vars.pkr.hcl
8985
# Ensure there's a newline at the end of the file
9086
echo "" >> common-nix.vars.pkr.hcl
9187
@@ -110,8 +106,9 @@ jobs:
110106
- name: Grab release version
111107
id: process_release_version
112108
run: |
113-
VERSION=$(cat common-nix.vars.pkr.hcl | sed -e 's/postgres-version = "\(.*\)"/\1/g')
114-
echo "version=$VERSION" >> $GITHUB_OUTPUT
109+
VERSION=$(sed -e 's/postgres-version = "\(.*\)"/\1/g' common-nix.vars.pkr.hcl)
110+
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
111+
echo "::notice title=AMI Published::Postgres AMI version: $VERSION"
115112
116113
- name: Create nix flake revision tarball
117114
run: |

nix/hooks.nix

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{ inputs, ... }:
2+
let
3+
ghWorkflows = builtins.attrNames (builtins.readDir ../.github/workflows);
4+
lintedWorkflows = [ "ami-release-nix.yml" ];
5+
in
26
{
37
imports = [ inputs.git-hooks.flakeModule ];
48
perSystem =
@@ -8,9 +12,17 @@
812
check.enable = true;
913
settings = {
1014
hooks = {
15+
actionlint = {
16+
enable = true;
17+
excludes = builtins.filter (name: !builtins.elem name lintedWorkflows) ghWorkflows;
18+
verbose = true;
19+
};
20+
1121
treefmt = {
1222
enable = true;
1323
package = config.treefmt.build.wrapper;
24+
pass_filenames = false;
25+
verbose = true;
1426
};
1527
};
1628
};

0 commit comments

Comments
 (0)