|
| 1 | +# .github/workflows/ci.yml |
1 | 2 | name: CI (Auto-tag + Build & Push Docker on SemVer) |
2 | 3 |
|
3 | 4 | on: |
4 | 5 | push: |
5 | | - branches: [ "main" ] |
6 | | - tags: [ "v*.*.*", "*.*.*" ] |
| 6 | + branches: [ "main" ] # main pushes can create a tag (no image push) |
| 7 | + tags: [ "v*.*.*", "*.*.*" ] # tag pushes build & push the SemVer image |
7 | 8 | workflow_dispatch: {} |
8 | 9 |
|
9 | 10 | env: |
10 | 11 | REGISTRY: docker.io |
11 | 12 | IMAGE_NAME: ${{ secrets.DOCKERHUB_USERNAME }}/gabs-redis-langcache |
12 | 13 | DOCKERFILE: ./Dockerfile |
13 | 14 |
|
| 15 | +# We need write so the job can create a tag via GitHub API |
14 | 16 | permissions: |
15 | | - contents: write # <-- gives GITHUB_TOKEN push access to create tags |
| 17 | + contents: write |
16 | 18 |
|
17 | 19 | jobs: |
18 | | - # 1️⃣ main branch: bump + tag if '#release' present |
| 20 | + # 1) MAIN PUSH: if any commit message includes #release, create next SemVer tag (no Docker build here) |
19 | 21 | auto-release: |
20 | | - if: ${{ github.ref == 'refs/heads/main' && contains(join(github.event.commits.*.message, ' '), '#release') }} |
| 22 | + if: ${{ github.ref == 'refs/heads/main' }} |
21 | 23 | runs-on: ubuntu-latest |
22 | 24 | steps: |
23 | | - - uses: actions/checkout@v4 |
| 25 | + - name: Checkout (full history for tags) |
| 26 | + uses: actions/checkout@v4 |
24 | 27 | with: |
25 | 28 | fetch-depth: 0 |
26 | 29 |
|
27 | | - - name: Bump patch version and create tag |
| 30 | + - name: Decide if this push requests a release |
| 31 | + id: decide |
| 32 | + run: | |
| 33 | + MSGS="${{ join(github.event.commits.*.message, ' | ') }}" |
| 34 | + echo "Commit messages: $MSGS" |
| 35 | + if echo "$MSGS" | grep -q '#release'; then |
| 36 | + echo "release=yes" >> $GITHUB_OUTPUT |
| 37 | + else |
| 38 | + echo "release=no" >> $GITHUB_OUTPUT |
| 39 | + fi |
| 40 | +
|
| 41 | + - name: Compute next patch tag (vX.Y.Z -> vX.Y.(Z+1)) |
| 42 | + id: bump |
| 43 | + if: steps.decide.outputs.release == 'yes' |
| 44 | + shell: bash |
28 | 45 | run: | |
29 | 46 | set -e |
30 | | - LAST=$(git tag -l 'v*' --sort=-v:refname | head -n1) |
| 47 | + LAST=$(git tag -l 'v*.*.*' --sort=-v:refname | head -n1) |
31 | 48 | [ -z "$LAST" ] && LAST="v0.0.0" |
32 | 49 | VER=${LAST#v} |
33 | 50 | IFS='.' read -r MA MI PA <<<"$VER" |
34 | 51 | NEW_TAG="v$MA.$MI.$((PA+1))" |
35 | | - echo "Creating $NEW_TAG" |
36 | | - git config user.name "github-actions[bot]" |
37 | | - git config user.email "github-actions[bot]@users.noreply.github.com" |
38 | | - git tag -a "$NEW_TAG" -m "release $NEW_TAG" |
39 | | - git push origin "$NEW_TAG" |
| 52 | + echo "NEW_TAG=$NEW_TAG" >> $GITHUB_ENV |
| 53 | + echo "new_tag=$NEW_TAG" >> $GITHUB_OUTPUT |
| 54 | + echo "Last tag: $LAST -> Next tag: $NEW_TAG" |
| 55 | +
|
| 56 | + - name: Create git tag via GitHub API |
| 57 | + if: steps.decide.outputs.release == 'yes' |
| 58 | + uses: actions/github-script@v7 |
| 59 | + with: |
| 60 | + script: | |
| 61 | + const newTag = process.env.NEW_TAG; // from $GITHUB_ENV |
| 62 | + core.info(`Creating tag ${newTag} at ${context.sha}`); |
| 63 | + await github.rest.git.createRef({ |
| 64 | + owner: context.repo.owner, |
| 65 | + repo: context.repo.repo, |
| 66 | + ref: `refs/tags/${newTag}`, |
| 67 | + sha: context.sha |
| 68 | + }); |
| 69 | + env: |
| 70 | + NEW_TAG: ${{ env.NEW_TAG }} |
40 | 71 |
|
41 | | - # 2️⃣ tag events: build + push ONLY semver tags |
| 72 | + # 2) TAG PUSH: ONLY build & push the SemVer image |
42 | 73 | build-and-push: |
43 | 74 | if: startsWith(github.ref, 'refs/tags/') |
44 | 75 | runs-on: ubuntu-latest |
45 | 76 | steps: |
46 | | - - uses: actions/checkout@v4 |
47 | | - - uses: docker/setup-qemu-action@v3 |
48 | | - - uses: docker/setup-buildx-action@v3 |
| 77 | + - name: Checkout |
| 78 | + uses: actions/checkout@v4 |
| 79 | + |
| 80 | + - name: Set up QEMU |
| 81 | + uses: docker/setup-qemu-action@v3 |
| 82 | + |
| 83 | + - name: Set up Buildx |
| 84 | + uses: docker/setup-buildx-action@v3 |
49 | 85 |
|
50 | 86 | - name: Log in to Docker Hub |
51 | 87 | uses: docker/login-action@v3 |
|
54 | 90 | username: ${{ secrets.DOCKERHUB_USERNAME }} |
55 | 91 | password: ${{ secrets.DOCKERHUB_TOKEN }} |
56 | 92 |
|
57 | | - - name: Extract Docker metadata |
| 93 | + - name: Extract Docker metadata (SemVer only) |
58 | 94 | id: meta |
59 | 95 | uses: docker/metadata-action@v5 |
60 | 96 | with: |
|
0 commit comments