1+ name : Build Image (TEMPLATE)
2+ on :
3+ workflow_call :
4+ inputs :
5+ image_names :
6+ required : true
7+ description : " the images to push, including registry prefix, whitespace separated"
8+ type : string
9+
10+ metadata_title :
11+ required : true
12+ description : " the title of the image"
13+ type : string
14+ metadata_description :
15+ required : true
16+ description : " the description of the image"
17+ type : string
18+
19+ tag_with_latest :
20+ default : false
21+ description : " if true, image tags will include 'latest'"
22+ type : boolean
23+ tag_with_semver :
24+ default : false
25+ description : " if true, image tags will include the version (from git tag event, without 'v' prefix)"
26+ type : boolean
27+ tag_with_sha :
28+ default : false
29+ description : " if true, image tags will include the commit SHA (both short and long)"
30+ type : boolean
31+
32+ build_file :
33+ required : true
34+ description : " path to Dockerfile"
35+ type : string
36+ build_context :
37+ required : true
38+ description : " path to build context folder"
39+ type : string
40+ build_platforms :
41+ required : true
42+ description : " docker buildx platforms to build for, whitespace separated"
43+ type : string
44+ build_registry_cache :
45+ required : true
46+ description : " an image to use as a registry-type build cache (registry + image + tag)"
47+ type : string
48+
49+ login_to_ghcr :
50+ default : false
51+ description : " if true, login to GitHub Container Registry with the GITHUB_TOKEN"
52+ type : boolean
53+ login_to_docker :
54+ default : false
55+ description : " if true, login to DockerHub using the DOCKER_USERNAME and DOCKER_PASSWORD secrets in the repository"
56+ type : boolean
57+
58+ jobs :
59+ build_image :
60+ name : Build '${{ inputs.metadata_title }}' Image
61+ runs-on : ubuntu-latest
62+ steps :
63+ # # We need to sanitize some inputs before we can use them:
64+ # # - the `build_registry_cache` must be lowercase so we can safely use
65+ # # it in `cache-from` and `cache-to` options
66+ - name : Sanitize Inputs
67+ id : sanitize_inputs
68+ env :
69+ build_registry_cache : ${{ inputs.build_registry_cache }}
70+ run : |
71+ echo "build_registry_cache=${build_registry_cache@L}" >> "$GITHUB_OUTPUT"
72+
73+ - name : Checkout
74+ uses : actions/checkout@v4
75+
76+ - name : Install QEMU
77+ uses : docker/setup-qemu-action@v3
78+
79+ - name : Install Docker Buildx
80+ uses : docker/setup-buildx-action@v3
81+
82+ - name : Login to GitHub Container Registry
83+ uses : docker/login-action@v3
84+ if : ${{ inputs.login_to_ghcr }}
85+ with :
86+ registry : ghcr.io
87+ username : ${{ github.actor }}
88+ password : ${{ secrets.GITHUB_TOKEN }}
89+
90+ - name : Login to DockerHub
91+ uses : docker/login-action@v3
92+ if : ${{ inputs.login_to_docker }}
93+ with :
94+ registry : docker.io
95+ username : ${{ secrets.DOCKER_USERNAME }}
96+ password : ${{ secrets.DOCKER_PASSWORD }}
97+
98+ - name : Generate Image Metadata
99+ id : meta
100+ uses : docker/metadata-action@v5
101+ with :
102+ images : ${{ inputs.image_names }}
103+ flavor : |
104+ latest=${{ inputs.tag_with_latest }}
105+ tags : |
106+ type=semver,priority=1000,pattern={{version}},enable=${{ inputs.tag_with_semver }}
107+ type=semver,priority=900,pattern={{major}}.{{minor}},enable=${{ inputs.tag_with_semver }}
108+ type=sha,priority=200,prefix=sha-,format=short,enable=${{ inputs.tag_with_sha }}
109+ type=sha,priority=100,prefix=sha-,format=long,enable=${{ inputs.tag_with_sha }}
110+ labels : |
111+ org.opencontainers.image.title=${{ inputs.metadata_title }}
112+ org.opencontainers.image.description=${{ inputs.metadata_description }}
113+ annotations : |
114+ org.opencontainers.image.title=${{ inputs.metadata_title }}
115+ org.opencontainers.image.description=${{ inputs.metadata_description }}
116+
117+ - name : Build and Push Image
118+ uses : docker/build-push-action@v5
119+ with :
120+ annotations : ${{ steps.meta.outputs.annotations }}
121+ cache-from : type=registry,ref=${{ steps.sanitize_inputs.outputs.build_registry_cache }}
122+ cache-to : type=registry,ref=${{ steps.sanitize_inputs.outputs.build_registry_cache }},mode=max
123+ context : ${{ inputs.build_context }}
124+ file : ${{ inputs.build_file }}
125+ labels : ${{ steps.meta.outputs.labels }}
126+ platforms : ${{ inputs.build_platforms }}
127+ push : true
128+ tags : ${{ steps.meta.outputs.tags }}
0 commit comments