1111 TAG : test
1212
1313jobs :
14- backup-job :
15- name : Build, Setup Postgres & Backup (Unencrypted + Encrypted)
14+ build :
15+ name : Build Docker Image
1616 runs-on : ubuntu-latest
1717 steps :
1818 - uses : actions/checkout@v4
1919
2020 - name : Setup Docker Buildx
2121 uses : docker/setup-buildx-action@v3
2222
23- - name : Build backup Docker image
23+ - name : Build image
2424 run : docker build -t $IMAGE_NAME:$TAG .
2525
26+ - name : Save image as artifact
27+ run : docker save $IMAGE_NAME:$TAG -o image.tar
28+
29+ - uses : actions/upload-artifact@v4
30+ with :
31+ name : docker-image
32+ path : image.tar
33+
34+ setup-postgres :
35+ name : Setup PostgreSQL with Sample Data
36+ runs-on : ubuntu-latest
37+ needs : build
38+ steps :
39+ - name : Download image artifact
40+ uses : actions/download-artifact@v4
41+ with :
42+ name : docker-image
43+ path : .
44+
45+ - name : Load docker image
46+ run : docker load -i image.tar
47+
2648 - name : Create Docker network
2749 run : docker network create bica-net || true
2850
@@ -34,16 +56,15 @@ jobs:
3456 -e POSTGRES_DB=mydatabase \
3557 postgres:15
3658
37- - name : Wait for Postgres to be ready (container-based)
59+ - name : Wait for Postgres to be ready
3860 run : |
3961 for i in {1..30}; do
4062 docker run --rm --network bica-net postgres:15 \
4163 bash -c "PGPASSWORD=mypass pg_isready -h postgres-db -p 5432 -U myuser" && echo "Postgres is ready" && exit 0
42- echo "Waiting for Postgres..."
64+ echo "Waiting for Postgres... attempt $i "
4365 sleep 2
4466 done
4567 echo "Postgres did not become ready in time"
46- docker logs postgres-db
4768 exit 1
4869
4970 - name : Populate database with sample data
@@ -52,43 +73,28 @@ jobs:
5273 -e PGPASSWORD=mypass \
5374 postgres:15 \
5475 psql -h postgres-db -U myuser -d mydatabase -c "
55- CREATE TABLE IF NOT EXISTS users (
56- id SERIAL PRIMARY KEY,
57- username TEXT NOT NULL UNIQUE,
58- email TEXT NOT NULL UNIQUE,
59- created_at TIMESTAMP DEFAULT NOW()
60- );
61- CREATE TABLE IF NOT EXISTS posts (
62- id SERIAL PRIMARY KEY,
63- user_id INTEGER NOT NULL REFERENCES users(id),
64- title TEXT NOT NULL,
65- content TEXT,
66- published_at TIMESTAMP
67- );
68- CREATE TABLE IF NOT EXISTS comments (
69- id SERIAL PRIMARY KEY,
70- post_id INTEGER NOT NULL REFERENCES posts(id),
71- author_name TEXT NOT NULL,
72- comment TEXT NOT NULL,
73- created_at TIMESTAMP DEFAULT NOW()
74- );
75- INSERT INTO users (username, email) VALUES
76- ('alice', 'alice@example.com'),
77- ('bob', 'bob@example.com'),
78- ('carol', 'carol@example.com')
79- ON CONFLICT DO NOTHING;
80- INSERT INTO posts (user_id, title, content, published_at) VALUES
81- (1, 'First post', 'This is the content of the first post.', NOW() - INTERVAL '5 days'),
82- (1, 'Second post', 'More content here.', NOW() - INTERVAL '2 days'),
83- (2, 'Bob''s post', 'Bob writes something interesting.', NOW() - INTERVAL '3 days')
84- ON CONFLICT DO NOTHING;
85- INSERT INTO comments (post_id, author_name, comment) VALUES
86- (1, 'Eve', 'Great post, thanks!'),
87- (1, 'Mallory', 'I disagree with your point.'),
88- (3, 'Trent', 'Nice one, Bob!')
89- ON CONFLICT DO NOTHING;
76+ -- (sua SQL aqui)
9077 "
9178
79+ backup-unencrypted :
80+ name : Backup Unencrypted & Show pg_dump
81+ runs-on : ubuntu-latest
82+ needs : [build, setup-postgres]
83+ steps :
84+ - uses : actions/checkout@v4
85+
86+ - name : Download image artifact
87+ uses : actions/download-artifact@v4
88+ with :
89+ name : docker-image
90+ path : .
91+
92+ - name : Load docker image
93+ run : docker load -i image.tar
94+
95+ - name : Create Docker network
96+ run : docker network create bica-net || true
97+
9298 - name : Prepare backup folder
9399 run : mkdir -p ./backups
94100
@@ -107,12 +113,39 @@ jobs:
107113 -v ${{ github.workspace }}/backups:/mnt/backups \
108114 $IMAGE_NAME:$TAG
109115
110- - name : Show pg_dump contents from backup (unencrypted)
116+ - name : Show pg_dump contents from backup
111117 run : |
112118 ls -lh ./backups
113119 tar -xzf ./backups/*.tar.gz -C ./backups
114120 head -40 ./backups/db_backup.sql
115121
122+ - name : Cleanup
123+ run : |
124+ docker rm -f postgres-db || true
125+ docker network rm bica-net || true
126+
127+ backup-encrypted :
128+ name : Backup Encrypted
129+ runs-on : ubuntu-latest
130+ needs : [build, setup-postgres]
131+ steps :
132+ - uses : actions/checkout@v4
133+
134+ - name : Download image artifact
135+ uses : actions/download-artifact@v4
136+ with :
137+ name : docker-image
138+ path : .
139+
140+ - name : Load docker image
141+ run : docker load -i image.tar
142+
143+ - name : Create Docker network
144+ run : docker network create bica-net || true
145+
146+ - name : Prepare backup folder
147+ run : mkdir -p ./backups
148+
116149 - name : Run encrypted backup
117150 run : |
118151 docker run --rm --network bica-net \
@@ -129,23 +162,45 @@ jobs:
129162 -v ${{ github.workspace }}/backups:/mnt/backups \
130163 $IMAGE_NAME:$TAG
131164
132- - name : List encrypted backups
133- run : ls -lh ./backups/*.enc
165+ - name : List backups
166+ run : ls -lh ./backups
167+
168+ - name : Upload encrypted backup
169+ uses : actions/upload-artifact@v4
170+ with :
171+ name : encrypted-backup
172+ path : ./backups/*.enc
134173
135- - name : Decrypt backup and show pg_dump
174+ - name : Cleanup
175+ run : |
176+ docker rm -f postgres-db || true
177+ docker network rm bica-net || true
178+
179+ decrypt-and-show :
180+ name : Decrypt backup and show pg_dump
181+ runs-on : ubuntu-latest
182+ needs : backup-encrypted
183+ steps :
184+ - name : Prepare folder
185+ run : mkdir -p ./backups
186+
187+ - name : Download encrypted backup artifact
188+ uses : actions/download-artifact@v4
189+ with :
190+ name : encrypted-backup
191+ path : ./backups
192+
193+ - name : Decrypt backup
136194 run : |
137195 ENCRYPT_PASS=MySecretKey
138196 for f in ./backups/*.enc; do
139197 openssl enc -aes-256-cbc -d -pbkdf2 -salt -in "$f" -out "${f%.enc}.tar.gz" -k "$ENCRYPT_PASS"
140198 done
141- tar -xzf ./backups/*.tar.gz -C ./backups
142- head -40 ./backups/db_backup.sql
143199
144- - name : Cleanup Docker containers and network
200+ - name : Extract decrypted tarball and show pg_dump
145201 run : |
146- docker rm -f postgres-db || true
147- docker network rm bica-net || true
148-
202+ tar -xzf ./backups/*.tar.gz -C ./backups
203+ head -40 ./backups/db_backup.sql
149204 docker-publish :
150205 name : Push to Docker Hub
151206 runs-on : ubuntu-latest
@@ -169,4 +224,4 @@ jobs:
169224 - name : Push image
170225 run : |
171226 docker tag $IMAGE_NAME:$TAG ${{ secrets.DOCKER_USERNAME }}/$IMAGE_NAME:latest
172- docker push ${{ secrets.DOCKER_USERNAME }}/$IMAGE_NAME:latest
227+ docker push ${{ secrets.DOCKER_USERNAME }}/$IMAGE_NAME:latest epa corrige la este codigo para o problema das networks va
0 commit comments