|
11 | 11 | workflow_dispatch: |
12 | 12 | inputs: |
13 | 13 | 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. |
15 | 15 | required: false |
16 | | - default: 'latest' |
17 | 16 |
|
18 | 17 | jobs: |
19 | 18 | prepare: |
20 | | - runs-on: ubuntu-20.04 |
| 19 | + runs-on: ubuntu-22.04 |
21 | 20 | outputs: |
22 | 21 | tag: ${{ steps.set-tag.outputs.tag }} |
23 | | - images: ${{ steps.set-images.outputs.images }} |
| 22 | + images: ${{ steps.list-images.outputs.images }} |
24 | 23 | 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" |
39 | 33 |
|
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 |
61 | 58 |
|
62 | 59 | publish_images: |
63 | 60 | needs: prepare |
|
0 commit comments