Skip to content

feat: add smarter caching #release #28

feat: add smarter caching #release

feat: add smarter caching #release #28

Workflow file for this run

# .github/workflows/ci.yml
name: CI (Auto-tag + Build & Push Docker on SemVer)
on:
push:
branches: [ "main" ] # main pushes can create a tag (no image push)
tags: [ "v*.*.*", "*.*.*" ] # tag pushes build & push the SemVer image
workflow_dispatch: {}
env:
REGISTRY: docker.io
IMAGE_NAME: ${{ secrets.DOCKERHUB_USERNAME }}/gabs-redis-langcache
DOCKERFILE: ./Dockerfile
# We need write so the job can create a tag via GitHub API
permissions:
contents: write
jobs:
# 1) MAIN PUSH: if any commit message includes #release, create next SemVer tag (no Docker build here)
auto-release:
if: ${{ github.ref == 'refs/heads/main' }}
runs-on: ubuntu-latest
steps:
- name: Checkout (full history for tags)
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Decide if this push requests a release
id: decide
run: |
MSGS="${{ join(github.event.commits.*.message, ' | ') }}"
echo "Commit messages: $MSGS"
if echo "$MSGS" | grep -q '#release'; then
echo "release=yes" >> $GITHUB_OUTPUT
else
echo "release=no" >> $GITHUB_OUTPUT
fi
- name: Compute next patch tag (vX.Y.Z -> vX.Y.(Z+1))
id: bump
if: steps.decide.outputs.release == 'yes'
shell: bash
run: |
set -e
LAST=$(git tag -l 'v*.*.*' --sort=-v:refname | head -n1)
[ -z "$LAST" ] && LAST="v0.0.0"
VER=${LAST#v}
IFS='.' read -r MA MI PA <<<"$VER"
NEW_TAG="v$MA.$MI.$((PA+1))"
echo "NEW_TAG=$NEW_TAG" >> $GITHUB_ENV
echo "new_tag=$NEW_TAG" >> $GITHUB_OUTPUT
echo "Last tag: $LAST -> Next tag: $NEW_TAG"
- name: Create git tag via GitHub API
if: steps.decide.outputs.release == 'yes'
uses: actions/github-script@v7
with:
script: |
const newTag = process.env.NEW_TAG; // from $GITHUB_ENV
core.info(`Creating tag ${newTag} at ${context.sha}`);
await github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `refs/tags/${newTag}`,
sha: context.sha
});
env:
NEW_TAG: ${{ env.NEW_TAG }}
# 2) TAG PUSH: ONLY build & push the SemVer image
build-and-push:
if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Extract Docker metadata (SemVer only)
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=semver,pattern={{version}}
labels: |
org.opencontainers.image.title=${{ github.event.repository.name }}
org.opencontainers.image.source=${{ github.repository }}
org.opencontainers.image.revision=${{ github.sha }}
- name: Build & Push SemVer image
uses: docker/build-push-action@v6
with:
context: .
file: ${{ env.DOCKERFILE }}
push: true
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
provenance: false