-
Notifications
You must be signed in to change notification settings - Fork 5
141 lines (129 loc) · 4.89 KB
/
image-build.yaml
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
name: Image build
env:
ONLINE_REGISTER: ghcr.io
ONLINE_REGISTER_USER: ${{ github.actor }}
BUILD_PLATFORM: linux/amd64,linux/arm64
ONLINE_REGISTER_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
on:
push:
branch:
- main
workflow_call:
inputs:
ref:
required: true
type: string
tag:
required: false
type: string
push:
required: false
type: boolean
workflow_dispatch:
inputs:
image_tag:
description: 'image tag'
required: true
default: v0.2.0
permissions: write-all
jobs:
build-image:
name: Image build
runs-on: ubuntu-latest
steps:
- name: Set up Docker Buildx
uses: docker/[email protected]
- name: Get Image Push
id: tag
run: |
if ${{ inputs.ref != '' }} ; then
echo "call by workflow_call"
echo "tag=${{ inputs.ref }}" >> $GITHUB_OUTPUT
dispatch=$(egrep "^v" <<< "${{ inputs.ref }}") || echo "call by run e2e"
if [ -n "${dispatch}" ] ; then
echo "tag=main" >> $GITHUB_OUTPUT
fi
echo ::set-output name=version::${{ inputs.ref }}
echo ::set-output name=push::${{ inputs.push }}
elif ${{ inputs.image_tag != '' }} ; then
echo "call by workflow_dispatch"
echo ${{ inputs.image_tag }}
echo "version=${{ inputs.image_tag }}" >> $GITHUB_OUTPUT
echo "tag=${{ inputs.image_tag }}" >> $GITHUB_OUTPUT
echo "push=true" >> $GITHUB_OUTPUT
elif ${{ github.event_name == 'push' }} ; then
echo "trigger by push"
echo ::set-output name=tag::${{ github.sha }}
echo ::set-output name=version::"latest"
echo ::set-output name=push::true
elif ${{ github.event_name == 'pull_request_target' }} ; then
echo "trigger by pull_request_target"
echo ::set-output name=tag::${{ github.event.pull_request.head.sha }}
echo ::set-output name=version::latest
echo ::set-output name=push::false
else
echo "unexpected event: ${{ github.event_name }}"
exit 1
fi
- name: Check out code into the Go module directory
uses: actions/checkout@v4
with:
persist-credentials: false
ref: ${{ steps.tag.outputs.tag }}
- name: Getting Build Arg
id: arg
continue-on-error: false
run: |
GIT_COMMIT_VERSION=$( git show -s --format='format:%H')
GIT_COMMIT_TIME=$( git show -s --format='format:%aI')
echo ::set-output name=commitver::${GIT_COMMIT_VERSION}
echo ::set-output name=committime::${GIT_COMMIT_TIME}
- name: Login to online register
uses: docker/[email protected]
if: ${{ steps.tag.outputs.push == 'true' }}
with:
username: ${{ env.ONLINE_REGISTER_USER }}
password: ${{ env.ONLINE_REGISTER_PASSWORD }}
registry: ${{ env.ONLINE_REGISTER }}
- name: Build container image and push
if: ${{ steps.tag.outputs.push == 'true' }}
uses: docker/[email protected]
with:
context: ./
push: true
tags: ghcr.io/${{ github.repository }}/meta-plugins:${{ steps.tag.outputs.version }}
file: ./images/Dockerfile
github-token: ${{ secrets.WELAN_PAT }}
platforms: ${{ env.BUILD_PLATFORM }}
build-args: |
GIT_COMMIT_VERSION=${{ steps.arg.outputs.commitver }}
GIT_COMMIT_TIME=${{ steps.arg.outputs.committime }}
VERSION=${{ steps.tag.outputs.tag }}
- name: Build ci container image for e2e-test
if: ${{ steps.tag.outputs.push != 'true' }}
uses: docker/[email protected]
with:
context: ./
push: false
tags: ghcr.io/${{ github.repository }}/meta-plugins-ci:${{ steps.tag.outputs.version }}
file: ./images/Dockerfile
github-token: ${{ secrets.GITHUB_TOKEN }}
platforms: linux/amd64
outputs: type=docker
build-args: |
GIT_COMMIT_VERSION=${{ steps.arg.outputs.commitver }}
GIT_COMMIT_TIME=${{ steps.arg.outputs.committime }}
VERSION=${{ steps.tag.outputs.tag }}
- name: Scan Image
if: ${{ steps.tag.outputs.push != 'true' }}
run: |
docker images
docker save -o /tmp/ci-images.tar ghcr.io/${{ github.repository }}/meta-plugins-ci:${{ steps.tag.outputs.version }}
make lint_image_trivy -e IMAGE_NAME=ghcr.io/${{ github.repository }}/meta-plugins-ci:${{ steps.tag.outputs.version }}
- name: Upload artifact e2e image tar
if: ${{ steps.tag.outputs.push != 'true' }}
uses: actions/[email protected]
with:
name: image-e2e-tar
path: /tmp/ci-images.tar
retention-days: 1