Skip to content

Commit 28a284c

Browse files
committed
Improve workflow
1 parent d6db565 commit 28a284c

2 files changed

Lines changed: 84 additions & 140 deletions

File tree

.github/workflows/auto-release.yml

Lines changed: 84 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,115 @@
1-
name: Auto Release on package.json version bump
1+
name: Release and Docker Push
22

33
on:
44
push:
55
branches: [v3]
66
paths:
77
- package.json
8+
workflow_dispatch:
89

910
permissions:
1011
contents: write
12+
packages: write
13+
14+
env:
15+
REGISTRY: ghcr.io
16+
IMAGE_NAME: ${{ github.repository }}
1117

1218
jobs:
13-
release:
19+
deploy:
1420
runs-on: ubuntu-latest
15-
1621
steps:
1722
- name: Checkout
1823
uses: actions/checkout@v4
1924
with:
2025
fetch-depth: 0
2126

22-
- name: Read current version
23-
id: current
27+
- name: Read versions
28+
id: versions
2429
run: |
25-
VERSION=$(node -p "require('./package.json').version")
26-
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
30+
set -euo pipefail
31+
CUR_VERSION=$(node -p "require('./package.json').version")
32+
echo "current=$CUR_VERSION" >> "$GITHUB_OUTPUT"
33+
echo "LOG: Current version found in package.json: $CUR_VERSION"
2734
28-
- name: Read previous version (from previous commit)
29-
id: previous
30-
run: |
3135
PREV_VERSION=$(git show HEAD~1:package.json 2>/dev/null | node -p "JSON.parse(require('fs').readFileSync(0,'utf8')).version" || echo "")
32-
echo "version=$PREV_VERSION" >> "$GITHUB_OUTPUT"
36+
echo "previous=$PREV_VERSION" >> "$GITHUB_OUTPUT"
37+
if [ -z "$PREV_VERSION" ]; then
38+
echo "LOG: No previous version found in git history"
39+
else
40+
echo "LOG: Previous version found in git history: $PREV_VERSION"
41+
fi
3342
34-
- name: Check if version increased
43+
- name: Version comparison
3544
id: check
3645
run: |
37-
CUR="${{ steps.current.outputs.version }}"
38-
PREV="${{ steps.previous.outputs.version }}"
46+
set -euo pipefail
47+
echo "LOG: Running semantic version comparison"
48+
node - <<'NODE' >> "$GITHUB_OUTPUT"
49+
const cur = process.env.CUR;
50+
const prev = process.env.PREV;
3951
40-
echo "Current: $CUR"
41-
echo "Previous: $PREV"
52+
function parse(v) {
53+
if (!v) return null;
54+
const m = String(v).trim().replace(/^v/, "").match(/^(\d+)\.(\d+)\.(\d+)(?:[-+].*)?$/);
55+
return m ? m.slice(1, 4).map(Number) : null;
56+
}
4257
43-
# if previous doesn't exist (first commit containing package.json), skip
44-
if [ -z "$PREV" ]; then
45-
echo "should_release=false" >> "$GITHUB_OUTPUT"
46-
exit 0
47-
fi
58+
function gt(a, b) {
59+
for (let i = 0; i < 3; i++) {
60+
if (a[i] !== b[i]) return a[i] > b[i];
61+
}
62+
return false;
63+
}
64+
65+
const a = parse(cur);
66+
const b = parse(prev);
67+
68+
let should = false;
69+
if (!prev && a) {
70+
should = true;
71+
} else if (a && b) {
72+
should = gt(a, b);
73+
}
74+
75+
console.log(`should_release=${should}`);
76+
NODE
77+
env:
78+
CUR: ${{ steps.versions.outputs.current }}
79+
PREV: ${{ steps.versions.outputs.previous }}
80+
81+
- name: Logging decision
82+
run: |
83+
echo "LOG: Release decision is ${{ steps.check.outputs.should_release }}"
4884
49-
# Compare semver using node's semver package if available; fallback to simple inequality
50-
node -e "
51-
const cur='${CUR}';
52-
const prev='${PREV}';
53-
let should = cur !== prev;
54-
try {
55-
const semver = require('semver');
56-
should = semver.gt(cur, prev);
57-
} catch (_) {}
58-
console.log('should_release=' + should);
59-
" >> "$GITHUB_OUTPUT"
60-
61-
- name: Stop if no bump
62-
if: steps.check.outputs.should_release != 'true'
63-
run: echo "No version increase detected."
64-
65-
- name: Create tag + GitHub Release
85+
- name: GitHub Release
6686
if: steps.check.outputs.should_release == 'true'
6787
uses: softprops/action-gh-release@v2
6888
with:
69-
tag_name: v${{ steps.current.outputs.version }}
70-
name: v${{ steps.current.outputs.version }}
89+
tag_name: v${{ steps.versions.outputs.current }}
90+
name: v${{ steps.versions.outputs.current }}
7191
generate_release_notes: true
92+
93+
- name: Set up Docker Buildx
94+
if: steps.check.outputs.should_release == 'true'
95+
uses: docker/setup-buildx-action@v3
96+
97+
- name: Log in to GHCR
98+
if: steps.check.outputs.should_release == 'true'
99+
uses: docker/login-action@v3
100+
with:
101+
registry: ${{ env.REGISTRY }}
102+
username: ${{ github.actor }}
103+
password: ${{ secrets.GITHUB_TOKEN }}
104+
105+
- name: Build and push Docker image
106+
if: steps.check.outputs.should_release == 'true'
107+
uses: docker/build-push-action@v5
108+
with:
109+
context: .
110+
push: true
111+
tags: |
112+
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.versions.outputs.current }}
113+
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
114+
cache-from: type=gha
115+
cache-to: type=gha,mode=max

.github/workflows/docker-release.yml

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)