Skip to content

Commit ee90710

Browse files
feat: ✨ add for loop through cmd
Signed-off-by: Andy Augustin <[email protected]>
1 parent d0d70e1 commit ee90710

File tree

5 files changed

+145
-13
lines changed

5 files changed

+145
-13
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
with:
2525
source_repo_path: AndreasAugustin/template.git
2626
is_dry_run: true
27+
is_force_push_pr: true
2728
- name: print output
2829
env:
2930
FORMER_OUTPUT_PR_BRANCH: ${{ steps.test.outputs.pr_branch }}

.github/workflows/test_all.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@ jobs:
3333
permissions:
3434
contents: write
3535
pull-requests: write
36+
call_test_steps:
37+
uses: ./.github/workflows/test_steps.yml
38+
secrets: inherit
39+
permissions:
40+
contents: write
41+
pull-requests: write

.github/workflows/test_steps.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: test-steps
2+
3+
on:
4+
push:
5+
# branches:
6+
# - "!main"
7+
# pull_request:
8+
workflow_call:
9+
workflow_dispatch:
10+
11+
jobs:
12+
test-implementation-job:
13+
runs-on: ubuntu-latest
14+
permissions:
15+
contents: write
16+
pull-requests: write
17+
steps:
18+
# To use this repository's private action, you must check out the repository
19+
- name: Checkout
20+
uses: actions/checkout@v4
21+
22+
- name: Test action step first steps
23+
uses: ./ # Uses an action in the root directory
24+
with:
25+
source_repo_path: AndreasAugustin/template.git
26+
is_dry_run: true
27+
is_force_push_pr: true
28+
steps: "prechecks,pull"
29+
30+
- name: in between step
31+
run: |
32+
echo "I can do whatever I want"
33+
git status
34+
35+
- name: Test action step next steps
36+
uses: ./ # Uses an action in the root directory
37+
id: test
38+
with:
39+
source_repo_path: AndreasAugustin/template.git
40+
is_dry_run: true
41+
is_force_push_pr: true
42+
steps: "commit,push,pr"
43+
44+
- name: print output
45+
env:
46+
FORMER_OUTPUT_PR_BRANCH: ${{ steps.test.outputs.pr_branch }}
47+
run: echo "pr_branch ${FORMER_OUTPUT_PR_BRANCH}"

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ jobs:
267267
| git_remote_pull_params | `[optional]` set remote pull parameters | `false` | `--allow-unrelated-histories --squash --strategy=recursive -X theirs` |
268268
| gpg_private_key | `[optional]` set if you want to sign commits | `false` | |
269269
| gpg_passphrase | `[optional]` set if your optionial gpg private key has a passphrase | `false` | |
270+
| steps | `[optional] add the steps you want to execute within the action` | `false` | all steps will be executed |
270271

271272
### Action Outputs
272273

@@ -423,6 +424,51 @@ If `is_dry_run` parameter is set to true then all stages modifying the github st
423424
It is possible to run a subset of the mentioned lifecycle actions.
424425
**preparation** and **github action outputs** will be run every time.
425426

427+
:warning: Advanced feature. Use with care (possibly set `is_dry_run: true` configuration parameter for testing purposes)
428+
429+
e.g.
430+
431+
```yaml
432+
# File: .github/workflows/test_steps.yml
433+
434+
on:
435+
# cronjob trigger
436+
schedule:
437+
- cron: "0 0 1 * *"
438+
# manual trigger
439+
workflow_dispatch:
440+
jobs:
441+
repo-sync:
442+
runs-on: ubuntu-latest
443+
# https://docs.github.com/en/actions/using-jobs/assigning-permissions-to-jobs
444+
permissions:
445+
contents: write
446+
pull-requests: write
447+
448+
steps:
449+
# To use this repository's private action, you must check out the repository
450+
- name: Checkout
451+
uses: actions/checkout@v4
452+
453+
- name: actions-template-sync first steps
454+
uses: AndreasAugustin/actions-template-sync@v2
455+
with:
456+
source_repo_path: <owner/repo>
457+
steps: "prechecks,pull"
458+
459+
- name: in between step
460+
run: |
461+
echo "I can do whatever I want"
462+
git status
463+
464+
- name: actions-template-sync next steps
465+
uses: AndreasAugustin/actions-template-sync@v2
466+
with:
467+
source_repo_path: <owner/repo>
468+
steps: "commit,push,pr"
469+
470+
```
471+
426472
## Lifecycle hooks
427473

428474
Different lifecycle hooks are supported. You need to enable the functionality with the option `is_allow_hooks` and set it to `true`

src/sync_template.sh

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -480,18 +480,50 @@ function arr_push_prepare_pr_create_pr() {
480480
echo "::endgroup::"
481481
}
482482

483-
declare -A arr
484-
485-
arr["prechecks"]=arr_prechecks
486-
arr["pull"]=arr_checkout_branch_and_pull
487-
arr["commit"]=arr_commit
488-
arr["push"]=arr_push
489-
arr["pr"]=arr_push_prepare_pr_create_pr
490-
491-
${arr["prechecks"]}
492-
${arr["pull"]}
493-
${arr["commit"]}
494-
${arr["push"]}
495-
${arr["pr"]}
483+
declare -A cmd_arr
484+
485+
cmd_arr["prechecks"]=arr_prechecks
486+
cmd_arr["pull"]=arr_checkout_branch_and_pull
487+
cmd_arr["commit"]=arr_commit
488+
cmd_arr["push"]=arr_push
489+
cmd_arr["pr"]=arr_push_prepare_pr_create_pr
490+
491+
if [[ -z "${STEPS}" ]]; then
492+
info "no steps provided. Default is to execute all."
493+
for key in "${!cmd_arr[@]}";
494+
do
495+
debug "execute cmd ${key}"
496+
${cmd_arr[${key}]}
497+
done
498+
else
499+
info "steps provided."
500+
readarray -t steps < <(awk -F',' '{ for( i=1; i<=NF; i++ ) print $i }' <<<"${STEPS}")
501+
# check if steps are supported
502+
not_supported_steps=""
503+
for step in "${steps[@]}";
504+
do
505+
matched=false
506+
for key in "${!cmd_arr[@]}";
507+
do
508+
debug "execute cmd ${key}"
509+
if [[ "${step}" == "${key}" ]]; then
510+
matched=true;
511+
fi
512+
done
513+
if [[ "$matched" == 'false' ]]; then
514+
not_supported_steps="${not_supported_steps} $step"
515+
fi
516+
done
517+
if [[ -z "${not_supported_steps}" ]]; then
518+
for step in "${steps[@]}";
519+
do
520+
debug "execute cmd ${step}"
521+
${cmd_arr[${step}]}
522+
done
523+
else
524+
err "following steps are not supported ${not_supported_steps}"
525+
exit 1
526+
fi
527+
fi
496528

497529
set_github_action_outputs "${PR_BRANCH}" "${TEMPLATE_GIT_HASH}"

0 commit comments

Comments
 (0)