Skip to content

Commit 0f484b0

Browse files
committed
WIP: Try first build
Signed-off-by: Nikolai Rodionov <iam@allanger.xyz>
1 parent 3c6ece8 commit 0f484b0

5 files changed

Lines changed: 179 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Publish a container image
2+
3+
on:
4+
push:
5+
release:
6+
types: [published, edited]
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-24.04
11+
permissions:
12+
packages: write
13+
steps:
14+
- uses: actions/checkout@v6
15+
- uses: docker/setup-qemu-action@v3
16+
- run: ./build/set_image_metadata
17+
18+
- name: Build an image
19+
id: build-image
20+
uses: redhat-actions/buildah-build@v2
21+
with:
22+
image: ${{ github.event.repository.name }} -dev
23+
tags: ${{ env.TAGS }}
24+
platforms: linux/amd64, linux/arm64/v8
25+
containerfiles: |
26+
./Containerfile
27+
labels: ${{ env.ANNOTATIONS }}
28+
29+
- name: Push the image to GHCR
30+
id: push-to-ghcr
31+
uses: redhat-actions/push-to-registry@v2
32+
with:
33+
image: ${{ steps.build-image.outputs.image }}
34+
tags: ${{ steps.build-image.outputs.tags }}
35+
registry: ghcr.io/${{ github.repository_owner }}
36+
username: ${{ github.actor }}
37+
password: ${{ github.token }}
38+
39+
- name: Set the version tag (only if released)
40+
id: retag-version
41+
if: (github.event_name == 'release' && (github.event.action == 'published' || github.event.action == 'edited'))
42+
run: |
43+
buildah tag \
44+
${{ steps.build-image.outputs.image }}:${{ github.sha }} \
45+
ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:latest
46+
buildah tag \
47+
${{ steps.build-image.outputs.image }}:${{ github.sha }} \
48+
ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:${{ github.event.release.tag_name }}
49+
50+
- name: Push the release image to GHCR
51+
if: (github.event_name == 'release' && (github.event.action == 'published' || github.event.action == 'edited'))
52+
uses: redhat-actions/push-to-registry@v2
53+
with:
54+
tags: ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:latest ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:${{ github.event.release.tag_name }}
55+
username: ${{ github.actor }}
56+
password: ${{ github.token }}

Containerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM postgres:13-alpine
2+
3+
RUN apk --no-cache add \
4+
curl \
5+
bash
6+
7+
COPY entrypoint.sh /
8+
RUN chmod +x /entrypoint.sh
9+
10+
ENTRYPOINT /entrypoint.sh

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# pgdump-gcs
2+
3+
Small docker container for creating a backup of a psql database and upload the dump to an external storage using rclone.
4+
5+
6+
TO BE DONE ...
7+
## how to use
8+
9+
```bash
10+
docker run \
11+
-v ./cred:/cred \
12+
-e DB_HOST={db host addess} \
13+
-e DB_NAME={database-name} \
14+
-e DB_PASSWORD_FILE=/cred/my_db_pass_as_file \
15+
-e DB_USERNAME_FILE=/cred/my_db_user_as_file \
16+
ghcr.io/db-operatopr/pgdump-rclone:postgres-
17+
```
18+
19+
## tipps
20+
21+
- create a lifecycle rule to keep your gcs bucket small
22+
- we create also a `_latest` file, so able to access the latest backup with another script
23+
24+
## monitoring
25+
26+
Simple curl pushing some basic parameter to a prometheus push gateway.
27+
28+
### metrics
29+
* timestamp
30+
* duration
31+
* size
32+
33+
### labels
34+
* job = pgdump-gcs
35+
* source_type = postgresql
36+
* source_name = `${DB_NAME}`

build/set_image_metadata

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#! /usr/bin/env bash
2+
# --------------------------------------------
3+
# -- Should be used in Github Actions
4+
# --------------------------------------------
5+
#
6+
# --------------------------------------------
7+
# -- To have a multi-line env var in github,
8+
# -- we must define the EOF
9+
# --------------------------------------------
10+
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
11+
echo "ANNOTATIONS<<$EOF" >> "$GITHUB_ENV"
12+
13+
ANNOTATIONS=$(cat << EOF
14+
org.opencontainers.image.created=$(date +"%Y-%m-%d %T")
15+
org.opencontainers.image.authors=$GITHUB_ACTOR
16+
org.opencontainers.image.url=$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID
17+
org.opencontainers.image.documentation=$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/blob/main/README.md
18+
org.opencontainers.image.source=$GITHUB_SERVER_URL/$GITHUB_REPOSITORY
19+
org.opencontainers.image.version=$GITHUB_SHA
20+
org.opencontainers.image.revision=$GITHUB_SHA
21+
org.opencontainers.image.vendor=$GITHUB_REPOSITORY_OWNER
22+
org.opencontainers.image.license=GNU GENERAL PUBLIC LICENSE v3
23+
org.opencontainers.image.title=$GITHUB_REPOSITORY
24+
org.opencontainers.image.description=Backup databases using pg_dump and upload backups using rclone
25+
EOF
26+
)
27+
28+
echo "${ANNOTATIONS}" >> "${GITHUB_ENV}"
29+
echo "$EOF" >> "$GITHUB_ENV"
30+
# --------------------------------------------
31+
# -- Set the image tag by commit sha
32+
# --------------------------------------------
33+
echo "TAGS=${GITHUB_SHA}" >> "${GITHUB_ENV}"
34+
35+

entrypoint.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
set -e
3+
4+
echo "Prepare configuration for script"
5+
TIMESTAMP=$(date +%F_%R)
6+
START_TIMESTAMP=$(date +%s)
7+
BACKUP_FILE=${DB_NAME}-${TIMESTAMP}.sql.gz
8+
BACKUP_FILE_LATEST=${DB_NAME}-latest.sql.gz
9+
DB_HOST=${DB_HOST:-localhost}
10+
DB_PASSWORD=$(cat ${DB_PASSWORD_FILE})
11+
DB_USER=$(cat ${DB_USERNAME_FILE})
12+
PROM_NAMESPACE=${PROM_NAMESPACE:-dboperator}
13+
14+
# create login credential file
15+
(umask 377 && echo *:5432:*:${DB_USER}:${DB_PASSWORD} >> ~/.pgpass)
16+
17+
echo "Start create backup"
18+
pg_dump -F c -Z 9 -h ${DB_HOST} -p 5432 -U ${DB_USER} ${DB_NAME} -f ${BACKUP_FILE}
19+
BACKUP_SIZE=$(du ${BACKUP_FILE} | awk '{print $1}')
20+
echo "End backup"
21+
22+
## copy to destination
23+
echo "Copy to gcs"
24+
gsutil cp ${BACKUP_FILE} gs://${GCS_BUCKET}/${DB_NAME}/${BACKUP_FILE} && gsutil cp ${BACKUP_FILE} gs://${GCS_BUCKET}/${DB_NAME}/${BACKUP_FILE_LATEST}
25+
26+
END_TIMESTAMP=$(date +%s)
27+
BACKUP_DURATION=$((END_TIMESTAMP - START_TIMESTAMP))
28+
if [[ ! -z "$PROMETHEUS_PUSH_GATEWAY" ]];
29+
then
30+
echo "sending monitoring metrics to ${PROMETHEUS_PUSH_GATEWAY}"
31+
cat <<EOF | curl -s --data-binary @- http://${PROMETHEUS_PUSH_GATEWAY}/metrics/job/pgdump-gcs/source_type/postgresql/source_name/${DB_NAME}
32+
# TYPE ${PROM_NAMESPACE}_backup_timestamp counter
33+
# HELP ${PROM_NAMESPACE}_backup_timestamp Timestamp of last backup run
34+
${PROM_NAMESPACE}_backup_timestamp $END_TIMESTAMP
35+
# TYPE ${PROM_NAMESPACE}_backup_duration gauge
36+
# HELP ${PROM_NAMESPACE}_backup_duration Time the backup run take until finished
37+
${PROM_NAMESPACE}_backup_duration $BACKUP_DURATION
38+
# TYPE ${PROM_NAMESPACE}_backup_size gauge
39+
# HELP ${PROM_NAMESPACE}_backup_size Backup Size in bytes
40+
${PROM_NAMESPACE}_backup_size $BACKUP_SIZE
41+
EOF
42+
fi

0 commit comments

Comments
 (0)