-
Notifications
You must be signed in to change notification settings - Fork 1
121 lines (100 loc) · 3.81 KB
/
build-dockerfile.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
# Copyright (c) 2023 Roberto Rossini (roberros@uio.no)
# SPDX-License-Identifier: MIT
name: Build Dockerfile
on:
workflow_call:
inputs:
dockerfile-glob:
required: true
type: string
outputs:
image:
description: "Docker image ID"
value: ${{ jobs.build-dockerfile.outputs.imageid }}
jobs:
collect-metadata:
name: Collect Metadata
runs-on: ubuntu-latest
outputs:
dockerfile: ${{ steps.metadata.outputs.dockerfile }}
name: ${{ steps.metadata.outputs.name }}
version: ${{ steps.metadata.outputs.version }}
push: ${{ steps.metadata.outputs.push }}
steps:
- name: Checkout
uses: actions/checkout@v3
if: ${{ github.event_name != 'pull_request' }}
with:
ref: ${{ github.sha }}
- name: Checkout (PR)
uses: actions/checkout@v3
if: ${{ github.event_name == 'pull_request' }}
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Collect metadata
id: metadata
run: |
dockerfile=(${{ inputs.dockerfile-glob }})
dockerfile_name="$(basename "${dockerfile[*]}" .Dockerfile)"
name="$(echo "$dockerfile_name" | sed -E 's/(.*)__.*/\1/')"
version="$(echo "$dockerfile_name" | sed -E 's/.*__v(.*)/\1/')"
push=${{ github.event_name != 'pull_request' }}
if test ${{ github.event_name }} == 'pull_request' && \
test ${{ github.event.pull_request.author_association }} == 'COLLABORATOR' && \
git log --format=%B -n 1 | grep -qF '[ci push]';
then
push=true
fi
echo "dockerfile=${dockerfile[*]}" >> $GITHUB_OUTPUT
echo "name=$name" >> $GITHUB_OUTPUT
echo "version=$version" >> $GITHUB_OUTPUT
echo "push=$push" >> $GITHUB_OUTPUT
build-dockerfile:
name: Build ${{ needs.collect-metadata.outputs.name }} Dockerfile
runs-on: ubuntu-latest
needs: [collect-metadata]
outputs:
imageid: ${{ steps.docker-build-outputs.imageid }}
permissions:
contents: read
packages: write
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Generate tags
id: generate-tags
working-directory: ${{ github.workspace }}/containers
run: |
base_tag="ghcr.io/${{ github.repository }}/${{ needs.collect-metadata.outputs.name }}"
tags="$base_tag:${{ needs.collect-metadata.outputs.version }}"
tags+=",$base_tag:latest"
tags+=",$base_tag:$(git rev-parse --short HEAD)"
echo "Tags: $tags"
echo "tags=$tags" >> $GITHUB_OUTPUT
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
# with:
# platforms: linux/amd64,linux/arm64
# - name: Set up QEMU
# uses: docker/setup-qemu-action@v2
# with:
# platforms: arm64
- name: Build Docker image and push to registries
id: docker-build
uses: docker/build-push-action@v4
with:
push: ${{ needs.collect-metadata.outputs.push }}
cache-from: type=gha,scope=${{ needs.collect-metadata.outputs.dockerfile }}
cache-to: type=gha,mode=min,scope=${{ needs.collect-metadata.outputs.dockerfile }}
tags: ${{ steps.generate-tags.outputs.tags }}
file: ${{ needs.collect-metadata.outputs.dockerfile }}
platforms: linux/amd64 #,linux/arm64
build-args: |
CONTAINER_VERSION=${{ needs.collect-metadata.outputs.version }}
CONTAINER_TITLE=${{ needs.collect-metadata.outputs.name }}