@@ -57,69 +57,70 @@ jobs:
5757
5858 - name : Wait for Postgres to be ready
5959 run : |
60- for i in {1..30}; do
61- docker run --rm --network bica-net ${{ env.POSTGRES_IMAGE }} \
62- bash -c "PGPASSWORD=$DB_PASSWORD pg_isready -h postgres-db -p 5432 -U $DB_USER" && exit 0
63- echo "Waiting for Postgres..."
60+ for i in $(seq 1 30); do
61+ docker exec postgres-db pg_isready -U $DB_USER -d $DB_NAME && exit 0
62+ echo "Waiting for Postgres... attempt $i"
6463 sleep 2
6564 done
66- echo "Postgres did not become ready in time"
65+ echo "Postgres did not become ready in time, showing logs:"
66+ docker logs postgres-db
6767 exit 1
6868
6969 - name : Populate database with sample data
7070 run : |
71- docker run --rm --network bica-net \
72- -e PGPASSWORD=$DB_PASSWORD \
73- ${{ env.POSTGRES_IMAGE }} \
74- psql -h postgres-db -U $DB_USER -d $DB_NAME -c "
75- CREATE TABLE IF NOT EXISTS users (
76- id SERIAL PRIMARY KEY,
77- username TEXT NOT NULL UNIQUE,
78- email TEXT NOT NULL UNIQUE,
79- created_at TIMESTAMP DEFAULT NOW()
80- );
81-
82- CREATE TABLE IF NOT EXISTS posts (
83- id SERIAL PRIMARY KEY,
84- user_id INTEGER NOT NULL REFERENCES users(id),
85- title TEXT NOT NULL,
86- content TEXT,
87- published_at TIMESTAMP
88- );
89-
90- CREATE TABLE IF NOT EXISTS comments (
91- id SERIAL PRIMARY KEY,
92- post_id INTEGER NOT NULL REFERENCES posts(id),
93- author_name TEXT NOT NULL,
94- comment TEXT NOT NULL,
95- created_at TIMESTAMP DEFAULT NOW()
96- );
97-
98- INSERT INTO users (username, email) VALUES
99- ('alice', 'alice@example.com'),
100- ('bob', 'bob@example.com'),
101- ('carol', 'carol@example.com')
102- ON CONFLICT DO NOTHING;
103-
104- INSERT INTO posts (user_id, title, content, published_at) VALUES
105- (1, 'First post', 'This is the content of the first post.', NOW() - INTERVAL '5 days'),
106- (1, 'Second post', 'More content here.', NOW() - INTERVAL '2 days'),
107- (2, 'Bob''s post', 'Bob writes something interesting.', NOW() - INTERVAL '3 days')
108- ON CONFLICT DO NOTHING;
109-
110- INSERT INTO comments (post_id, author_name, comment) VALUES
111- (1, 'Eve', 'Great post, thanks!'),
112- (1, 'Mallory', 'I disagree with your point.'),
113- (3, 'Trent', 'Nice one, Bob!')
114- ON CONFLICT DO NOTHING;
115- "
71+ docker exec -i postgres-db psql -U $DB_USER -d $DB_NAME <<EOF
72+ CREATE TABLE IF NOT EXISTS users (
73+ id SERIAL PRIMARY KEY,
74+ username TEXT NOT NULL UNIQUE,
75+ email TEXT NOT NULL UNIQUE,
76+ created_at TIMESTAMP DEFAULT NOW()
77+ );
78+
79+ CREATE TABLE IF NOT EXISTS posts (
80+ id SERIAL PRIMARY KEY,
81+ user_id INTEGER NOT NULL REFERENCES users(id),
82+ title TEXT NOT NULL,
83+ content TEXT,
84+ published_at TIMESTAMP
85+ );
86+
87+ CREATE TABLE IF NOT EXISTS comments (
88+ id SERIAL PRIMARY KEY,
89+ post_id INTEGER NOT NULL REFERENCES posts(id),
90+ author_name TEXT NOT NULL,
91+ comment TEXT NOT NULL,
92+ created_at TIMESTAMP DEFAULT NOW()
93+ );
94+
95+ INSERT INTO users (username, email) VALUES
96+ ('alice', 'alice@example.com'),
97+ ('bob', 'bob@example.com'),
98+ ('carol', 'carol@example.com')
99+ ON CONFLICT DO NOTHING;
100+
101+ INSERT INTO posts (user_id, title, content, published_at) VALUES
102+ (1, 'First post', 'This is the content of the first post.', NOW() - INTERVAL '5 days'),
103+ (1, 'Second post', 'More content here.', NOW() - INTERVAL '2 days'),
104+ (2, 'Bob''s post', 'Bob writes something interesting.', NOW() - INTERVAL '3 days')
105+ ON CONFLICT DO NOTHING;
106+
107+ INSERT INTO comments (post_id, author_name, comment) VALUES
108+ (1, 'Eve', 'Great post, thanks!'),
109+ (1, 'Mallory', 'I disagree with your point.'),
110+ (3, 'Trent', 'Nice one, Bob!')
111+ ON CONFLICT DO NOTHING;
112+ EOF
116113
117114 backup-unencrypted :
118115 name : Backup Unencrypted & Show pg_dump
119116 runs-on : ubuntu-latest
120117 needs : [build, setup-postgres]
121118 env :
119+ DB_USER : ${{ env.DB_USER }}
122120 DB_PASSWORD : ${{ secrets.DB_PASSWORD }}
121+ DB_NAME : ${{ env.DB_NAME }}
122+ BACKUP_DIR : ${{ env.BACKUP_DIR }}
123+ RETENTION_DAYS : ${{ env.RETENTION_DAYS }}
123124 steps :
124125 - uses : actions/checkout@v4
125126
@@ -150,13 +151,13 @@ jobs:
150151 -v ${{ github.workspace }}/backups:$BACKUP_DIR \
151152 $IMAGE_NAME:$TAG
152153
153- - name : Show pg_dump contents from backup
154+ - name : Show pg_dump first 40 lines
154155 run : |
155156 ls -lh ./backups
156157 tar -xzf ./backups/*.tar.gz -C ./backups
157- cat ./backups/db_backup.sql
158+ head -40 ./backups/db_backup.sql
158159
159- - name : Cleanup
160+ - name : Cleanup Docker containers and network
160161 run : |
161162 docker rm -f postgres-db || true
162163 docker network rm bica-net || true
@@ -166,7 +167,11 @@ jobs:
166167 runs-on : ubuntu-latest
167168 needs : [build, setup-postgres]
168169 env :
170+ DB_USER : ${{ env.DB_USER }}
169171 DB_PASSWORD : ${{ secrets.DB_PASSWORD }}
172+ DB_NAME : ${{ env.DB_NAME }}
173+ BACKUP_DIR : ${{ env.BACKUP_DIR }}
174+ RETENTION_DAYS : ${{ env.RETENTION_DAYS }}
170175 ENCRYPT_PASS : ${{ secrets.ENCRYPT_PASS }}
171176 steps :
172177 - uses : actions/checkout@v4
@@ -208,7 +213,7 @@ jobs:
208213 name : encrypted-backup
209214 path : ./backups/*.enc
210215
211- - name : Cleanup
216+ - name : Cleanup Docker containers and network
212217 run : |
213218 docker rm -f postgres-db || true
214219 docker network rm bica-net || true
@@ -235,16 +240,19 @@ jobs:
235240 openssl enc -aes-256-cbc -d -pbkdf2 -salt -in "$f" -out "${f%.enc}.tar.gz" -k "$ENCRYPT_PASS"
236241 done
237242
238- - name : Extract decrypted tarball and show pg_dump
243+ - name : Extract decrypted tarball and show pg_dump
239244 run : |
240245 tar -xzf ./backups/*.tar.gz -C ./backups
241- cat ./backups/db_backup.sql
246+ head -40 ./backups/db_backup.sql
242247
243248 docker-publish :
244249 name : Push to Docker Hub
245250 runs-on : ubuntu-latest
246251 needs : [decrypt-and-show, backup-unencrypted]
247252 if : github.ref == 'refs/heads/main' && github.event_name == 'push'
253+ env :
254+ IMAGE_NAME : ${{ env.IMAGE_NAME }}
255+ TAG : ${{ env.TAG }}
248256 steps :
249257 - uses : actions/checkout@v4
250258
0 commit comments