-
Notifications
You must be signed in to change notification settings - Fork 0
139 lines (123 loc) · 5.13 KB
/
workflow-build-push-container-github-registry.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
---
name: Reusable workflow to build and push docker container to github container registry
on:
workflow_call:
inputs:
container-name:
required: true
type: string
tag:
required: true
type: string
registry:
required: true
type: string
dockerfile-folder-path:
required: false
type: string
jobs:
build-push-image:
runs-on: ubuntu-latest
steps:
- name: Check Out Repo
uses: actions/checkout@v4
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v3
- name: Log in to the github container registry (GCR)
uses: docker/login-action@7ca345011ac4304463197fac0e56eab1bc7e6af0
with:
registry: ${{ inputs.registry }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Cache Docker layers
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: ${{ runner.os }}-buildx
- name: Extract branch name + remove undesired characters
shell: bash
id: extract_branch_name
run: |
BRANCH_NAME="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
CLEAN_BRANCH_NAME=$(echo "$BRANCH_NAME" | sed 's/[\/\\]/-/g')
echo "clean_branch_name=$CLEAN_BRANCH_NAME" >> $GITHUB_ENV
echo "::set-output name=clean_branch_name::$CLEAN_BRANCH_NAME"
- name: Build and push (3 tags | commit number, pr number and branch name) (unmerged)
id: docker_build_and_tag_unmerged
if: github.event.pull_request.merged == false
uses: docker/build-push-action@v6
with:
context: ${{ inputs.dockerfile-folder-path || '.' }}
push: true
platforms: linux/amd64,linux/arm64
tags: |
${{ inputs.registry }}/${{ inputs.container-name }}:${{ github.event.number || github.run_number }}
${{ inputs.registry }}/${{ inputs.container-name }}:${{ inputs.tag }}
${{ inputs.registry }}/${{ inputs.container-name }}:${{ steps.extract_branch_name.outputs.clean_branch_name || 'latest' }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new
- name: Build and push (2 tags | commit number and branch name) (merged)
id: docker_build_and_tag_merged
if: github.event.pull_request.merged == true
uses: docker/build-push-action@v6
with:
context: ${{ inputs.dockerfile-folder-path || '.' }}
push: true
platforms: linux/amd64,linux/arm64
tags: |
${{ inputs.registry }}/${{ inputs.container-name }}:${{ inputs.tag }}
${{ inputs.registry }}/${{ inputs.container-name }}:${{ github.event.pull_request.base.ref }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new
- name: Refresh Cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
- name: Image digest (latest)
if: github.event.pull_request.merged == false
run: echo ${{ steps.docker_build_and_tag_unmerged.outputs.digest }}
- name: Image digest (latest)
if: github.event.pull_request.merged == true
run: echo ${{ steps.docker_build_and_tag_merged.outputs.digest }}
remove-old-image:
runs-on: ubuntu-latest
needs: build-push-image
steps:
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.8
- name: Install the remove-previous-image from github.com/ai-cfia/devops inside the user-site
run: >
python -m pip install --user \
git+https://$USER:$USER_TOKEN@github.com/ai-cfia/devops.git@main
env:
USER: ${{ secrets.USER }}
USER_TOKEN: ${{ secrets.USER_TOKEN }}
- name: Access user site-packages
run: |
USER_SITE=$(python -m site --user-site)
echo "Path to site-packages is $USER_SITE"
echo "USER_SITE=$USER_SITE" >> $GITHUB_ENV
- name: Delete the previous image (unmerged pull request)
if: github.event.pull_request.merged == false
run: python $USER_SITE/remove-previous-image/remove_previous_image.py
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REGISTRY: ${{ inputs.registry }}
CONTAINER_NAME: ${{ inputs.container-name }}
UNIQUE_TAG: ${{ github.event.number }}
USER: ${{ github.actor }}
CURRENT_COMMIT: ${{ inputs.tag }}
- name: Delete the previous image (merged pull request)
if: github.event.pull_request.merged == true
run: python $USER_SITE/remove-previous-image/remove_previous_image.py
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REGISTRY: ${{ inputs.registry }}
CONTAINER_NAME: ${{ inputs.container-name }}
UNIQUE_TAG: ${{ github.event.pull_request.base.ref }}
USER: ${{ github.actor }}
CURRENT_COMMIT: ${{ inputs.tag }}