-
Notifications
You must be signed in to change notification settings - Fork 219
109 lines (93 loc) · 3.17 KB
/
dockerhub-push.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
name: Build Docker image and push to DockerHub
on:
push:
branches:
- master
tags:
- v*
jobs:
build-and-push:
runs-on: self-hosted-amd64-1cpu
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Import secrets
uses: hashicorp/vault-action@v2
id: secrets
with:
exportEnv: false
url: ${{ secrets.VAULT_URL }}
role: ${{ secrets.VAULT_ROLE }}
method: kubernetes
path: kubernetes-ci
secrets: |
kv-gitlab-ci/data/github/shared/dockerhub-creds user | DOCKERHUB_USER ;
kv-gitlab-ci/data/github/shared/dockerhub-creds password | DOCKERHUB_PASSWORD ;
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ steps.secrets.outputs.DOCKERHUB_USER }}
password: ${{ steps.secrets.outputs.DOCKERHUB_PASSWORD }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: wallarm/gotestwaf
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=raw,value=edge,enable={{is_default_branch}}
- name: Build and push
uses: docker/build-push-action@v3
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
clean-old-cache:
needs: build-and-push
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Delete old cached docker layers
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -eu -o pipefail
LAST_WORKFLOW_TIME=$(
gh run list \
--json workflowName,startedAt \
--jq ".[] | select( .workflowName == \"${{ github.workflow }}\") | .startedAt" \
| head -n 1
)
echo "Time of the last running '${{ github.workflow }}' workflow: $LAST_WORKFLOW_TIME"
while true; do
OLD_CACHE_IDS=$(
gh api \
-H "Accept: application/vnd.github+json" \
--jq ".actions_caches[] | select(.last_accessed_at < \"$LAST_WORKFLOW_TIME\") | .id" \
/repos/wallarm/gotestwaf/actions/caches \
| tr '\n' ' '
)
if [ -z "$OLD_CACHE_IDS" ]; then
echo "Done"
break
fi
echo "ID of caches to delete: $OLD_CACHE_IDS"
for cache_id in $OLD_CACHE_IDS; do
gh api \
--method DELETE \
-H "Accept: application/vnd.github+json" \
/repos/wallarm/gotestwaf/actions/caches/$cache_id
done
done