-
Notifications
You must be signed in to change notification settings - Fork 77
75 lines (64 loc) · 2.71 KB
/
docker-dev.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
name: Publish Dev Docker Image
on:
push:
branches:
- master
jobs:
push_dev_to_registry:
name: Push Development Docker image to Docker Hub
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Extract Commit Hash
id: extract_hash
run: echo "::set-output name=hash::$(git rev-parse --short HEAD)"
- name: Get WebCalendar Version
run: echo "WEBCALENDAR_VERSION=$(./bump_version.sh -p)" >> $GITHUB_ENV
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./docker/Dockerfile-php8
push: true
tags: |
${{ secrets.DOCKER_HUB_USERNAME }}/webcalendar:${{ env.WEBCALENDAR_VERSION }}-${{ steps.extract_hash.outputs.hash }}-php8-apache-dev
${{ secrets.DOCKER_HUB_USERNAME }}/webcalendar:dev-php8-apache
${{ secrets.DOCKER_HUB_USERNAME }}/webcalendar:dev
- name: Cleanup Old Images
env:
DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }}
DOCKER_HUB_REPO: ${{ secrets.DOCKER_HUB_USERNAME }}/webcalendar
DOCKER_HUB_ACCESS_TOKEN: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} # Updated line
KEEP_LAST_N_IMAGES: 5
run: |
# Get list of tags
RESPONSE=$(curl -s -u $DOCKER_HUB_USERNAME:$DOCKER_HUB_ACCESS_TOKEN -o /tmp/response.json -w "%{http_code}" https://hub.docker.com/v2/repositories/$DOCKER_HUB_REPO/tags/?page_size=100)
# Check HTTP Status Code
if [ "$RESPONSE" -ne 200 ]; then
echo "Failed to fetch tags, HTTP Response code is $RESPONSE"
cat /tmp/response.json
exit 1
fi
# Check for null JSON Response and Extract Tags
TAGS=$(jq -r '.results? | if type=="array" then .[].name else empty end' /tmp/response.json)
# Filter tags with "dev"
DEV_TAGS=$(echo "$TAGS" | grep dev)
# Sort tags and delete all except for the most recent N
COUNTER=0
for TAG in $(echo $DEV_TAGS | tr " " "\n" | sort -V)
do
if [ $COUNTER -ge $KEEP_LAST_N_IMAGES ]
then
echo "Deleting tag $TAG"
curl -X DELETE -u $DOCKER_HUB_USERNAME:$DOCKER_HUB_ACCESS_TOKEN https://hub.docker.com/v2/repositories/$DOCKER_HUB_REPO/tags/$TAG/
else
echo "Keeping tag $TAG"
fi
COUNTER=$((COUNTER + 1))
done