-
Notifications
You must be signed in to change notification settings - Fork 0
128 lines (108 loc) · 4.5 KB
/
Copy pathdocker-build.yml
File metadata and controls
128 lines (108 loc) · 4.5 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
name: Build Home Assistant Addon
on:
push:
branches:
- main
release:
types: [published]
workflow_dispatch:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
submodules: recursive
fetch-depth: 0
- name: Bump dev version
if: github.ref == 'refs/heads/main'
id: bump_version
run: |
# Read current version from addon-dev config
CURRENT_VERSION=$(grep '^version:' addon-dev/config.yaml | sed 's/version: "\(.*\)"/\1/')
echo "Current version: $CURRENT_VERSION"
# Generate YY.M.N version based on current date
YEAR=$(date +%y)
MONTH=$(date +%-m)
EXPECTED_BASE="${YEAR}.${MONTH}"
# Parse current version
IFS='.' read -r CURRENT_YEAR CURRENT_MONTH CURRENT_RELEASE <<< "$CURRENT_VERSION"
# If we're in the same year/month, increment release number
if [[ "${CURRENT_YEAR}.${CURRENT_MONTH}" == "${EXPECTED_BASE}" ]]; then
NEW_RELEASE=$((CURRENT_RELEASE + 1))
else
# New month/year, start at .1
NEW_RELEASE=1
fi
NEW_VERSION="${EXPECTED_BASE}.${NEW_RELEASE}"
echo "New version: $NEW_VERSION"
# Update addon-dev/config.yaml
sed -i "s/^version: .*/version: \"${NEW_VERSION}\"/" addon-dev/config.yaml
# Output new version for later steps
echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "VERSION=$NEW_VERSION" >> $GITHUB_ENV
- name: Commit version bump
if: github.ref == 'refs/heads/main'
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "🤖 bump dev version to ${{ steps.bump_version.outputs.version }}"
file_pattern: addon-dev/config.yaml
commit_options: "--no-verify"
commit_user_name: github-actions[bot]
commit_user_email: github-actions[bot]@users.noreply.github.com
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Determine Docker tags and config
id: tags
run: |
# Convert repository name to lowercase for Docker
IMAGE_NAME_LOWER=$(echo "${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]')
if [[ "${{ github.event_name }}" == "release" ]]; then
# Extract version from release tag (remove 'v' prefix if present)
VERSION="${{ github.event.release.tag_name }}"
VERSION=${VERSION#v}
echo "tags=${{ env.REGISTRY }}/${IMAGE_NAME_LOWER}:${VERSION},${{ env.REGISTRY }}/${IMAGE_NAME_LOWER}:latest" >> $GITHUB_OUTPUT
echo "config_dir=addon" >> $GITHUB_OUTPUT
# Update addon/config.yaml with the release version
sed -i "s/^version: .*/version: \"${VERSION}\"/" addon/config.yaml
echo "Building production addon version ${VERSION}"
elif [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
# Use the bumped version from the previous step
DEV_VERSION="${{ env.VERSION }}"
echo "tags=${{ env.REGISTRY }}/${IMAGE_NAME_LOWER}:${DEV_VERSION},${{ env.REGISTRY }}/${IMAGE_NAME_LOWER}:dev" >> $GITHUB_OUTPUT
echo "config_dir=addon-dev" >> $GITHUB_OUTPUT
echo "Building dev addon version ${DEV_VERSION}"
else
echo "tags=${{ env.REGISTRY }}/${IMAGE_NAME_LOWER}:${{ github.sha }}" >> $GITHUB_OUTPUT
echo "config_dir=addon-dev" >> $GITHUB_OUTPUT
echo "Building dev addon (non-main branch)"
fi
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.tags.outputs.tags }}
cache-from: |
type=gha
type=registry,ref=${{ env.REGISTRY }}/${{ github.repository }}:buildcache
cache-to: type=gha,mode=max