From eb2ba4d19daf4016ba94f4d043e24fb84ba4cb11 Mon Sep 17 00:00:00 2001 From: "chris.dumas" Date: Fri, 2 Oct 2026 22:48:04 +0000 Subject: [PATCH] build immutable image before deploy --- .gitea/workflows/deploy.yml | 56 +++++++++++++++++++++++++++++++++---- README.md | 2 +- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 11d7a21..a435227 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -18,6 +18,56 @@ jobs: - run: npm test - run: npm run lint - run: npm run build + - name: Load deployment SSH key + if: ${{ gitea.event_name == 'push' }} + uses: webfactory/ssh-agent@v0.9.0 + with: + ssh-private-key: ${{ secrets.DEPLOY_SSH_KEY }} + - name: Build and publish immutable image + if: ${{ gitea.event_name == 'push' }} + shell: bash + env: + DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }} + DEPLOY_USER: ${{ secrets.DEPLOY_USER }} + REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }} + REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }} + GITEA_REF: ${{ gitea.ref }} + GITEA_SHA: ${{ gitea.sha }} + run: | + set -Eeuo pipefail + mkdir -p ~/.ssh + ssh-keyscan -H "$DEPLOY_HOST" >> ~/.ssh/known_hosts + case "$GITEA_REF" in + refs/heads/develop) TARGET_DIR=/mnt/appdata/dev/ComfyUiPortal; BRANCH=develop ;; + refs/heads/qa) TARGET_DIR=/mnt/appdata/qa/ComfyUiPortal; BRANCH=qa ;; + refs/heads/main) TARGET_DIR=/mnt/appdata/prod/ComfyUiPortal; BRANCH=main ;; + *) exit 0 ;; + esac + REPO_URL=ssh://git@git.dumas.ddns.net:222/chris.dumas/ComfyUiPortalNext.git + IMAGE_REF=git.dumas.ddns.net/chris.dumas/comfyui-portal-next:sha-${GITEA_SHA} + + if [ -z "$REGISTRY_USERNAME" ] || [ -z "$REGISTRY_PASSWORD" ]; then + echo 'REGISTRY_USERNAME and REGISTRY_PASSWORD are required for registry authentication' + exit 1 + fi + + printf '%s' "$REGISTRY_PASSWORD" | ssh -o StrictHostKeyChecking=yes "${DEPLOY_USER}@${DEPLOY_HOST}" "docker login git.dumas.ddns.net --username '$REGISTRY_USERNAME' --password-stdin" + ssh -o StrictHostKeyChecking=yes "${DEPLOY_USER}@${DEPLOY_HOST}" " + set -Eeuo pipefail + trap 'docker logout git.dumas.ddns.net >/dev/null 2>&1 || true' EXIT + mkdir -p '$TARGET_DIR' + if [ ! -d '$TARGET_DIR/.git' ]; then git init '$TARGET_DIR' && cd '$TARGET_DIR' && git remote add origin '$REPO_URL'; else cd '$TARGET_DIR' && git remote set-url origin '$REPO_URL'; fi + cd '$TARGET_DIR' + GIT_SSH_COMMAND='ssh -p 222 -o StrictHostKeyChecking=no' git fetch origin '$BRANCH' + git reset --hard 'origin/$BRANCH' + git clean -fdx -e data/ -e data/** + if docker manifest inspect '$IMAGE_REF' >/dev/null 2>&1; then + echo 'Reusing existing immutable image $IMAGE_REF' + else + docker build --tag '$IMAGE_REF' . + docker push '$IMAGE_REF' + fi + " deploy: if: ${{ gitea.event_name == 'push' }} @@ -72,12 +122,6 @@ jobs: GIT_SSH_COMMAND='ssh -p 222 -o StrictHostKeyChecking=no' git fetch origin '$BRANCH' git reset --hard 'origin/$BRANCH' git clean -fdx -e data/ -e data/** - if ! docker manifest inspect '$IMAGE_REF' >/dev/null 2>&1; then - docker build --tag '$IMAGE_REF' . - docker push '$IMAGE_REF' - else - echo 'Reusing existing immutable image $IMAGE_REF' - fi docker ps -q --filter publish=$PORT | xargs -r docker rm -f docker compose -p '$PROJECT' -f docker-compose.yml -f '$COMPOSE_FILE' down --remove-orphans || true IMAGE_REF='$IMAGE_REF' docker compose -p '$PROJECT' -f docker-compose.yml -f '$COMPOSE_FILE' pull diff --git a/README.md b/README.md index 12aab40..4f4adb8 100644 --- a/README.md +++ b/README.md @@ -15,4 +15,4 @@ The first run seeds `data/users.json` from `ADMIN_USERNAME` and `ADMIN_PASSWORD` Gitea Actions deploys `develop`, `qa`, and `main` to the existing ComfyUiPortal target directories and preserves the existing 7002/6002/5002 port contract. On first startup of a fresh data directory, provide `ADMIN_USERNAME` and `ADMIN_PASSWORD` in the deployment host environment. Existing target data is retained during repository cutover. -The deployment workflow builds an immutable image tagged with the source commit SHA and publishes it to the Gitea container registry. If that SHA-tagged image already exists, the workflow reuses it instead of rebuilding. Configure the masked `REGISTRY_USERNAME` and `REGISTRY_PASSWORD` repository Actions secrets with a Gitea account/token that can write container packages. Compose receives the exact image reference through `IMAGE_REF`, so the same artifact can be redeployed or promoted without rebuilding. +After validation succeeds, the workflow builds one immutable image tagged with the source commit SHA on the Docker host and publishes it to the Gitea container registry. The deploy stage only pulls that exact image and restarts the service; it does not build. If the SHA-tagged image already exists, the workflow reuses it instead of rebuilding. Configure the masked `REGISTRY_USERNAME` and `REGISTRY_PASSWORD` repository Actions secrets with a Gitea account/token that can write container packages. Compose receives the exact image reference through `IMAGE_REF`, so the same artifact can be redeployed or promoted without rebuilding.