-
Notifications
You must be signed in to change notification settings - Fork 0
160 lines (140 loc) · 4.12 KB
/
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
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
name: Build Docker image
on:
workflow_call:
inputs:
prefix:
required: true
type: string
image:
required: false
type: string
version:
required: false
type: string
environment:
required: false
type: string
outputs:
version:
value: ${{ jobs.prepare.outputs.version }}
version_full:
value: ${{ jobs.prepare.outputs.version_full }}
image_name:
value: ${{ jobs.prepare.outputs.image_name }}
image_url:
value: ${{ jobs.prepare.outputs.image_url }}
image_full:
value: ${{ jobs.prepare.outputs.image_full }}
built:
value: ${{ jobs.build.result != 'skipped' }}
workflow_dispatch:
inputs:
prefix:
required: true
type: string
image:
required: false
type: string
version:
required: false
type: string
environment:
required: false
type: string
repository_dispatch:
inputs:
prefix:
required: true
type: string
image:
required: false
type: string
version:
required: false
type: string
environment:
required: false
type: string
env:
CONTAINER_REGISTRY: ghcr.io
concurrency: build
jobs:
prepare:
name: Prepare build
runs-on: ubuntu-latest
outputs:
version: ${{ steps.vars.outputs.version }}
version_full: ${{ steps.vars.outputs.version_full }}
image_name: ${{ steps.vars.outputs.image_name }}
image_url: ${{ steps.vars.outputs.image_url }}
image_full: ${{ steps.vars.outputs.image_full }}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Compute variables
id: vars
run: |
if [ -z "${{ inputs.version }}" ]; then
branch=`git branch --show-current`
sha_short=`git rev-parse --short HEAD`
version="$branch-$sha_short"
else
version="${{ inputs.version }}"
fi
version_full="${{ inputs.prefix }}$version-$(date +%s)"
image_name="${{ inputs.image || github.repository }}"
image_url="${{ env.CONTAINER_REGISTRY }}/${{ inputs.image || github.repository }}"
image_full="$image_url:$version_full"
echo "::set-output name=version::$version"
echo "::set-output name=version_full::$version_full"
echo "::set-output name=image_name::$image_name"
echo "::set-output name=image_url::$image_url"
echo "::set-output name=image_full::$image_full"
retag:
needs: prepare
name: Re-tag existing Docker image
runs-on: ubuntu-latest
environment: ${{ inputs.environment }}
outputs:
status: ${{ steps.push.outcome }}
steps:
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ${{ env.CONTAINER_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push new tag
id: push
uses: akhilerm/[email protected]
continue-on-error: true
with:
src: ${{ needs.prepare.outputs.image_url }}:commit-${{ github.sha }}
dst: |
${{ needs.prepare.outputs.image_url }}:${{ inputs.prefix }}latest
${{ needs.prepare.outputs.image_full }}
build:
needs:
- prepare
- retag
name: Build and publish Docker image to registry
runs-on: ubuntu-latest
if: needs.retag.outputs.status != 'success'
environment: ${{ inputs.environment }}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ${{ env.CONTAINER_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push image
uses: docker/build-push-action@v2
with:
push: true
tags: |
${{ needs.prepare.outputs.image_url }}:${{ inputs.prefix }}latest
${{ needs.prepare.outputs.image_url }}:commit-${{ github.sha }}
${{ needs.prepare.outputs.image_full }}