107 lines
4.0 KiB
YAML
107 lines
4.0 KiB
YAML
name: CI/CD
|
|
|
|
on:
|
|
push:
|
|
pull_request:
|
|
|
|
# Registry on the local Gitea instance (HTTP — add to Docker insecure-registries if needed)
|
|
env:
|
|
REGISTRY: 192.168.0.234:8765
|
|
# Deployment directory on the host — compose file lives here so relative
|
|
# volume paths (./infra/traefik/...) resolve correctly on the host.
|
|
DEPLOY_DIR: /opt/ems
|
|
|
|
jobs:
|
|
|
|
# ── Test ──────────────────────────────────────────────────────────────────
|
|
# Runs on every push and every PR.
|
|
test:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- name: Install Go 1.23
|
|
run: |
|
|
wget -qO go.tar.gz https://go.dev/dl/go1.23.4.linux-arm64.tar.gz
|
|
sudo tar -C /usr/local -xzf go.tar.gz
|
|
rm go.tar.gz
|
|
echo "/usr/local/go/bin" >> $GITHUB_PATH
|
|
|
|
- name: Run unit tests
|
|
run: go test ./...
|
|
|
|
- name: Build binary (compile check)
|
|
run: CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /dev/null .
|
|
|
|
# ── Build & Push ──────────────────────────────────────────────────────────
|
|
# Builds the Docker image and pushes it to the Gitea container registry.
|
|
# Runs on house/lutz and main; skipped on other branches and PRs.
|
|
#
|
|
# Required secret:
|
|
# GITEA_TOKEN — personal access token with write:packages permission
|
|
build-push:
|
|
needs: test
|
|
runs-on: ubuntu-latest
|
|
if: github.ref == 'refs/heads/house/lutz' || github.ref == 'refs/heads/main'
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- name: Log in to Gitea registry
|
|
run: |
|
|
echo "${{ secrets.GITEA_TOKEN }}" | \
|
|
docker login ${{ env.REGISTRY }} -u ${{ gitea.actor }} --password-stdin
|
|
|
|
- name: Build and push image
|
|
run: |
|
|
IMAGE=${{ env.REGISTRY }}/${{ gitea.repository_owner }}/ems
|
|
docker build \
|
|
-t ${IMAGE}:${{ gitea.sha }} \
|
|
-t ${IMAGE}:latest \
|
|
.
|
|
docker push ${IMAGE}:${{ gitea.sha }}
|
|
docker push ${IMAGE}:latest
|
|
echo "IMAGE=${IMAGE}:${{ gitea.sha }}" >> $GITHUB_ENV
|
|
|
|
# ── Deploy ────────────────────────────────────────────────────────────────
|
|
# Syncs compose + infra files to DEPLOY_DIR on the host, then restarts
|
|
# only the EMS container. Traefik keeps running untouched.
|
|
#
|
|
# Uses a docker:cli helper container so that:
|
|
# - relative paths in docker-compose.yml resolve from DEPLOY_DIR on the host
|
|
# - Docker socket gives access to the host daemon from within the job container
|
|
deploy:
|
|
needs: build-push
|
|
runs-on: ubuntu-latest
|
|
if: github.ref == 'refs/heads/house/lutz' || github.ref == 'refs/heads/main'
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
|
|
- name: Log in to Gitea registry
|
|
run: |
|
|
echo "${{ secrets.GITEA_TOKEN }}" | \
|
|
docker login ${{ env.REGISTRY }} -u ${{ gitea.actor }} --password-stdin
|
|
|
|
- name: Sync files and restart EMS
|
|
run: |
|
|
IMAGE=${{ env.REGISTRY }}/${{ gitea.repository_owner }}/ems:${{ gitea.sha }}
|
|
|
|
docker run --rm \
|
|
-v /var/run/docker.sock:/var/run/docker.sock \
|
|
-v ${{ env.DEPLOY_DIR }}:${{ env.DEPLOY_DIR }} \
|
|
-v "$(pwd)":/src:ro \
|
|
-e EMS_IMAGE=${IMAGE} \
|
|
docker:cli sh -c "
|
|
set -e
|
|
mkdir -p ${{ env.DEPLOY_DIR }}
|
|
cp /src/docker-compose.yml ${{ env.DEPLOY_DIR }}/
|
|
cp -r /src/infra ${{ env.DEPLOY_DIR }}/
|
|
cd ${{ env.DEPLOY_DIR }}
|
|
docker compose pull ems
|
|
docker compose up -d ems
|
|
"
|
|
|
|
- name: Verify health
|
|
run: |
|
|
sleep 10
|
|
wget -qO- http://localhost:9099/health && echo " — OK"
|