-
Notifications
You must be signed in to change notification settings - Fork 0
85 lines (74 loc) · 2.74 KB
/
build-and-push-image.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
name: Build and Push Image
on:
workflow_call:
outputs:
branch:
description: "The name of the branch"
value: ${{ jobs.build-and-push.outputs.branch }}
image:
description: "The fully qualified built image"
value: ${{ jobs.build-and-push.outputs.image }}
tag:
description: "The tag component of the image"
value: ${{ jobs.build-and-push.outputs.tag }}
env:
REGISTRY: ghcr.io
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
# This is used to complete the identity challenge
# with sigstore/fulcio when running outside of PRs.
id-token: write
outputs:
branch: ${{ steps.branch-name.outputs.current_branch }}
image: ${{ steps.image-tag.outputs.image }}
tag: ${{ steps.image-tag.outputs.tag }}
steps:
- name: Checkout
uses: actions/checkout@v3
# github.repository as <account>/<repo>
- name: Capture image name
run: |
echo "IMAGE_NAME=${GITHUB_REPOSITORY@L}" >>${GITHUB_ENV}
- name: Get branch name
id: branch-name
uses: tj-actions/branch-names@v7
# Set up BuildKit Docker container builder to be able to build
# multi-platform images and export cache
# https://github.com/docker/setup-buildx-action
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Set image name and tag
id: image-tag
run: |
tag="${{ steps.branch-name.outputs.current_branch }}.${{ github.run_id }}"
echo "image=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:$tag" >> $GITHUB_OUTPUT
echo "tag=$tag" >> $GITHUB_OUTPUT
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Build and push Docker image with Buildx
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0
with:
context: .
push: true
tags: ${{ steps.image-tag.outputs.image }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max