-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
184 lines (139 loc) · 4.7 KB
/
action.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
---
# yamllint disable rule:line-length
name: Build and push Docker image
description: Builds a Docker image and pushes it into a registry
inputs:
registry_address:
description: 'Docker registry address'
type: string
required: true
registry_username:
description: 'Username to authorise in registry'
type: string
required: true
registry_token:
description: 'Token to authorise in registry'
type: string
required: true
name:
description: 'Name of the Docker image (incl. repository, excl. version)'
type: string
required: true
build_args:
description: 'Additional build arguments to pass to docker build command'
type: string
required: false
default: ""
platform:
description: 'Platform to set in the manifest'
type: string
required: false
default: ""
working_directory:
description: 'Where to execute the build'
type: string
required: false
default: "./"
dockerfile:
description: 'Where Dockerfile is located'
type: string
required: false
default: "Dockerfile"
planned_version:
description: 'Version to assign to the image'
type: string
required: true
default_branch:
description: 'Default branch name that would not be added to version tag part'
type: string
required: false
default: "master"
skip_branch_suffix:
description: 'Overrides behaviour of "default_branch" parameter, forces to omit branch name in version tag'
type: boolean
required: false
default: false
fail_on_error:
description: 'Fail the pipeline on error'
type: boolean
required: true
default: false
outputs:
image_full_name:
description: "Full name of the pushed image"
value: ${{ steps.set_docker_image_id.outputs.github_image_full_name }}
runs:
using: "composite"
steps:
- name: Get trigger branch name
id: get_branch_name
shell: bash
run: |
echo "branch_name=${GITHUB_REF#refs/heads/}" >> "${GITHUB_OUTPUT}"
- name: Login to Docker registry
id: docker_registry_login
shell: bash
run: >-
echo "${{ inputs.registry_token }}" |
docker login
"${{ inputs.registry_address }}"
-u "${{ inputs.registry_username }}"
--password-stdin
- name: Define Docker image ID and full name
id: set_docker_image_id
shell: bash
run: |
if [[ "${{ steps.get_branch_name.outputs.branch_name }}" == "${{ inputs.default_branch }}" ]]; then
long_suffix=""
else
if [[ "${{ inputs.skip_branch_suffix }}" == "true" ]]; then
long_suffix=""
else
long_suffix="${{steps.get_branch_name.outputs.branch_name}}"
fi
fi
GITHUB_IMAGE_ID="$(echo "${{ inputs.name }}" | tr '[A-Z]' '[a-z]')"
GITHUB_IMAGE_FULL_NAME="${{ inputs.registry_address }}/${GITHUB_IMAGE_ID}:${{ inputs.planned_version }}${long_suffix}"
echo "::debug::GITHUB_IMAGE_ID: ${GITHUB_IMAGE_ID}"
echo "::debug::GITHUB_IMAGE_FULL_NAME: ${GITHUB_IMAGE_FULL_NAME}"
echo "github_image_id=${GITHUB_IMAGE_ID}" >> "${GITHUB_OUTPUT}"
echo "github_image_full_name=${GITHUB_IMAGE_FULL_NAME}" >> "${GITHUB_OUTPUT}"
- name: Render platform argument
id: set_platform_arg
shell: bash
run: |
if [[ ! -z "${{ inputs.platform }}" ]]; then
PLATFORM_ARG='--platform "${{ inputs.platform }}"'
fi
echo "platform_arg=${PLATFORM_ARG}" >> "${GITHUB_OUTPUT}"
- name: Render Docker build arguments
id: set_docker_build_args
shell: bash
run: |
BUILD_ARGS_COMPILED=( )
if [[ ! -z "${{ inputs.build_args }}" ]]; then
while read build_arg; do
[[ -z "${build_arg}" ]] && continue
echo "Processing build_arg: '${build_arg}'"
BUILD_ARGS_COMPILED+=( "--build-arg" "'${build_arg}'" )
done <<< "${{ inputs.build_args }}"
fi
echo "build_args_compiled=${BUILD_ARGS_COMPILED[@]}" >> "${GITHUB_OUTPUT}"
- name: Build Docker image
id: build_docker_image
shell: bash
working-directory: ${{ inputs.working_directory }}
run: >
docker build
--tag "${{ steps.set_docker_image_id.outputs.github_image_full_name }}"
--build-arg BUILD_VERSION="${{ inputs.planned_version }}"
${{ steps.set_docker_build_args.outputs.build_args_compiled }}
${{ steps.set_platform_arg.outputs.platform_arg }}
--file "${{ inputs.dockerfile }}"
.
- name: Push Docker image to registry
id: push_docker_image
shell: bash
run: |
docker push "${{ steps.set_docker_image_id.outputs.github_image_full_name }}"
...