-
Notifications
You must be signed in to change notification settings - Fork 1
132 lines (121 loc) · 4.43 KB
/
docker_build.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
name: Docker Build and Push
on:
push:
paths-ignore:
- 'README*'
- 'LICENSE'
- '.editorconfig'
- '.github/**'
- '.gitignore'
- 'makefile'
- 'config.example.yaml'
- 'script/**'
- '.vscode/**'
workflow_call:
inputs:
tags:
description: 'Docker image tag list, multiple tags are separated by comma, e.g. 0.1.0,latest'
required: true
type: string
default: 'latest'
workflow_dispatch:
inputs:
tags:
required: true
type: string
default: 'latest'
jobs:
setup:
name: Setup
runs-on: ubuntu-latest
if: github.ref_name == 'main' || github.ref_type == 'tag'|| github.event_name == 'workflow_dispatch' || github.event_name == 'workflow_call'
outputs:
TAG_NAME: ${{ steps.set_tag_name.outputs.TAG_NAME }}
BUILD_TRIGGER_DESCRIPTION: ${{ steps.set_tag_name.outputs.BUILD_TRIGGER_DESCRIPTION }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.ref }}
- name: Set tag name
id: set_tag_name
run: |
replace_comma_with_tag() {
new_tags=""
for tag in $(echo $1 | tr ',' ' '); do
new_tags="$new_tags ${{ github.repository }}:$tag"
done
echo $new_tags | tr ' ' ','
}
{
if [ -n "${{ github.event.inputs.tags }}" ]; then
echo "TAG_NAME=$(replace_comma_with_tag ${{ github.event.inputs.tags }})"
echo "BUILD_TRIGGER_DESCRIPTION=Triggered via workflow_dispatch with tag ${{ github.event.inputs.tags }}"
elif [ -n "${{ inputs.tags }}" ]; then
echo "TAG_NAME=$(replace_comma_with_tag ${{ inputs.tags }})"
echo "BUILD_TRIGGER_DESCRIPTION=Triggered via workflow_call with tag ${{ inputs.tags }}"
elif [ "${{ github.ref_type }}" == 'tag' ]; then
echo "TAG_NAME=${{ github.repository }}:${{ github.ref_name }}"
echo "BUILD_TRIGGER_DESCRIPTION=Triggered via tag ${{ github.ref_name }}"
elif [ "${{ github.event_name }}" == 'push' ]; then
echo "TAG_NAME=${{ github.repository }}:$(git rev-parse --short HEAD)"
echo "BUILD_TRIGGER_DESCRIPTION=Triggered via push to ${{ github.ref_name }}"
elif [ "${{ github.event.pull_request.merged }}" == 'true' ]; then
echo "TAG_NAME=${{ github.repository }}:$(echo ${{ github.event.pull_request.head.sha }} | cut -c1-7)"
echo "BUILD_TRIGGER_DESCRIPTION=Triggered via pull request ${{ github.event.pull_request.number }}"
else
echo "TAG_NAME="
fi
} >> $GITHUB_OUTPUT
docker-release:
name: Publish Docker images
needs: [setup]
runs-on: ubuntu-latest
timeout-minutes: 20
if: needs.setup.outputs.TAG_NAME != ''
steps:
- name: Build trigger description
run: |
echo "The build trigger description is ${{ needs.setup.outputs.BUILD_TRIGGER_DESCRIPTION }}"
echo "The publish docker image tag is ${{ needs.setup.outputs.TAG_NAME }}"
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.ref }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Cache go-build
uses: actions/cache@v4
with:
path: go-build
key: go-build-${{ hashFiles('**/go.sum') }}
- name: Cache go-pkg
uses: actions/cache@v4
with:
path: go-pkg
key: go-pkg-${{ hashFiles('**/go.sum') }}
- name: Inject go-build into Docker
uses: reproducible-containers/[email protected]
with:
cache-source: go-build
cache-target: /root/.cache/go-build
- name: Inject go-pkg into Docker
uses: reproducible-containers/[email protected]
with:
cache-source: go-pkg
cache-target: /go/pkg
- name: Docker build and push with tag name
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ needs.setup.outputs.TAG_NAME }}
platforms: |
linux/amd64
linux/arm64
linux/arm/v7