-
Notifications
You must be signed in to change notification settings - Fork 0
87 lines (74 loc) · 2.67 KB
/
Copy pathdocker-build.yml
File metadata and controls
87 lines (74 loc) · 2.67 KB
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
name: Build and Push Multi-Arch Docker Image
on:
workflow_dispatch:
pull_request:
push:
branches:
- main
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout source
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
with:
platforms: all
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GH_PAT }}
- name: Build and Push to Docker Hub and GHCR
uses: docker/build-push-action@v5
with:
context: .
push: true
platforms: linux/amd64,linux/arm64
tags: |
${{ secrets.DOCKER_USERNAME }}/net-utils:latest
ghcr.io/${{ github.repository_owner }}/net-utils:latest
- name: Delete untagged images from GHCR
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
env:
GH_TOKEN: ${{ secrets.GH_PAT }}
PACKAGE_NAME: net-utils
OWNER: ${{ github.repository_owner }}
run: |
set -euo pipefail
function check_package() {
local path="$1"
if gh api -H "Accept: application/vnd.github+json" "$path" >/dev/null 2>&1; then
echo "$path"
return 0
fi
return 1
}
PACKAGE_API=""
if PACKAGE_API=$(check_package "/orgs/${OWNER}/packages/container/${PACKAGE_NAME}" 2>/dev/null); then
:
elif PACKAGE_API=$(check_package "/users/${OWNER}/packages/container/${PACKAGE_NAME}" 2>/dev/null); then
:
else
echo "Package not found in org or user scope, skipping cleanup."
exit 0
fi
echo "Cleaning untagged versions for package: ${PACKAGE_API}"
gh api -H "Accept: application/vnd.github+json" "${PACKAGE_API}/versions?per_page=100" --paginate --jq '.[] | select(.metadata.container.tags | length == 0) | .id' | while read -r version_id; do
if [[ -n "$version_id" ]]; then
echo "Deleting untagged version ${version_id}"
gh api -X DELETE -H "Accept: application/vnd.github+json" "${PACKAGE_API}/versions/${version_id}"
fi
done