Skip to content

Commit

Permalink
Merge pull request #5 from a7543/master
Browse files Browse the repository at this point in the history
Add function to parse and filter Docker tags by date
  • Loading branch information
taoky authored Jan 22, 2024
2 parents db37321 + cc61fcb commit 7e9fc75
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ jobs:
with:
fetch-depth: 50 # the same value as Travis CI

- name: Install jq
run: sudo apt-get install -y jq

- name: Run build script
run: ./build.sh
env:
Expand Down
40 changes: 26 additions & 14 deletions common.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
#!/bin/bash

one_year_ago="$(date -d "1 year ago" +%s)"
parse-tags-js() {
is_empty=true
page_url="$(jq -r '.next' <<< "$tags_js")"
for result in $(jq -c '.results[]' <<< "$tags_js"); do
if ! date="$(date -d "$(jq -r '.last_updated' <<< "$result")" +%s)"; then
return 1
fi
if ! name="$(jq -r '.name' <<< "$result")"; then
return 1
fi
if (( date > one_year_ago )); then
echo "$name"
is_empty=false
fi
done
}
docker-tags(){
image=library/$1
tags_js=$(curl -sSL "https://registry.hub.docker.com/v2/repositories/${image}/tags/")
if [ $? -ne 0 ]; then
echo "curl $image failed" >&2
return 1
fi
grep -oP '(?<="name":").+?(?=")' <(echo "$tags_js")
while next_page=$(grep -oP '(?<="next":").+?(?=")' <(echo "$tags_js") | sed 's/\\u0026/\&/' | xargs)
is_empty=false
image="library/$1"
page_url="https://registry.hub.docker.com/v2/repositories/${image}/tags/"
while [[ -n "$page_url" && "$page_url" != "null" && "$is_empty" != "true" ]]
do
if [[ -z "$next_page" || "$next_page" == "null" ]]; then
break
if ! tags_js="$(curl -fsSL "$page_url")"; then
echo "curl $page_url failed" >&2
return 1
fi
tags_js=$(curl -sSL "$next_page")
if [ $? -ne 0 ]; then
echo "curl $next_page failed" >&2
if ! parse-tags-js; then
echo "parse json failed" >&2
return 1
fi
grep -oP '(?<="name":").+?(?=")' <(echo "$tags_js")
done
}

0 comments on commit 7e9fc75

Please sign in to comment.