-
Notifications
You must be signed in to change notification settings - Fork 3
115 lines (104 loc) · 3.58 KB
/
publish-docker-image.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
name: Build and publish Docker Image
on:
workflow_call:
inputs:
context_path:
required: true
type: string
version_file_name:
required: true
type: string
apps_repo:
required: true
type: string
version_file_directory:
description: "Directory where version file with image ref is stored"
required: false
default: "./versions"
type: string
outputs:
image_url:
value: ${{ jobs.build.outputs.image_url }}
secrets:
PAT_REPO:
required: true
env:
REGISTRY: ghcr.io
CONTEXT_PATH: ${{ inputs.context_path }}
APPS_REPO: ${{ inputs.apps_repo }}
BRANCH: ${{ github.head_ref || github.ref_name }}
jobs:
build:
name: Build container image
runs-on: ubuntu-latest
outputs:
image_url: ${{ steps.set_output.outputs.image_url }}
permissions:
contents: read
packages: write
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check for changes in context path
id: check_changes
run: |
if git diff --quiet HEAD^ HEAD -- ${{ env.CONTEXT_PATH }}; then
echo "No changes in ${{ env.CONTEXT_PATH }}. Skipping build."
echo "::set-output name=skip_build::true"
else
echo "::set-output name=skip_build::false"
fi
- name: Login to Github Container Registry
if: steps.check_changes.outputs.skip_build == 'false'
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Build and push Docker image
if: steps.check_changes.outputs.skip_build == 'false'
id: build-docker
uses: docker/build-push-action@v5
with:
file: ${{ env.CONTEXT_PATH }}/Dockerfile
context: ${{ env.CONTEXT_PATH }}
push: ${{ !github.event.pull_request.draft }}
tags: ${{ env.REGISTRY }}/${{ github.repository }}:latest
- name: Checkout the repository
if: steps.check_changes.outputs.skip_build == 'false'
uses: actions/checkout@v3
with:
repository: kartverket/${{ env.APPS_REPO }}
ref: main
token: ${{ secrets.PAT_REPO }}
- name: Create file
if: steps.check_changes.outputs.skip_build == 'false'
run: |
setup_directory () {
DIRECTORY=${{ inputs.version_file_directory }}
touch $DIRECTORY/${{ inputs.version_file_name }}
echo "\"${{ env.REGISTRY }}/${{ github.repository }}@${{ steps.build-docker.outputs.digest }}\"" > $DIRECTORY/${{ inputs.version_file_name }}
}
if [[ "${{ env.BRANCH }}" == "main" ]]; then
setup_directory
fi
- name: Commit and Push Changes
if: steps.check_changes.outputs.skip_build == 'false'
run: |
git config --global user.email "[email protected]"
git config --global user.name "DASK CI"
git add .
if git diff --staged --quiet; then
echo "No changes to commit."
else
git commit -m "Update ${{ inputs.version_file_name }}"
git push
fi
- name: Set output with build values
if: steps.check_changes.outputs.skip_build == 'false'
id: set_output
run: |
echo "image_url=${{ env.REGISTRY }}/${{ github.repository }}@${{ steps.build-docker.outputs.digest }}" >> $GITHUB_OUTPUT