From affb1b1ad8568c149ba5e4210df808076a21b6cd Mon Sep 17 00:00:00 2001 From: ryan Date: Sun, 6 Sep 2026 13:52:34 +0330 Subject: [PATCH 1/2] feat:added CI/CD worflow and rollback workflow --- .gitea/workflows/develop.yml | 55 +++++++++++++++++++++++++++++++++++ .gitea/workflows/rollback.yml | 37 +++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 .gitea/workflows/develop.yml create mode 100644 .gitea/workflows/rollback.yml diff --git a/.gitea/workflows/develop.yml b/.gitea/workflows/develop.yml new file mode 100644 index 0000000..5ef42eb --- /dev/null +++ b/.gitea/workflows/develop.yml @@ -0,0 +1,55 @@ +name: Build & Deploy (develop) + +# Runs on: +# - Pull requests targeting develop +# - Direct pushes to develop +# main is intentionally NOT included yet (no prod target configured) +on: + push: + branches: + - develop + pull_request: + branches: + - develop + +jobs: + build-and-deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Resolve commit SHA + 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" + + - name: Build Docker image (tagged by commit SHA) + run: | + docker build -t robot-market:${{ steps.vars.outputs.sha }} . + + - name: Save previous tag for rollback + run: | + if [ -f /opt/robot-market-front/.env ]; then + cp /opt/robot-market-front/.env /opt/robot-market-front/.env.previous + fi + + - name: Point compose at this build + run: | + echo "IMAGE_TAG=${{ steps.vars.outputs.sha }}" > /opt/robot-market-front/.env + + - name: Deploy via compose + run: | + docker compose --env-file /opt/robot-market-front/.env \ + -f /opt/robot-market-front/docker-compose.yml \ + up -d --force-recreate + + - name: Clean up dangling (untagged) layers only + run: docker image prune -f + # Note: this does NOT remove SHA-tagged images, so rollback targets + # stay available. Periodically clean up old SHA tags manually if + # disk usage grows (e.g. keep the last 10 builds). \ No newline at end of file diff --git a/.gitea/workflows/rollback.yml b/.gitea/workflows/rollback.yml new file mode 100644 index 0000000..cd72baa --- /dev/null +++ b/.gitea/workflows/rollback.yml @@ -0,0 +1,37 @@ +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. +on: + workflow_dispatch: + inputs: + sha: + description: "Short commit SHA to roll back to (empty = previous deploy)" + required: false + default: "" + +jobs: + rollback: + runs-on: ubuntu-latest + steps: + - name: Determine target tag + run: | + if [ -n "${{ gitea.event.inputs.sha }}" ]; then + TAG="${{ gitea.event.inputs.sha }}" + else + TAG=$(grep IMAGE_TAG /opt/robot-market-front/.env.previous | cut -d= -f2) + fi + echo "Rolling back to: $TAG" + echo "IMAGE_TAG=$TAG" > /opt/robot-market-front/.env + + - name: Verify that image actually exists locally + run: | + TAG=$(grep IMAGE_TAG /opt/robot-market-front/.env | cut -d= -f2) + docker image inspect robot-market:$TAG > /dev/null + + - name: Redeploy target image + run: | + docker compose --env-file /opt/robot-market-front/.env \ + -f /opt/robot-market-front/docker-compose.yml \ + up -d --force-recreate \ No newline at end of file From af9f92478ed481d6c5cfbf78f65b99daa65a357d Mon Sep 17 00:00:00 2001 From: ryan Date: Sun, 6 Sep 2026 14:22:05 +0330 Subject: [PATCH 2/2] feat:added CI/CD worflow and rollback workflow --- .gitea/workflows/develop.yml | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/.gitea/workflows/develop.yml b/.gitea/workflows/develop.yml index 5ef42eb..b2a98f7 100644 --- a/.gitea/workflows/develop.yml +++ b/.gitea/workflows/develop.yml @@ -48,6 +48,32 @@ jobs: -f /opt/robot-market-front/docker-compose.yml \ up -d --force-recreate + - name: Health check (retries for ~30s) + run: | + for i in $(seq 1 6); do + if docker exec robot-market wget -qO- --timeout=5 http://localhost:3000 > /dev/null 2>&1; then + echo "Container is healthy" + exit 0 + fi + echo "Not ready yet, retrying..." + sleep 5 + done + echo "Health check failed after 30s" + exit 1 + + - name: Roll back to previous version on failed health check + if: failure() + run: | + if [ -f /opt/robot-market-front/.env.previous ]; then + cp /opt/robot-market-front/.env.previous /opt/robot-market-front/.env + docker compose --env-file /opt/robot-market-front/.env \ + -f /opt/robot-market-front/docker-compose.yml \ + up -d --force-recreate + echo "Rolled back to previous image." + else + echo "No previous deploy recorded — nothing to roll back to." + fi + - name: Clean up dangling (untagged) layers only run: docker image prune -f # Note: this does NOT remove SHA-tagged images, so rollback targets