3131 name : docker-image
3232 path : image.tar
3333
34-
35- backup-unencrypted :
36- name : Backup Unencrypted & Show pg_dump
34+ backup-encrypted :
35+ name : Backup Encrypted
3736 runs-on : ubuntu-latest
3837 needs : [build]
3938 steps :
@@ -49,77 +48,83 @@ jobs:
4948 run : docker load -i image.tar
5049
5150 - name : Create Docker network
52- run : docker network create bica-net-unenc || true
53-
54- - name : Give execute permission to setup_postgres.sh
55- run : chmod +x ./scripts/setup_postgres.sh
56-
57- - name : Populate database with sample data
58- run : ./scripts/setup_postgres.sh bica-net-unenc postgres-db-unenc
59-
60- - name : Prepare backup folder
61- run : mkdir -p ./backups
62-
63- - name : Run unencrypted backup
64- run : |
65- docker run --rm --network bica-net-unenc \
66- --entrypoint /backup.sh \
67- -e DB_HOST=postgres-db-unenc \
68- -e DB_PORT=5432 \
69- -e DB_USER=myuser \
70- -e DB_PASSWORD=mypass \
71- -e DB_NAME=mydatabase \
72- -e BACKUP_DIR=/mnt/backups \
73- -e RETENTION_DAYS=7 \
74- -e ENCRYPT=false \
75- -v ${{ github.workspace }}/backups:/mnt/backups \
76- $IMAGE_NAME:$TAG
51+ run : docker network create bica-net || true
7752
78- - name : Show pg_dump contents from backup
53+ - name : Start PostgreSQL container
7954 run : |
80- ls -lh ./backups
81- tar -xzf ./backups/*.tar.gz -C ./backups
82- head -40 ./backups/db_backup.sql
55+ docker run -d --name postgres-db --network bica-net \
56+ -e POSTGRES_USER=myuser \
57+ -e POSTGRES_PASSWORD=mypass \
58+ -e POSTGRES_DB=mydatabase \
59+ postgres:15
8360
84- - name : Cleanup
61+ - name : Wait for Postgres to be ready
8562 run : |
86- docker rm -f postgres-db-unenc || true
87- docker network rm bica-net-unenc || true
88-
89-
90- backup-encrypted :
91- name : Backup Encrypted
92- runs-on : ubuntu-latest
93- needs : [build]
94- steps :
95- - uses : actions/checkout@v4
96-
97- - name : Download image artifact
98- uses : actions/download-artifact@v4
99- with :
100- name : docker-image
101- path : .
102-
103- - name : Load docker image
104- run : docker load -i image.tar
105-
106- - name : Create Docker network
107- run : docker network create bica-net-enc || true
108-
109- - name : Give execute permission to setup_postgres.sh
110- run : chmod +x ./scripts/setup_postgres.sh
63+ for i in {1..30}; do
64+ docker run --rm --network bica-net postgres:15 \
65+ bash -c "PGPASSWORD=mypass pg_isready -h postgres-db -p 5432 -U myuser" && echo "Postgres is ready" && exit 0
66+ echo "Waiting for Postgres... attempt $i"
67+ sleep 2
68+ done
69+ echo "Postgres did not become ready in time"
70+ exit 1
11171
11272 - name : Populate database with sample data
113- run : ./scripts/setup_postgres.sh bica-net-enc postgres-db-enc
73+ run : |
74+ docker run --rm --network bica-net \
75+ -e PGPASSWORD=mypass \
76+ postgres:15 \
77+ psql -h postgres-db -U myuser -d mydatabase -c "
78+ CREATE TABLE IF NOT EXISTS users (
79+ id SERIAL PRIMARY KEY,
80+ username TEXT NOT NULL UNIQUE,
81+ email TEXT NOT NULL UNIQUE,
82+ created_at TIMESTAMP DEFAULT NOW()
83+ );
84+
85+ CREATE TABLE IF NOT EXISTS posts (
86+ id SERIAL PRIMARY KEY,
87+ user_id INTEGER NOT NULL REFERENCES users(id),
88+ title TEXT NOT NULL,
89+ content TEXT,
90+ published_at TIMESTAMP
91+ );
92+
93+ CREATE TABLE IF NOT EXISTS comments (
94+ id SERIAL PRIMARY KEY,
95+ post_id INTEGER NOT NULL REFERENCES posts(id),
96+ author_name TEXT NOT NULL,
97+ comment TEXT NOT NULL,
98+ created_at TIMESTAMP DEFAULT NOW()
99+ );
100+
101+ INSERT INTO users (username, email) VALUES
102+ ('alice', 'alice@example.com'),
103+ ('bob', 'bob@example.com'),
104+ ('carol', 'carol@example.com')
105+ ON CONFLICT DO NOTHING;
106+
107+ INSERT INTO posts (user_id, title, content, published_at) VALUES
108+ (1, 'First post', 'This is the content of the first post.', NOW() - INTERVAL '5 days'),
109+ (1, 'Second post', 'More content here.', NOW() - INTERVAL '2 days'),
110+ (2, 'Bob''s post', 'Bob writes something interesting.', NOW() - INTERVAL '3 days')
111+ ON CONFLICT DO NOTHING;
112+
113+ INSERT INTO comments (post_id, author_name, comment) VALUES
114+ (1, 'Eve', 'Great post, thanks!'),
115+ (1, 'Mallory', 'I disagree with your point.'),
116+ (3, 'Trent', 'Nice one, Bob!')
117+ ON CONFLICT DO NOTHING;
118+ "
114119
115120 - name : Prepare backup folder
116121 run : mkdir -p ./backups
117122
118123 - name : Run encrypted backup
119124 run : |
120- docker run --rm --network bica-net-enc \
125+ docker run --rm --network bica-net \
121126 --entrypoint /backup.sh \
122- -e DB_HOST=postgres-db-enc \
127+ -e DB_HOST=postgres-db \
123128 -e DB_PORT=5432 \
124129 -e DB_USER=myuser \
125130 -e DB_PASSWORD=mypass \
@@ -142,9 +147,8 @@ jobs:
142147
143148 - name : Cleanup
144149 run : |
145- docker rm -f postgres-db-enc || true
146- docker network rm bica-net-enc || true
147-
150+ docker rm -f postgres-db || true
151+ docker network rm bica-net || true
148152
149153 decrypt-and-show :
150154 name : Decrypt backup and show pg_dump
@@ -172,11 +176,10 @@ jobs:
172176 tar -xzf ./backups/*.tar.gz -C ./backups
173177 head -40 ./backups/db_backup.sql
174178
175-
176179 docker-publish :
177180 name : Push to Docker Hub
178181 runs-on : ubuntu-latest
179- needs : [decrypt-and-show, backup-unencrypted ]
182+ needs : [decrypt-and-show]
180183 if : github.ref == 'refs/heads/main' && github.event_name == 'push'
181184 steps :
182185 - uses : actions/checkout@v4
0 commit comments