mirror of
https://github.com/tj-actions/pg-dump.git
synced 2024-12-20 01:18:49 +00:00
Merge 91010d584c
into d9e5a3fe4f
This commit is contained in:
commit
f3a48b76a6
6 changed files with 222 additions and 3 deletions
65
.github/workflows/test.yml
vendored
65
.github/workflows/test.yml
vendored
|
@ -9,9 +9,72 @@ on:
|
||||||
- main
|
- main
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
shellcheck:
|
||||||
|
name: Run shellcheck
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4
|
||||||
|
- name: shellcheck
|
||||||
|
uses: reviewdog/action-shellcheck@v1.19
|
||||||
|
test-postgresql:
|
||||||
|
name: Test pg_dump with PostgreSQL version
|
||||||
|
runs-on: ${{ matrix.platform }}
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
platform: [ubuntu-latest, windows-latest, macos-latest, macos-11, windows-2022]
|
||||||
|
postgresql_version: [12, 14, 15]
|
||||||
|
services:
|
||||||
|
postgres:
|
||||||
|
image: postgres:${{ matrix.postgresql_version }}
|
||||||
|
env:
|
||||||
|
POSTGRES_USER: test_user
|
||||||
|
POSTGRES_PASSWORD: test_user_password
|
||||||
|
POSTGRES_DB: testdb
|
||||||
|
ports:
|
||||||
|
- 5432:5432
|
||||||
|
# needed because the postgres container does not provide a healthcheck
|
||||||
|
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal token
|
||||||
|
- name: Run pg_dump
|
||||||
|
uses: ./
|
||||||
|
with:
|
||||||
|
database_url: "postgres://test_user:test_user_password@localhost:5432/testdb"
|
||||||
|
postgresql_version: ${{ matrix.postgresql_version }}
|
||||||
|
path: "backups/${{ matrix.postgresql_version }}/backup.sql"
|
||||||
|
- name: Check changes to the backup file.
|
||||||
|
id: changed_backup
|
||||||
|
if: matrix.platform == 'ubuntu-latest'
|
||||||
|
uses: tj-actions/verify-changed-files@v16
|
||||||
|
with:
|
||||||
|
files: backups/${{ matrix.postgresql_version }}/backup.sql
|
||||||
|
|
||||||
|
- name: Commit changes to backup file.
|
||||||
|
if: steps.changed_backup.outputs.files_changed == 'true' && matrix.platform == 'ubuntu-latest'
|
||||||
|
run: |
|
||||||
|
git config --local user.email "github-actions[bot]@users.noreply.github.com"
|
||||||
|
git config --local user.name "github-actions[bot]"
|
||||||
|
git add backups/${{ matrix.postgresql_version }}/backup.sql
|
||||||
|
git commit -m "Auto updated backup.sql."
|
||||||
|
|
||||||
|
- name: Push changes
|
||||||
|
if: steps.changed_backup.outputs.files_changed == 'true' && matrix.platform == 'ubuntu-latest'
|
||||||
|
uses: ad-m/github-push-action@master
|
||||||
|
with:
|
||||||
|
github_token: ${{ secrets.PAT_TOKEN }}
|
||||||
|
branch: ${{ github.head_ref }}
|
||||||
|
|
||||||
test:
|
test:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
name: Test postgres-restore
|
name: Test pg_dump
|
||||||
services:
|
services:
|
||||||
postgres:
|
postgres:
|
||||||
image: postgres:9.6.24
|
image: postgres:9.6.24
|
||||||
|
|
13
action.yml
13
action.yml
|
@ -2,6 +2,9 @@ name: pg-dump
|
||||||
description: Run pg_dump to generate a backup
|
description: Run pg_dump to generate a backup
|
||||||
author: tj-actions
|
author: tj-actions
|
||||||
inputs:
|
inputs:
|
||||||
|
postgresql_version:
|
||||||
|
description: 'Version of PostgreSQL. e.g 15'
|
||||||
|
required: false
|
||||||
database_url:
|
database_url:
|
||||||
description: 'Database URL'
|
description: 'Database URL'
|
||||||
required: true
|
required: true
|
||||||
|
@ -17,9 +20,15 @@ runs:
|
||||||
using: 'composite'
|
using: 'composite'
|
||||||
steps:
|
steps:
|
||||||
- run: |
|
- run: |
|
||||||
mkdir -p $(dirname "${{ inputs.path }}")
|
bash $GITHUB_ACTION_PATH/entrypoint.sh
|
||||||
pg_dump ${{ inputs.options }} -d "${{ inputs.database_url }}" > "${{ inputs.path }}"
|
|
||||||
shell: bash
|
shell: bash
|
||||||
|
env:
|
||||||
|
# INPUT_<VARIABLE_NAME> is not available in Composite run steps
|
||||||
|
# https://github.community/t/input-variable-name-is-not-available-in-composite-run-steps/127611
|
||||||
|
INPUT_POSTGRESQL_VERSION: ${{ inputs.postgresql_version }}
|
||||||
|
INPUT_PATH: ${{ inputs.path }}
|
||||||
|
INPUT_DATABASE_URL: ${{ inputs.database_url }}
|
||||||
|
INPUT_OPTIONS: ${{ inputs.options }}
|
||||||
branding:
|
branding:
|
||||||
icon: hard-drive
|
icon: hard-drive
|
||||||
color: white
|
color: white
|
||||||
|
|
22
backups/12/backup.sql
Normal file
22
backups/12/backup.sql
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
--
|
||||||
|
-- PostgreSQL database dump
|
||||||
|
--
|
||||||
|
|
||||||
|
-- Dumped from database version 12.16 (Debian 12.16-1.pgdg120+1)
|
||||||
|
-- Dumped by pg_dump version 14.9 (Ubuntu 14.9-1.pgdg22.04+1)
|
||||||
|
|
||||||
|
SET statement_timeout = 0;
|
||||||
|
SET lock_timeout = 0;
|
||||||
|
SET idle_in_transaction_session_timeout = 0;
|
||||||
|
SET client_encoding = 'UTF8';
|
||||||
|
SET standard_conforming_strings = on;
|
||||||
|
SELECT pg_catalog.set_config('search_path', '', false);
|
||||||
|
SET check_function_bodies = false;
|
||||||
|
SET xmloption = content;
|
||||||
|
SET client_min_messages = warning;
|
||||||
|
SET row_security = off;
|
||||||
|
|
||||||
|
--
|
||||||
|
-- PostgreSQL database dump complete
|
||||||
|
--
|
||||||
|
|
22
backups/14/backup.sql
Normal file
22
backups/14/backup.sql
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
--
|
||||||
|
-- PostgreSQL database dump
|
||||||
|
--
|
||||||
|
|
||||||
|
-- Dumped from database version 14.9 (Debian 14.9-1.pgdg120+1)
|
||||||
|
-- Dumped by pg_dump version 14.9 (Ubuntu 14.9-1.pgdg22.04+1)
|
||||||
|
|
||||||
|
SET statement_timeout = 0;
|
||||||
|
SET lock_timeout = 0;
|
||||||
|
SET idle_in_transaction_session_timeout = 0;
|
||||||
|
SET client_encoding = 'UTF8';
|
||||||
|
SET standard_conforming_strings = on;
|
||||||
|
SELECT pg_catalog.set_config('search_path', '', false);
|
||||||
|
SET check_function_bodies = false;
|
||||||
|
SET xmloption = content;
|
||||||
|
SET client_min_messages = warning;
|
||||||
|
SET row_security = off;
|
||||||
|
|
||||||
|
--
|
||||||
|
-- PostgreSQL database dump complete
|
||||||
|
--
|
||||||
|
|
22
backups/15/backup.sql
Normal file
22
backups/15/backup.sql
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
--
|
||||||
|
-- PostgreSQL database dump
|
||||||
|
--
|
||||||
|
|
||||||
|
-- Dumped from database version 15.4 (Debian 15.4-1.pgdg120+1)
|
||||||
|
-- Dumped by pg_dump version 15.4 (Ubuntu 15.4-1.pgdg22.04+1)
|
||||||
|
|
||||||
|
SET statement_timeout = 0;
|
||||||
|
SET lock_timeout = 0;
|
||||||
|
SET idle_in_transaction_session_timeout = 0;
|
||||||
|
SET client_encoding = 'UTF8';
|
||||||
|
SET standard_conforming_strings = on;
|
||||||
|
SELECT pg_catalog.set_config('search_path', '', false);
|
||||||
|
SET check_function_bodies = false;
|
||||||
|
SET xmloption = content;
|
||||||
|
SET client_min_messages = warning;
|
||||||
|
SET row_security = off;
|
||||||
|
|
||||||
|
--
|
||||||
|
-- PostgreSQL database dump complete
|
||||||
|
--
|
||||||
|
|
81
entrypoint.sh
Executable file
81
entrypoint.sh
Executable file
|
@ -0,0 +1,81 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
echo "::group::pg-dump"
|
||||||
|
|
||||||
|
echo "Checking if the output directory exists..."
|
||||||
|
|
||||||
|
if [ ! -d "$(dirname "$INPUT_PATH")" ]; then
|
||||||
|
echo "The output directory does not exist. Creating it..."
|
||||||
|
mkdir -p "$(dirname "$INPUT_PATH")"
|
||||||
|
echo "Created the output directory"
|
||||||
|
else
|
||||||
|
echo "The output directory already exists"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -n "$INPUT_POSTGRESQL_VERSION" ]]; then
|
||||||
|
echo "Verifying version"
|
||||||
|
|
||||||
|
# Check if the input is an integer
|
||||||
|
if ! [[ "$INPUT_POSTGRESQL_VERSION" =~ ^[0-9]+$ ]]; then
|
||||||
|
echo "Error: $INPUT_POSTGRESQL_VERSION is not a valid integer."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check if the input is between 10 and 15 (inclusive)
|
||||||
|
if (( INPUT_POSTGRESQL_VERSION < 10 || INPUT_POSTGRESQL_VERSION > 15 )); then
|
||||||
|
echo "Error: $INPUT_POSTGRESQL_VERSION is not between 10 and 15 (inclusive)."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Validated postgresql version: $INPUT_POSTGRESQL_VERSION"
|
||||||
|
|
||||||
|
echo "Installing postgresql..."
|
||||||
|
|
||||||
|
if [[ "$(uname -s)" == "Linux" ]]; then
|
||||||
|
# Create the file repository configuration:
|
||||||
|
sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
|
||||||
|
|
||||||
|
# Import the repository signing key:
|
||||||
|
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
|
||||||
|
|
||||||
|
# Update the package lists:
|
||||||
|
sudo apt-get update
|
||||||
|
|
||||||
|
# Install PostgreSQL
|
||||||
|
sudo apt-get install -y "postgresql-$INPUT_POSTGRESQL_VERSION"
|
||||||
|
elif [[ "$(uname -s)" == "NT"* ]]; then
|
||||||
|
choco install postgresql --version="$INPUT_POSTGRESQL_VERSION" -y
|
||||||
|
elif [[ "$(uname -s)" == "Darwin" ]]; then
|
||||||
|
brew update
|
||||||
|
brew install "postgresql@$INPUT_POSTGRESQL_VERSION"
|
||||||
|
else
|
||||||
|
echo "Unsupported OS"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Installed postgresql"
|
||||||
|
|
||||||
|
echo "Running pg_dump..."
|
||||||
|
|
||||||
|
# Verify installation by running pg_dump directly
|
||||||
|
if [[ "$(uname -s)" == "NT"* ]]; then
|
||||||
|
"/Program Files/PostgreSQL/$INPUT_POSTGRESQL_VERSION/bin/pg_dump" --version
|
||||||
|
# shellcheck disable=SC2086
|
||||||
|
"/Program Files/PostgreSQL/$INPUT_POSTGRESQL_VERSION/bin/pg_dump" $INPUT_OPTIONS -d "$INPUT_DATABASE_URL" > "$INPUT_PATH"
|
||||||
|
else
|
||||||
|
"/usr/lib/postgresql/$INPUT_POSTGRESQL_VERSION/bin/pg_dump" --version
|
||||||
|
# shellcheck disable=SC2086
|
||||||
|
"/usr/lib/postgresql/$INPUT_POSTGRESQL_VERSION/bin/pg_dump" $INPUT_OPTIONS -d "$INPUT_DATABASE_URL" > "$INPUT_PATH"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "Running pg_dump..."
|
||||||
|
|
||||||
|
# shellcheck disable=SC2086
|
||||||
|
pg_dump $INPUT_OPTIONS -d "$INPUT_DATABASE_URL" > "$INPUT_PATH"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Complete"
|
||||||
|
|
||||||
|
echo "::endgroup::"
|
Loading…
Add table
Reference in a new issue