refac:updated ci/cd pipeline to create docker images with timestamp and commit sha
Build & Deploy (develop) / build-and-deploy (push) Successful in 2m17s
Build & Deploy (develop) / build-and-deploy (push) Successful in 2m17s
This commit is contained in:
@@ -20,17 +20,20 @@ jobs:
|
|||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Resolve commit SHA
|
- name: Resolve image tag
|
||||||
id: vars
|
id: vars
|
||||||
run: |
|
run: |
|
||||||
# For PRs, gitea.sha is a temporary merge commit — use the real
|
# For PRs, gitea.sha is a temporary merge commit — use the real
|
||||||
# PR head commit instead so tags map to actual git history.
|
# PR head commit instead so tags map to actual git history.
|
||||||
FULL_SHA="${{ gitea.event.pull_request.head.sha || gitea.sha }}"
|
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: |
|
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
|
- name: Save previous tag for rollback
|
||||||
id: backup
|
id: backup
|
||||||
@@ -42,7 +45,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Point compose at this build
|
- name: Point compose at this build
|
||||||
run: |
|
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
|
- name: Deploy via compose
|
||||||
id: deploy
|
id: deploy
|
||||||
@@ -85,7 +88,7 @@ jobs:
|
|||||||
- name: Clean up dangling (untagged) layers
|
- name: Clean up dangling (untagged) layers
|
||||||
run: docker image prune -f
|
run: docker image prune -f
|
||||||
|
|
||||||
- name: Prune old SHA-tagged images (keep last 5)
|
- name: Prune old build-tagged images (keep last 5)
|
||||||
run: |
|
run: |
|
||||||
KEEP=5
|
KEEP=5
|
||||||
CURRENT_TAG=$(grep IMAGE_TAG /opt/robot-market-front/.env 2>/dev/null | cut -d= -f2)
|
CURRENT_TAG=$(grep IMAGE_TAG /opt/robot-market-front/.env 2>/dev/null | cut -d= -f2)
|
||||||
|
|||||||
@@ -2,7 +2,8 @@ name: Rollback (develop)
|
|||||||
|
|
||||||
# Trigger manually from the Gitea Actions UI.
|
# Trigger manually from the Gitea Actions UI.
|
||||||
# Leave "sha" empty to roll back to the previous deploy.
|
# 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:
|
on:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
inputs:
|
inputs:
|
||||||
@@ -18,7 +19,15 @@ jobs:
|
|||||||
- name: Determine target tag
|
- name: Determine target tag
|
||||||
run: |
|
run: |
|
||||||
if [ -n "${{ gitea.event.inputs.sha }}" ]; then
|
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
|
else
|
||||||
TAG=$(grep IMAGE_TAG /opt/robot-market-front/.env.previous | cut -d= -f2)
|
TAG=$(grep IMAGE_TAG /opt/robot-market-front/.env.previous | cut -d= -f2)
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user