Skip to content

Commit

Permalink
Merge pull request #75 from twz123/gh-api-image-list
Browse files Browse the repository at this point in the history
Use GitHub API to list images
  • Loading branch information
kke authored Feb 9, 2024
2 parents 5218b99 + fee7f6c commit 781d1c2
Showing 1 changed file with 36 additions and 39 deletions.
75 changes: 36 additions & 39 deletions .github/workflows/publish-images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,50 @@ on:
workflow_dispatch:
inputs:
tag:
description: 'The tag to use for images. Defaults to :latest if not specified.'
description: The tag to use for images. Defaults to the tag name if publishing a tag, "latest" otherwise.
required: false
default: 'latest'

jobs:
prepare:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
outputs:
tag: ${{ steps.set-tag.outputs.tag }}
images: ${{ steps.set-images.outputs.images }}
images: ${{ steps.list-images.outputs.images }}
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Tag from dispatch
if: github.event_name == 'workflow_dispatch'
run: echo "tag=${{ github.event.inputs.tag || 'latest' }}" >> $GITHUB_ENV

- name: Tag from ref
if: startsWith(github.ref, 'refs/tags/')
run: echo "tag=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV

- name: Default tag
if: github.event_name != 'workflow_dispatch' && !startsWith(github.ref, 'refs/tags/')
run: echo "tag=latest" >> $GITHUB_ENV
- name: Set tag
id: set-tag
env:
TAG: "${{ inputs.tag }}"
run: |
if [ -z "$TAG" ] && [[ "$GITHUB_REF" = refs/tags/* ]]; then
TAG="${GITHUB_REF#refs/tags/}"
fi
echo tag="${TAG-latest}" >>"$GITHUB_OUTPUT"
- name: Set tag
id: set-tag
run: echo "tag=${tag}" >> $GITHUB_OUTPUT

- name: List all images
if: github.event_name == 'workflow_dispatch' || startsWith(github.ref, 'refs/tags/')
run: |
image_dirs=$(find images -name 'Dockerfile' -exec dirname {} \; | xargs -n 1 basename | tr '\n' ' ')
echo "image_dirs=$image_dirs" >> $GITHUB_ENV
- name: List changed images
if: github.event_name != 'workflow_dispatch' && !startsWith(github.ref, 'refs/tags/')
run: |
image_dirs=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep 'images/.*/Dockerfile' | awk -F/ '{print $(NF-1)}' | tr '\n' ' ' | sed 's/ $//')
echo "image_dirs=$image_dirs" >> $GITHUB_ENV
- name: Set image list
id: set-images
run: |
image_dirs_json=$(echo "$image_dirs" | jq -R -c 'split(" ") | map(select(. != ""))')
echo "images=${image_dirs_json}}" >> $GITHUB_OUTPUT
- name: List images
id: list-images
env:
BEFORE: "${{ github.event.before }}"
run: |
if [ "$GITHUB_EVENT_NAME" = workflow_dispatch ] || [[ "$GITHUB_REF" = refs/tags/* ]]; then
gh api /repos/{owner}/{repo}/git/trees/"${GITHUB_SHA}"?recursive=1 --paginate --jq '
[ .tree[]
| select(.type == "blob")
| .path
| split("/")
| select(length == 3 and .[0] == "images" and .[2] == "Dockerfile")
| .[1]
] | unique | "images=" + tojson
' >>"$GITHUB_OUTPUT"
else
gh api /repos/{owner}/{repo}/compare/"${BEFORE}...${GITHUB_SHA}" --paginate --jq '
[ .files[].filename
| split("/")
| select(length == 3 and .[0] == "images" and .[2] == "Dockerfile")
| .[1]
] | unique | "images=" + tojson
' >>"$GITHUB_OUTPUT"
fi
publish_images:
needs: prepare
Expand Down

0 comments on commit 781d1c2

Please sign in to comment.