-
Notifications
You must be signed in to change notification settings - Fork 0
96 lines (85 loc) · 3.5 KB
/
docker-description.yaml
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
name: Docker Hub Description
on:
push:
branches:
- main
paths:
- README.md
- .github/workflows/docker-description.yml
env:
DOCKER_REGISTRY: "https://hub.docker.com/v2"
DESCRIPTION_LIMIT: 100
jobs:
update-docker-hub:
runs-on: thevickypedia-lite
steps:
- uses: actions/checkout@v4
- name: Set Repository
run: |
docker_repository=$(echo "${{ github.event.repository.name }}" | tr '[:upper:]' '[:lower:]')
echo "DOCKER_REPOSITORY=${{ secrets.DOCKER_USERNAME }}/${docker_repository}" >> $GITHUB_ENV
shell: bash
- name: Fetch API Token
run: |
payload=$(jq -n \
--arg username "${{ secrets.DOCKER_USERNAME }}" \
--arg password "${{ secrets.DOCKER_PASSWORD }}" \
'{username: $username, password: $password}')
token=$(curl -s -X POST "${{ env.DOCKER_REGISTRY }}/users/login/" \
-H "Content-Type: application/json" \
-d "$payload" | jq -r '.token')
if [[ -n "${token}" ]]; then
echo "::debug title=Token Retriever::Retrieved token successfully"
echo "API_TOKEN=${token}" >> $GITHUB_ENV
else
echo "::error title=Token Retriever::Failed to get auth token"
exit 1
fi
shell: bash
- name: Get Description
run: |
warn="Description exceeds DockerHub's limit and has been truncated to ${{ env.DESCRIPTION_LIMIT }} characters."
description="${{ github.event.repository.description }}"
description_length=${#description}
if [[ "$description_length" -gt "${{ env.DESCRIPTION_LIMIT }}" ]]; then
echo "::warning title=Description Too Long::${warn}"
shortened_description="${description:0:97}..."
else
shortened_description="$description"
fi
echo "SHORT_DESCRIPTION=${shortened_description}" >> $GITHUB_ENV
shell: bash
- name: Update description
run: |
full_description="$(cat README.md)"
payload=$(jq -n \
--arg description "${{ env.SHORT_DESCRIPTION }}" \
--arg full_description "$full_description" \
'{description: $description, full_description: $full_description}')
response=$(curl -s -o /tmp/desc -w "%{http_code}" -X PATCH \
"${{ env.DOCKER_REGISTRY }}/repositories/${{ env.DOCKER_REPOSITORY }}/" \
-H "Authorization: Bearer ${{ env.API_TOKEN }}" \
-H "Content-Type: application/json" \
-d "$payload")
status_code="${response: -3}"
if [[ "${status_code}" -eq 200 ]]; then
echo "::notice title=Updater::Updated description successfully"
exit 0
elif [[ -f "/tmp/desc" ]]; then
echo "::error title=Updater::Failed to update description"
response_payload="$(cat /tmp/desc)"
reason=$(echo "${response_payload}" | jq '.message')
info=$(echo "${response_payload}" | jq '.errinfo')
if [[ "$reason" != "null" ]]; then
echo "::error title=Updater::[${status_code}]: $reason"
else
echo "::error title=Updater::[${status_code}]: $(cat /tmp/desc)"
fi
if [[ "$info" != "null" ]]; then
echo "::error title=Updater::${info}"
fi
else
echo "::error title=Updater::Failed to update description - ${status_code}"
fi
exit 1
shell: bash