-
Notifications
You must be signed in to change notification settings - Fork 0
165 lines (135 loc) · 4.98 KB
/
cd-release.yml
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
name: CD (Release)
on:
release:
types: [published]
workflow_dispatch:
inputs:
force_rebuild:
description: 'Force container rebuild'
required: true
type: choice
options: [yes, no]
schedule:
- cron: '30 12 * * *'
permissions:
packages: write
contents: read
env:
IMAGE_NAME: ${{ github.repository }}
DOCKER_FILE: alpine
jobs:
BuildImage:
name: Image Build & Publish
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Checkout the latest tag
run: |
rev=$(git rev-list --tags --max-count=1)
tag=$(git describe --tags "$rev")
if [[ -z "$tag" ]] ; then
echo "::error::Can't find the latest tag"
exit 1
fi
echo -e "\033[34mRev:\033[0m $rev"
echo -e "\033[34mTag:\033[0m $tag"
git checkout "$tag"
- name: Prepare metadata for build
id: metadata
run: |
rev=$(git rev-list --tags --max-count=1)
version=$(git describe --tags "$rev" | tr -d 'v')
if [[ -z "$version" ]] ; then
echo "::error::Can't find version info"
exit 1
fi
docker_file=".docker/${{env.DOCKER_FILE}}.docker"
base_image=$(grep 'FROM ' $docker_file | grep -v 'builder' | sed 's#${REGISTRY}/##' | tail -1 | cut -f2 -d' ')
if [[ -z "$base_image" ]] ; then
echo "::error::Can't extract base image info"
exit 1
fi
echo "version=$version" >> $GITHUB_OUTPUT
echo "dockerfile=$docker_file" >> $GITHUB_OUTPUT
echo "baseimage=$base_image" >> $GITHUB_OUTPUT
echo -e "\033[34mVersion:\033[0m $version"
echo -e "\033[34mDockerfile:\033[0m $docker_file"
echo -e "\033[34mBase image:\033[0m $base_image"
- name: Check if build/rebuild is required
id: build_check
run: |
if [[ "$GITHUB_EVENT_NAME" == "release" ]] ; then
echo "build=true" >> $GITHUB_OUTPUT
exit 0
fi
if [[ "${{ github.event.inputs.force_rebuild }}" == "true" ]] ; then
echo "::warning::Rebuild is required (reason: forced rebuild)"
echo "build=true" >> $GITHUB_OUTPUT
exit 0
fi
echo -e "::group::\033[34mDownloading built image…\033[0m"
if ! docker pull ghcr.io/${{env.IMAGE_NAME}}:latest ; then
echo "::error::Can't download image ghcr.io/${{env.IMAGE_NAME}}:latest"
exit 1
fi
echo "::endgroup::"
echo -e "::group::\033[34mDownloading base image…\033[0m"
if ! docker pull ${{steps.metadata.outputs.baseimage}} ; then
echo "::error::Can't download image ${{steps.metadata.outputs.baseimage}}"
exit 1
fi
echo "::endgroup::"
base_layer=$(docker inspect "${{steps.metadata.outputs.baseimage}}" | jq -r '.[0].RootFS.Layers[-1]')
if [[ -z "$base_layer" ]] ; then
echo "::error::Can't extract layers info from base image"
exit 1
fi
if ! docker inspect "ghcr.io/${{env.IMAGE_NAME}}:latest" | jq -r '.[0].RootFS.Layers' | grep -q "$base_layer" ; then
echo "::warning::Rebuild image (reason: base image rebuilt)"
echo "build=true" >> $GITHUB_OUTPUT
exit 0
fi
- name: Build and push Docker images (Docker)
if: ${{ steps.build_check.outputs.build == 'true' }}
uses: docker/build-push-action@v6
with:
push: true
context: .
file: ${{steps.metadata.outputs.dockerfile}}
build-args: |
REGISTRY=docker.io
tags: |
${{env.IMAGE_NAME}}:${{steps.metadata.outputs.version}}
${{env.IMAGE_NAME}}:latest
- name: Build and push Docker images (GHCR)
if: ${{ steps.build_check.outputs.build == 'true' }}
uses: docker/build-push-action@v6
with:
push: true
context: .
file: ${{steps.metadata.outputs.dockerfile}}
build-args: |
REGISTRY=ghcr.io
tags: |
ghcr.io/${{env.IMAGE_NAME}}:${{steps.metadata.outputs.version}}
ghcr.io/${{env.IMAGE_NAME}}:latest
- name: Show info about built Docker image
if: ${{ steps.build_check.outputs.build == 'true' }}
uses: essentialkaos/docker-info-action@v1
with:
image: ghcr.io/${{env.IMAGE_NAME}}:latest
show-labels: true