-
Notifications
You must be signed in to change notification settings - Fork 1
140 lines (121 loc) · 4.18 KB
/
Copy pathdocker.yml
File metadata and controls
140 lines (121 loc) · 4.18 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
name: Build, Push & Release
on:
workflow_dispatch:
inputs:
version:
description: 'Version to build (leave empty for automatic versioning)'
required: false
type: string
push:
branches: [ main ]
tags: [ 'v*' ]
pull_request:
branches: [ main ]
env:
REGISTRY: docker.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to DockerHub
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=tag
type=raw,value=latest,enable={{is_default_branch}}
type=raw,value={{date 'YYYYMMDD'}}-{{sha}},enable={{is_default_branch}}
- name: Calculate version
id: version
run: |
if [[ -n "${{ github.event.inputs.version }}" ]]; then
# Use manually specified version
VERSION="${{ github.event.inputs.version }}"
elif [[ $GITHUB_REF == refs/tags/* ]]; then
# If this is a tag, use the tag version
VERSION=${GITHUB_REF#refs/tags/}
VERSION=${VERSION#v}
else
# Try to get the latest tag, if none exists, start from v0.0.0
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
LATEST_TAG=${LATEST_TAG#v}
# Get the commit count from the beginning of the repository
# This ensures we always have a valid number even with no tags
COMMITS_SINCE_TAG=$(git rev-list --count HEAD)
# Split version into major and minor
MAJOR=$(echo $LATEST_TAG | cut -d. -f1)
MINOR=$(echo $LATEST_TAG | cut -d. -f2)
# If we couldn't parse the version properly, default to 0.0
if [[ -z "$MAJOR" ]] || [[ -z "$MINOR" ]]; then
MAJOR=0
MINOR=0
fi
# Calculate new version
VERSION="${MAJOR}.${COMMITS_SINCE_TAG}"
fi
echo "version=${VERSION}" >> $GITHUB_OUTPUT
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.version.outputs.version }}
${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
BUILDKIT_INLINE_CACHE=1
provenance: false
sbom: false
release:
if: startsWith(github.ref, 'refs/tags/v')
needs: build-and-push
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check for release notes file
id: notes
run: |
if [[ -f RELEASE_NOTES.md ]]; then
echo "has_notes=true" >> $GITHUB_OUTPUT
else
echo "has_notes=false" >> $GITHUB_OUTPUT
fi
- name: Create GitHub Release (with custom notes)
if: steps.notes.outputs.has_notes == 'true'
uses: softprops/action-gh-release@v2
with:
body_path: RELEASE_NOTES.md
generate_release_notes: false
- name: Create GitHub Release (auto-generated notes)
if: steps.notes.outputs.has_notes == 'false'
uses: softprops/action-gh-release@v2
with:
generate_release_notes: true