Skip to content

Commit e834b75

Browse files
committed
Cache PostgreSQL package downloads
PostgreSQL packages are downloaded from upstream repositories on every action invocation. These downloads account for a significant portion of the setup time and are redundant when the package version has not changed. They are not always reliable either: Chocolatey, for example, frequently fails to download the PostgreSQL installer. So we better cache these downloads to make subsequent runs faster and more reliable. Since package updates retain the same PostgreSQL major version, rotate cache keys weekly to allow updated packages to be cached for the same platform and PostgreSQL version.
1 parent 3dba498 commit e834b75

1 file changed

Lines changed: 32 additions & 21 deletions

File tree

action.yml

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ outputs:
4242
runs:
4343
using: composite
4444
steps:
45+
- name: Get current week number
46+
id: week
47+
run: |
48+
echo "value=$(date -u +%Y-%W)" >> "$GITHUB_OUTPUT"
49+
shell: bash
50+
51+
- name: Cache PostgreSQL packages
52+
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9
53+
with:
54+
path: |
55+
${{ runner.temp }}/packages/postgresql*.deb
56+
${{ runner.temp }}/packages/libpq*.deb
57+
${{ runner.temp }}/packages/postgresql-*.zip
58+
~/Library/Caches/Homebrew/downloads/*--postgresql@*
59+
key: postgresql-${{ runner.os }}-${{ runner.arch }}-${{ inputs.postgres-version }}-${{ steps.week.outputs.value }}
60+
4561
- name: Install PostgreSQL
4662
run: |
4763
if [[ ! "$INPUT_POSTGRES_VERSION" =~ ^(14|15|16|17|18)$ ]]; then
@@ -64,8 +80,11 @@ runs:
6480
echo "create_main_cluster = false" | sudo tee \
6581
/etc/postgresql-common/createcluster.d/50-action-setup-postgres.conf
6682
83+
mkdir -p "$RUNNER_TEMP/packages"
6784
sudo apt-get update
68-
sudo apt-get -y install postgresql-$INPUT_POSTGRES_VERSION
85+
sudo apt-get \
86+
-o "Dir::Cache::archives=$RUNNER_TEMP/packages" \
87+
-y install postgresql-$INPUT_POSTGRES_VERSION
6988
7089
PG_BINDIR=$("/usr/lib/postgresql/$INPUT_POSTGRES_VERSION/bin/pg_config" --bindir)
7190
echo "$PG_BINDIR" >> $GITHUB_PATH
@@ -81,28 +100,20 @@ runs:
81100
echo "$name=" >> $GITHUB_ENV
82101
done
83102
84-
# Chocolatey downloads the PostgreSQL installer from
85-
# get.enterprisedb.com, which intermittently responds with HTTP 403
86-
# and aborts the install. Retry a few times to absorb such transient
87-
# download failures.
88-
for attempt in 1 2 3; do
89-
if choco install postgresql$INPUT_POSTGRES_VERSION \
90-
--ia "--enable-components server,commandlinetools --extract-only 1" \
91-
--no-progress; then
92-
break
93-
fi
94-
95-
if [ "$attempt" -eq 3 ]; then
96-
echo "::error::Failed to install PostgreSQL after $attempt attempts."
97-
exit 1
98-
fi
103+
POSTGRES_VERSION=$(choco search postgresql$INPUT_POSTGRES_VERSION \
104+
--exact --limit-output | cut -d "|" -f 2 | cut -d "." -f 1,2)
105+
POSTGRES_ARCHIVE="$RUNNER_TEMP/packages/postgresql-$POSTGRES_VERSION-1-windows-x64-binaries.zip"
106+
POSTGRES_DIR="$RUNNER_TEMP/postgresql"
99107
100-
echo "::warning::choco install failed (attempt $attempt), retrying in 15s..."
101-
sleep 15
102-
done
108+
if [ ! -f "$POSTGRES_ARCHIVE" ]; then
109+
curl --fail --location --retry 3 --retry-all-errors \
110+
--output "$POSTGRES_ARCHIVE" \
111+
"https://get.enterprisedb.com/postgresql/$(basename "$POSTGRES_ARCHIVE")"
112+
fi
113+
unzip -q "$POSTGRES_ARCHIVE" -d "$POSTGRES_DIR"
103114
104-
PG_BINDIR=$("$PROGRAMFILES/PostgreSQL/$INPUT_POSTGRES_VERSION/bin/pg_config.exe" --bindir)
105-
PG_LIBDIR=$("$PROGRAMFILES/PostgreSQL/$INPUT_POSTGRES_VERSION/bin/pg_config.exe" --libdir)
115+
PG_BINDIR="$POSTGRES_DIR/pgsql/bin"
116+
PG_LIBDIR="$POSTGRES_DIR/pgsql/lib"
106117
107118
echo "$PG_BINDIR" >> $GITHUB_PATH
108119
echo "PQ_LIB_DIR=$PG_LIBDIR" >> $GITHUB_ENV

0 commit comments

Comments
 (0)