-
Notifications
You must be signed in to change notification settings - Fork 0
180 lines (156 loc) · 6.12 KB
/
dev-build.yml
File metadata and controls
180 lines (156 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
name: Dev Build
on:
push:
branches: [main]
paths-ignore:
- CHANGELOG.md
- '**.md'
- LICENSE
- '**/*.example.yml'
- '**/*.example.yaml'
pull_request:
paths-ignore:
- CHANGELOG.md
- '**.md'
- LICENSE
- '**/*.example.yml'
- '**/*.example.yaml'
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
check-build-needed:
name: Check if build is needed
runs-on: ubuntu-latest
outputs:
should-build: ${{ steps.check.outputs.should-build }}
steps:
- name: Checkout code
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Check if build should be skipped
id: check
run: |
# Get the current commit SHA
CURRENT_SHA="${{ github.sha }}"
# Get commit message from git log
COMMIT_MSG=$(git log -1 --format="%s" "$CURRENT_SHA" 2>/dev/null || echo "")
echo "Checking commit: $CURRENT_SHA"
echo "Commit message: $COMMIT_MSG"
# Never build dev images for release commits (they get built by release workflow)
if echo "$COMMIT_MSG" | grep -qiE "^(chore|release|version).*(release|version|changelog)"; then
# Check if commit message explicitly says to skip CI
if echo "$COMMIT_MSG" | grep -qiE "[skip ci]|[ci skip]|skip ci"; then
echo "Skip reason: [skip ci] detected in commit message"
echo "should-build=false" >> $GITHUB_OUTPUT
exit 0
fi
echo "Skip reason: Release commit detected - dev builds only run for non-release commits"
echo "should-build=false" >> $GITHUB_OUTPUT
exit 0
fi
# For PR events, check changed files against base branch
if [ "${{ github.event_name }}" = "pull_request" ]; then
BASE_SHA="${{ github.event.pull_request.base.sha }}"
HEAD_SHA="${{ github.event.pull_request.head.sha }}"
CHANGED_FILES=$(git diff --name-only "$BASE_SHA" "$HEAD_SHA")
# For push events, check changed files against base branch
else
BASE_SHA="${{ github.event.before }}"
HEAD_SHA="${{ github.event.after }}"
if [ -z "$BASE_SHA" ] || [ "$BASE_SHA" = "0000000000000000000000000000000000" ]; then
# Initial commit or no base, check all files
CHANGED_FILES=$(git ls-tree -r --name-only HEAD)
else
# Get list of changed files
CHANGED_FILES=$(git diff --name-only "$BASE_SHA" "$HEAD_SHA")
fi
fi
if [ -z "$CHANGED_FILES" ]; then
echo "No changed files detected, skipping build"
echo "should-build=false" >> $GITHUB_OUTPUT
exit 0
fi
# Documentation file patterns to ignore
DOC_PATTERNS="^README\.md$|^CHANGELOG\.md$|^docs/|\.md$|^LICENSE"
# Check if all changed files are documentation
NON_DOC_FILES=$(echo "$CHANGED_FILES" | grep -vE "$DOC_PATTERNS" || true)
if [ -z "$NON_DOC_FILES" ]; then
echo "Skip reason: Only documentation files changed"
echo "Changed files:"
echo "$CHANGED_FILES" | sed 's/^/ - /'
echo "should-build=false" >> $GITHUB_OUTPUT
else
echo "Build needed: Non-documentation files changed"
echo "Non-doc files:"
echo "$NON_DOC_FILES" | sed 's/^/ - /'
echo "should-build=true" >> $GITHUB_OUTPUT
fi
dev-build:
name: Development Build
runs-on: ubuntu-latest
needs: [check-build-needed]
if: needs.check-build-needed.outputs.should-build == 'true'
permissions:
contents: read
packages: write
steps:
- name: Checkout code
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GHCR
if: github.event_name == 'push'
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Generate dev tag
id: meta
run: |
# Create a semver-compatible dev tag with commits since last release and commit SHA
SHORT_SHA=$(echo ${{ github.sha }} | cut -c1-7)
# Get the last release tag (excluding pre-release tags)
LAST_TAG=$(git describe --tags --abbrev=0 --match="v[0-9]*.[0-9]*.[0-9]*" 2>/dev/null || echo "")
if [ -z "$LAST_TAG" ]; then
# No release tags found, use 0.0.0 and count all commits
VERSION="0.0.0"
COMMIT_COUNT=$(git rev-list --count HEAD)
else
# Extract version from tag (remove 'v' prefix)
VERSION=${LAST_TAG#v}
# Count commits since last release
COMMIT_COUNT=$(git rev-list --count ${LAST_TAG}..HEAD)
fi
DEV_TAG="v${VERSION}-dev.${COMMIT_COUNT}.${SHORT_SHA}"
echo "dev_tag=${DEV_TAG}" >> $GITHUB_OUTPUT
echo "Last release tag: ${LAST_TAG:-'none'}"
echo "Base version: ${VERSION}"
echo "Commits since last release: ${COMMIT_COUNT}"
echo "Generated dev tag: ${DEV_TAG}"
- name: Extract metadata
id: docker_meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
${{ steps.meta.outputs.dev_tag }}
dev
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name == 'push' }}
tags: ${{ steps.docker_meta.outputs.tags }}
labels: ${{ steps.docker_meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=min
build-args: |
VERSION=${{ steps.meta.outputs.dev_tag }}
COMMIT=${{ github.sha }}
BUILD_DATE=${{ github.event.head_commit.timestamp || github.event.pull_request.head.repo.updated_at }}