-
Notifications
You must be signed in to change notification settings - Fork 0
121 lines (108 loc) · 4.3 KB
/
docker.yml
File metadata and controls
121 lines (108 loc) · 4.3 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
name: Build and Push Docker Image
on:
push:
branches:
- main
- 'release/v*'
paths:
- 'frontend/**'
- 'backend/**'
- 'Dockerfile'
- 'deploy/**'
- 'nginx/**'
- '.github/workflows/docker.yml'
tags:
- 'v*'
workflow_dispatch: # Manual trigger from Actions tab
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: write # Required for tag/release creation
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Need full history for tag checks
- name: Get version from package.json
id: version
run: echo "version=$(node -p "require('./frontend/package.json').version")" >> $GITHUB_OUTPUT
- name: Check if release branch
id: release_branch
run: |
if [[ "${{ github.ref }}" == refs/heads/release/v* ]]; then
echo "is_release=true" >> $GITHUB_OUTPUT
echo "branch_version=${GITHUB_REF#refs/heads/release/v}" >> $GITHUB_OUTPUT
else
echo "is_release=false" >> $GITHUB_OUTPUT
fi
- name: Check if tag exists
id: check_tag
run: |
git fetch --tags
if git rev-parse "v${{ steps.version.outputs.version }}" >/dev/null 2>&1; then
echo "exists=true" >> $GITHUB_OUTPUT
else
echo "exists=false" >> $GITHUB_OUTPUT
fi
- name: Extract changelog for version
id: changelog
if: github.ref == 'refs/heads/main' && steps.check_tag.outputs.exists == 'false'
run: |
chmod +x .github/scripts/extract-changelog.sh
CHANGELOG=$(.github/scripts/extract-changelog.sh "${{ steps.version.outputs.version }}" 2>/dev/null || echo "")
if [ -n "$CHANGELOG" ]; then
echo "found=true" >> $GITHUB_OUTPUT
# Use delimiter for multiline output
echo "content<<EOF" >> $GITHUB_OUTPUT
echo "$CHANGELOG" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
else
echo "found=false" >> $GITHUB_OUTPUT
fi
- name: Create tag and release
if: github.ref == 'refs/heads/main' && steps.check_tag.outputs.exists == 'false'
uses: softprops/action-gh-release@v2
with:
tag_name: v${{ steps.version.outputs.version }}
body: ${{ steps.changelog.outputs.found == 'true' && steps.changelog.outputs.content || '' }}
generate_release_notes: ${{ steps.changelog.outputs.found != 'true' }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels)
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
# Release branch: version-rc tag (e.g., 0.3.0-rc) for pre-release testing
type=raw,value=${{ steps.release_branch.outputs.branch_version }}-rc,enable=${{ steps.release_branch.outputs.is_release == 'true' }}
# Release to main: latest and version tags
type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/v') || (github.ref == 'refs/heads/main' && steps.check_tag.outputs.exists == 'false') }}
type=raw,value=${{ steps.version.outputs.version }},enable=${{ github.ref == 'refs/heads/main' && steps.check_tag.outputs.exists == 'false' }}
# Semver tags for tagged releases
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
# Always include SHA for traceability
type=sha,prefix=
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
APP_VERSION=${{ steps.version.outputs.version }}
cache-from: type=gha
cache-to: type=gha,mode=max