From b93c780feafb23f5263f79f16372ade6a9d41c98 Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 29 Sep 2026 10:59:09 +0330 Subject: [PATCH] refac:updated ci/cd pipeline to create docker images with timestamp and commit sha --- .gitea/workflows/develop.yml | 15 +++++++++------ .gitea/workflows/rollback.yml | 13 +++++++++++-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/.gitea/workflows/develop.yml b/.gitea/workflows/develop.yml index c337495..0e7d5d2 100644 --- a/.gitea/workflows/develop.yml +++ b/.gitea/workflows/develop.yml @@ -20,17 +20,20 @@ jobs: - name: Checkout code uses: actions/checkout@v4 - - name: Resolve commit SHA + - name: Resolve image tag id: vars run: | # For PRs, gitea.sha is a temporary merge commit — use the real # PR head commit instead so tags map to actual git history. FULL_SHA="${{ gitea.event.pull_request.head.sha || gitea.sha }}" - echo "sha=${FULL_SHA:0:7}" >> "$GITHUB_OUTPUT" + SHORT_SHA="${FULL_SHA:0:7}" + BUILD_TIME=$(date -u +%Y%m%d-%H%M%S) + echo "sha=${SHORT_SHA}" >> "$GITHUB_OUTPUT" + echo "tag=${BUILD_TIME}-${SHORT_SHA}" >> "$GITHUB_OUTPUT" - - name: Build Docker image (tagged by commit SHA) + - name: Build Docker image (tagged by UTC build time and commit SHA) run: | - docker build -t robot-market:${{ steps.vars.outputs.sha }} . + docker build -t robot-market:${{ steps.vars.outputs.tag }} . - name: Save previous tag for rollback id: backup @@ -42,7 +45,7 @@ jobs: - name: Point compose at this build run: | - echo "IMAGE_TAG=${{ steps.vars.outputs.sha }}" > /opt/robot-market-front/.env + echo "IMAGE_TAG=${{ steps.vars.outputs.tag }}" > /opt/robot-market-front/.env - name: Deploy via compose id: deploy @@ -85,7 +88,7 @@ jobs: - name: Clean up dangling (untagged) layers run: docker image prune -f - - name: Prune old SHA-tagged images (keep last 5) + - name: Prune old build-tagged images (keep last 5) run: | KEEP=5 CURRENT_TAG=$(grep IMAGE_TAG /opt/robot-market-front/.env 2>/dev/null | cut -d= -f2) diff --git a/.gitea/workflows/rollback.yml b/.gitea/workflows/rollback.yml index 6895551..9d84ca8 100644 --- a/.gitea/workflows/rollback.yml +++ b/.gitea/workflows/rollback.yml @@ -2,7 +2,8 @@ name: Rollback (develop) # Trigger manually from the Gitea Actions UI. # Leave "sha" empty to roll back to the previous deploy. -# Fill "sha" in (short 7-char form, e.g. a3f9c21) to roll back to a specific past build. +# Fill "sha" in (short 7-char form, e.g. a3f9c21) to roll back to the newest +# build for that commit. Build tags include a UTC timestamp and commit SHA. on: workflow_dispatch: inputs: @@ -18,7 +19,15 @@ jobs: - name: Determine target tag run: | if [ -n "${{ gitea.event.inputs.sha }}" ]; then - TAG="${{ gitea.event.inputs.sha }}" + SHA="${{ gitea.event.inputs.sha }}" + TAG=$(docker images robot-market --format '{{.Tag}}' \ + | grep -E "-${SHA}$" \ + | sort -r \ + | head -n 1) + # Keep accepting legacy SHA-only tags created before timestamped tags. + if [ -z "$TAG" ]; then + TAG="$SHA" + fi else TAG=$(grep IMAGE_TAG /opt/robot-market-front/.env.previous | cut -d= -f2) fi