Skip to content

Commit 781d1c2

Browse files
authored
Merge pull request #75 from twz123/gh-api-image-list
Use GitHub API to list images
2 parents 5218b99 + fee7f6c commit 781d1c2

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed

.github/workflows/publish-images.yaml

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,50 @@ on:
1111
workflow_dispatch:
1212
inputs:
1313
tag:
14-
description: 'The tag to use for images. Defaults to :latest if not specified.'
14+
description: The tag to use for images. Defaults to the tag name if publishing a tag, "latest" otherwise.
1515
required: false
16-
default: 'latest'
1716

1817
jobs:
1918
prepare:
20-
runs-on: ubuntu-20.04
19+
runs-on: ubuntu-22.04
2120
outputs:
2221
tag: ${{ steps.set-tag.outputs.tag }}
23-
images: ${{ steps.set-images.outputs.images }}
22+
images: ${{ steps.list-images.outputs.images }}
2423
steps:
25-
- name: Checkout code
26-
uses: actions/checkout@v4
27-
28-
- name: Tag from dispatch
29-
if: github.event_name == 'workflow_dispatch'
30-
run: echo "tag=${{ github.event.inputs.tag || 'latest' }}" >> $GITHUB_ENV
31-
32-
- name: Tag from ref
33-
if: startsWith(github.ref, 'refs/tags/')
34-
run: echo "tag=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
35-
36-
- name: Default tag
37-
if: github.event_name != 'workflow_dispatch' && !startsWith(github.ref, 'refs/tags/')
38-
run: echo "tag=latest" >> $GITHUB_ENV
24+
- name: Set tag
25+
id: set-tag
26+
env:
27+
TAG: "${{ inputs.tag }}"
28+
run: |
29+
if [ -z "$TAG" ] && [[ "$GITHUB_REF" = refs/tags/* ]]; then
30+
TAG="${GITHUB_REF#refs/tags/}"
31+
fi
32+
echo tag="${TAG-latest}" >>"$GITHUB_OUTPUT"
3933
40-
- name: Set tag
41-
id: set-tag
42-
run: echo "tag=${tag}" >> $GITHUB_OUTPUT
43-
44-
- name: List all images
45-
if: github.event_name == 'workflow_dispatch' || startsWith(github.ref, 'refs/tags/')
46-
run: |
47-
image_dirs=$(find images -name 'Dockerfile' -exec dirname {} \; | xargs -n 1 basename | tr '\n' ' ')
48-
echo "image_dirs=$image_dirs" >> $GITHUB_ENV
49-
50-
- name: List changed images
51-
if: github.event_name != 'workflow_dispatch' && !startsWith(github.ref, 'refs/tags/')
52-
run: |
53-
image_dirs=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep 'images/.*/Dockerfile' | awk -F/ '{print $(NF-1)}' | tr '\n' ' ' | sed 's/ $//')
54-
echo "image_dirs=$image_dirs" >> $GITHUB_ENV
55-
56-
- name: Set image list
57-
id: set-images
58-
run: |
59-
image_dirs_json=$(echo "$image_dirs" | jq -R -c 'split(" ") | map(select(. != ""))')
60-
echo "images=${image_dirs_json}}" >> $GITHUB_OUTPUT
34+
- name: List images
35+
id: list-images
36+
env:
37+
BEFORE: "${{ github.event.before }}"
38+
run: |
39+
if [ "$GITHUB_EVENT_NAME" = workflow_dispatch ] || [[ "$GITHUB_REF" = refs/tags/* ]]; then
40+
gh api /repos/{owner}/{repo}/git/trees/"${GITHUB_SHA}"?recursive=1 --paginate --jq '
41+
[ .tree[]
42+
| select(.type == "blob")
43+
| .path
44+
| split("/")
45+
| select(length == 3 and .[0] == "images" and .[2] == "Dockerfile")
46+
| .[1]
47+
] | unique | "images=" + tojson
48+
' >>"$GITHUB_OUTPUT"
49+
else
50+
gh api /repos/{owner}/{repo}/compare/"${BEFORE}...${GITHUB_SHA}" --paginate --jq '
51+
[ .files[].filename
52+
| split("/")
53+
| select(length == 3 and .[0] == "images" and .[2] == "Dockerfile")
54+
| .[1]
55+
] | unique | "images=" + tojson
56+
' >>"$GITHUB_OUTPUT"
57+
fi
6158
6259
publish_images:
6360
needs: prepare

0 commit comments

Comments
 (0)