Skip to content

Commit

Permalink
feat: swarm additions
Browse files Browse the repository at this point in the history
- additions for swarm restarts and health checks

- build images workflow
  • Loading branch information
thenick775 committed Nov 13, 2024
1 parent 8ea6e8d commit b3b0a8c
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 11 deletions.
24 changes: 24 additions & 0 deletions .env.example.swarm
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# merged env
# gbajs3
export ROM_PATH=./auth/local_roms/
export SAVE_PATH=./auth/local_saves/
export CLIENT_HOST=https://localhost
export CERT_DIR=./auth/certs
export CERT_LOC=$CERT_DIR/certificate.crt
export KEY_LOC=$CERT_DIR/privateKey.key

# admin
export ADMIN_APP_ID=12345678910b

# postgres
export PG_DB_HOST=gba-postgres
export PG_DB_USER=postgres
export PG_DB_PASSWORD=postgres
export GBAJS_DB_NAME=gbajs3
export ADMIN_DB_NAME=goadmin
export PG_DB_PORT=5432
export PG_SSL_MODE=disable
export PG_DATA_LOCATION=./postgres/pg_data

# registry items
export REGISTRY_PREFIX=ghcr.io/thenick775/gbajs3/
70 changes: 70 additions & 0 deletions .github/workflows/publish-images.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Build images

on:
push:
tags:
- '*'

jobs:
setup:
runs-on: ubuntu-latest
outputs:
repo_name: ${{ steps.set_repo_name.outputs.repo_name }}
image_tag: ${{ steps.set_image_tag.outputs.image_tag }}

steps:
- id: set_repo_name
run: echo "repo_name=$(echo "${{ github.repository }}" | cut -d'/' -f2)" >> $GITHUB_OUTPUT

- id: set_image_tag
run: echo "image_tag=${{ github.ref_name }}" >> $GITHUB_OUTPUT

build-and-push:
runs-on: ubuntu-latest
needs: setup
strategy:
matrix:
image:
- name: gba-postgres
directory: ./postgres
build_arg_name: PG_DB_USER
- name: gba-webserver
directory: ./gbajs3
build_arg_name: CLIENT_HOST
- name: gba-auth-server
directory: ./auth
- name: gba-admin-server
directory: ./admin

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Login to Registry
run: echo "${{ secrets.GHCR_PAT }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin

- name: Build
env:
REPO_NAME: ${{ needs.setup.outputs.repo_name }}
IMAGE_TAG: ${{ needs.setup.outputs.image_tag }}
BUILD_ARG_NAME: ${{ matrix.image.build_arg_name }}
BUILD_ARG_VALUE: ${{ secrets[matrix.image.build_arg_name] || '' }}
run: |
if [ -n "$BUILD_ARG_NAME" ] && [ -n "$BUILD_ARG_VALUE" ]; then
docker build --build-arg $BUILD_ARG_NAME=$BUILD_ARG_VALUE \
-t ghcr.io/${{ github.actor }}/${REPO_NAME}/${{ matrix.image.name }}:${IMAGE_TAG} \
-t ghcr.io/${{ github.actor }}/${REPO_NAME}/${{ matrix.image.name }}:latest \
${{ matrix.image.directory }}
else
docker build -t ghcr.io/${{ github.actor }}/${REPO_NAME}/${{ matrix.image.name }}:${IMAGE_TAG} \
-t ghcr.io/${{ github.actor }}/${REPO_NAME}/${{ matrix.image.name }}:latest \
${{ matrix.image.directory }}
fi
- name: Push
env:
REPO_NAME: ${{ needs.setup.outputs.repo_name }}
IMAGE_TAG: ${{ needs.setup.outputs.image_tag }}
run: |
docker push ghcr.io/${{ github.actor }}/${REPO_NAME}/${{ matrix.image.name }}:${IMAGE_TAG}
docker push ghcr.io/${{ github.actor }}/${REPO_NAME}/${{ matrix.image.name }}:latest
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
/**/users.db
/**/swag
/**/.env
/**/.env.swarm
/**/shepherd-registries
/**/certs/**
/**/*.log
/**/authapi
Expand Down
3 changes: 3 additions & 0 deletions admin/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ RUN make build

# second stage
FROM alpine:latest

LABEL org.opencontainers.image.source https://github.com/thenick775/gbajs3

WORKDIR /app/
COPY --from=builder /app/build/goadmin .
COPY --from=builder /app/html/ ./html
Expand Down
2 changes: 1 addition & 1 deletion admin/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
gba-admin-server:
image: gba-admin-server
image: ${REGISTRY_PREFIX}gba-admin-server
logging:
options:
max-size: '20m'
Expand Down
3 changes: 3 additions & 0 deletions auth/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ RUN CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-w -s' -o authserver

# second stage
FROM alpine:latest

LABEL org.opencontainers.image.source https://github.com/thenick775/gbajs3

WORKDIR /app/
COPY --from=builder /app/authserver .
CMD ["./authserver"]
2 changes: 1 addition & 1 deletion auth/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
gba-auth-server:
image: gba-auth-server
image: ${REGISTRY_PREFIX}gba-auth-server
logging:
options:
max-size: '20m'
Expand Down
3 changes: 3 additions & 0 deletions bin/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ if [[ $REPLY == "y" || $REPLY == "Y" ]]; then
if [ -d "$dir" ]; then
cp "$dir/.env.example" "$dir/.env" &&
echo "Copied .env.example to .env in $dir"

cp "$dir/.env.example.swarm" "$dir/.env.swarm" &&
echo "Copied .env.example.swarm to .env.swarm in $dir"
else
echo "Directory $dir does not exist."
fi
Expand Down
30 changes: 25 additions & 5 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
x-restart-policy: &default-restart-policy
condition: on-failure
max_attempts: 50
window: 30s

services:
gba-postgres:
deploy:
restart_policy: *default-restart-policy
labels:
- shepherd.auth.config=github
gba-auth-server:
depends_on:
gba-postgres:
condition: service_healthy
- gba-postgres
deploy:
restart_policy: *default-restart-policy
labels:
- shepherd.auth.config=github
gba-admin-server:
depends_on:
gba-postgres:
condition: service_healthy
- gba-postgres
deploy:
restart_policy: *default-restart-policy
labels:
- shepherd.auth.config=github
gba-webserver:
depends_on:
- gba-auth-server
- gba-admin-server
- gba-admin-server
deploy:
restart_policy: *default-restart-policy
labels:
- shepherd.auth.config=github
3 changes: 3 additions & 0 deletions gbajs3/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ RUN npm run build

# second stage, server
FROM nginx:stable-alpine

LABEL org.opencontainers.image.source https://github.com/thenick775/gbajs3

COPY ./docker/nginx.conf.template /etc/nginx/templates/nginx.conf.template

# copy build
Expand Down
2 changes: 1 addition & 1 deletion gbajs3/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
gba-webserver:
image: gba-webserver
image: ${REGISTRY_PREFIX}gba-webserver
logging:
options:
max-size: '20m'
Expand Down
2 changes: 2 additions & 0 deletions postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM postgres:15.2-alpine

LABEL org.opencontainers.image.source https://github.com/thenick775/gbajs3

# copy the initialization script to the container,
# this will create the go-admin db structure as
# well as initialize gbajs3 specific content
Expand Down
5 changes: 2 additions & 3 deletions postgres/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
gba-postgres:
image: gba-postgres
image: ${REGISTRY_PREFIX}gba-postgres
logging:
options:
max-size: '20m'
Expand All @@ -13,10 +13,9 @@ services:
environment:
POSTGRES_USER: ${PG_DB_USER}
POSTGRES_PASSWORD: ${PG_DB_PASSWORD}
POSTGRES_DB: ${ADMIN_DB_NAME} # todo reevaluate if this is needed
command: postgres
healthcheck:
test: ["CMD-SHELL", "pg_isready -q -U ${PG_DB_USER} -d ${ADMIN_DB_NAME} -h ${PG_DB_HOST}"]
test: ["CMD-SHELL", "pg_isready -q -U ${PG_DB_USER} -h localhost"]
interval: 10s
timeout: 5s
retries: 12
Expand Down
23 changes: 23 additions & 0 deletions shepherd/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
services:
shepherd:
build: .
image: containrrr/shepherd
volumes:
- /var/run/docker.sock:/var/run/docker.sock
deploy:
placement:
constraints:
- node.role == manager
environment:
- IGNORELIST_SERVICES=gbajs3_shepherd
- IMAGE_AUTOCLEAN_LIMIT=1
- ROLLBACK_ON_FAILURE=true
- WITH_REGISTRY_AUTH=true
- REGISTRIES_FILE=/var/run/secrets/shepherd-registries
- VERBOSE=true
restart: always
secrets:
- shepherd-registries
secrets:
shepherd-registries:
file: ./shepherd/shepherd-registries

0 comments on commit b3b0a8c

Please sign in to comment.