-
Notifications
You must be signed in to change notification settings - Fork 0
97 lines (83 loc) · 3.21 KB
/
docker-image-test.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
name: Test Backup
on:
push:
branches:
- main # Change this to your default branch if needed
pull_request:
branches:
- main # Change this to your default branch if needed
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:14
env:
POSTGRES_USER: your_db_user
POSTGRES_PASSWORD: your_db_password
POSTGRES_DB: your_database_name
ports:
- 5432:5432
options: >-
--health-cmd "pg_isready -U your_db_user"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Build Docker Image
run: docker build -t pg_backup_image .
- name: Create Sample Database and Insert Data
run: |
echo "Creating sample database and inserting data..."
until pg_isready -h localhost -U your_db_user; do
echo "Waiting for PostgreSQL to be ready..."
sleep 5
done
# Set PGPASSWORD to allow the psql command to connect
export PGPASSWORD=your_db_password
psql -h localhost -U your_db_user -d your_database_name -c "CREATE TABLE test_table (id SERIAL PRIMARY KEY, name VARCHAR(50));"
psql -h localhost -U your_db_user -d your_database_name -c "INSERT INTO test_table (name) VALUES ('Test Data 1'), ('Test Data 2');"
- name: Set CRONTIME
run: echo "CRONTIME='* * * * *'" >> $GITHUB_ENV
- name: Make backup mount point
run: mkdir ~/backup
- name: Start Cron Job
run: |
echo "Setting up cron job..."
docker run -d \
--name pg_backup_container \
-e PGUSER=your_db_user \
-e PGDATABASE=your_database_name \
-e PGPASSWORD=your_db_password \
-e PGHOST=localhost \
-e CRONTIME="${CRONTIME}" \
-v ~/backup:/backup \
pg_backup_image
echo "Waiting 2 minutes for cron job to run..."
sleep 90 # Wait for 90 seconds to allow the cron job to execute
# see if there is anything in the backup directory
- name: Check for Backups
run: |
ls ~/backup
- name: Verify Backup
run: |
echo "Verifying backup..."
# Check if the backup file exists (old ref docker run --rm pg_backup_image)
BACKUP_FILE=$(ls ~/backup | head -n 1)
echo "Found backup file: $BACKUP_FILE" # Log the found file name
if [ -z "$BACKUP_FILE" ]; then
echo "No backup file found!"
exit 1
fi
echo "Verifying contents of $BACKUP_FILE..."
# Restore the backup and verify
docker run --rm \
-e PGUSER=your_db_user \
-e PGPASSWORD=your_db_password \
-e PGDATABASE=your_database_name \
-e PGHOST=localhost \
pg_backup_image sh -c "pg_restore -d your_database_name -U your_db_user /backup/$BACKUP_FILE && psql -h localhost -U your_db_user -d your_database_name -c 'SELECT * FROM test_table;'"