1919
2020name : build-docker-auto
2121
22+ permissions :
23+ contents : write
24+ pull-requests : write
25+
2226on :
2327 push :
2428 branches :
3236 - dev
3337 paths :
3438 - " **/Dockerfile.*" # Only trigger if a Dockerfile.* is modified in any directory
39+ types :
40+ - opened
41+ - reopened
42+ - synchronize
43+ - ready_for_review # Ensures the workflow triggers only when a draft is marked "Ready for Review"
3544
3645env :
37- suffix : ${{ github.base_ref == 'main' && github.event_name == 'pull_request' && 'main' || github.base_ref == 'dev' && github.event_name == 'pull_request' && 'dev' || 'feat' }}
38-
39- permissions :
40- contents : read
46+ suffix :
47+ ${{ github.base_ref == 'main' && github.event_name == 'pull_request' &&
48+ ' main ' || github.base_ref == 'dev' && github.event_name == 'pull_request' &&
49+ ' dev ' || 'feat' }}
4150
4251jobs :
4352 get-files :
@@ -48,22 +57,35 @@ jobs:
4857 - name : Checkout repository
4958 id : checkout
5059 uses : actions/checkout@v6
60+ with :
61+ fetch-depth : 2 # Need parent commit to diff against for HEAD~1..HEAD
62+ ref : ${{ github.head_ref || github.ref_name }}
5163
5264 - id : changed-files
5365 name : Check changed files
54- uses : knu/changed-files@v1
55- with :
56- paths : |
57- **/Dockerfile.*
66+ # Compare only the latest commit (HEAD~1..HEAD) so that subsequent pushes
67+ # to a PR do not re-trigger builds for Dockerfiles touched in earlier commits.
68+ run : |
69+ files=$(git diff --name-only HEAD~1 HEAD | grep -E '(^|/)Dockerfile\.' || true)
70+ echo "matched_files<<EOF" >> "$GITHUB_OUTPUT"
71+ echo "$files" >> "$GITHUB_OUTPUT"
72+ echo "EOF" >> "$GITHUB_OUTPUT"
73+ if [ -z "$files" ]; then
74+ echo 'matched_files_json=[]' >> "$GITHUB_OUTPUT"
75+ else
76+ json=$(echo "$files" | jq -R -s 'split("\n") | map(select(length > 0))')
77+ echo "matched_files_json=$json" >> "$GITHUB_OUTPUT"
78+ fi
5879
5980 - name : Show changed files
6081 id : matrix
6182 run : |
6283 echo "matched files:"
63- echo "${{ steps.changed-files.outputs.matched_files }}" | sed 's|^| | '
84+ echo "${{ steps.changed-files.outputs.matched_files }}" | sed 's/^/ / '
6485
6586 build-docker :
6687 needs : [get-files]
88+ if : needs.get-files.outputs.json != '[]' # Skip if no Dockerfiles changed in the latest commit
6789 strategy :
6890 matrix :
6991 file : " ${{ fromJson(needs.get-files.outputs.json) }}"
@@ -76,18 +98,10 @@ jobs:
7698 pull-requests : write
7799 steps :
78100 - uses : actions/checkout@v6
79- name : " checkout PR ${{ github.head_ref }}"
80- if : github.event_name == 'pull_request'
81- with :
82- fetch-depth : 0
83- ref : ${{ github.head_ref }} # branch name of PR
84-
85- - uses : actions/checkout@v6
86- name : " checkout push ${{ github.ref_name }}"
87- if : github.event_name == 'push'
101+ name : Checkout repository
88102 with :
89103 fetch-depth : 0
90- ref : ${{ github.ref_name }} # branch name of push
104+ ref : ${{ github.head_ref || github. ref_name }}
91105
92106 - uses : CCBR/actions/build-docker@latest
93107 with :
97111 dockerhub-token : ${{ secrets.DOCKERHUBRW_TOKEN_VK }}
98112 suffix : ${{ env.suffix }}
99113 push : true
100- ccbr-actions-version : v0.3
101114 github-token : ${{ github.token }}
102- config-file : " scripts/tool_version_commands.txt"
0 commit comments