Skip to content

Commit

Permalink
feat: ✨ add for loop through cmd
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Augustin <dev@andreas-augustin.org>
  • Loading branch information
AndreasAugustin committed Apr 23, 2024
1 parent d0d70e1 commit 0000d2b
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 13 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
with:
source_repo_path: AndreasAugustin/template.git
is_dry_run: true
is_force_push_pr: true
- name: print output
env:
FORMER_OUTPUT_PR_BRANCH: ${{ steps.test.outputs.pr_branch }}
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/test_all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ jobs:
permissions:
contents: write
pull-requests: write
call_test_steps:
uses: ./.github/workflows/test_steps.yml
secrets: inherit
permissions:
contents: write
pull-requests: write
47 changes: 47 additions & 0 deletions .github/workflows/test_steps.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: test-steps

on:
push:
# branches:
# - "!main"
# pull_request:
workflow_call:
workflow_dispatch:

jobs:
test-implementation-job:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
# To use this repository's private action, you must check out the repository
- name: Checkout
uses: actions/checkout@v4

- name: Test action step first steps
uses: ./ # Uses an action in the root directory
with:
source_repo_path: AndreasAugustin/template.git
is_dry_run: true
is_force_push_pr: true
steps: "prechecks,pull"

- name: in between step
run: |
echo "I can do whatever I want"
git status
- name: Test action step next steps
uses: ./ # Uses an action in the root directory
id: test
with:
source_repo_path: AndreasAugustin/template.git
is_dry_run: true
is_force_push_pr: true
steps: "commit,push,pr"

- name: print output
env:
FORMER_OUTPUT_PR_BRANCH: ${{ steps.test.outputs.pr_branch }}
run: echo "pr_branch ${FORMER_OUTPUT_PR_BRANCH}"
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ jobs:
| git_remote_pull_params | `[optional]` set remote pull parameters | `false` | `--allow-unrelated-histories --squash --strategy=recursive -X theirs` |
| gpg_private_key | `[optional]` set if you want to sign commits | `false` | |
| gpg_passphrase | `[optional]` set if your optionial gpg private key has a passphrase | `false` | |
| steps | `[optional] add the steps you want to execute within the action` | `false` | all steps will be executed |

### Action Outputs

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

:warning: Advanced feature. Use with care (possibly set `is_dry_run: true` configuration parameter for testing purposes)

e.g.

```yaml
# File: .github/workflows/test_steps.yml
on:
# cronjob trigger
schedule:
- cron: "0 0 1 * *"
# manual trigger
workflow_dispatch:
jobs:
repo-sync:
runs-on: ubuntu-latest
# https://docs.github.com/en/actions/using-jobs/assigning-permissions-to-jobs
permissions:
contents: write
pull-requests: write
steps:
# To use this repository's private action, you must check out the repository
- name: Checkout
uses: actions/checkout@v4
- name: actions-template-sync first steps
uses: AndreasAugustin/actions-template-sync@v2
with:
source_repo_path: <owner/repo>
steps: "prechecks,pull" # order matters
- name: in between step
run: |
echo "I can do whatever I want"
git status
- name: actions-template-sync next steps
uses: AndreasAugustin/actions-template-sync@v2
with:
source_repo_path: <owner/repo>
steps: "commit,push,pr" # order matters
```

## Lifecycle hooks

Different lifecycle hooks are supported. You need to enable the functionality with the option `is_allow_hooks` and set it to `true`
Expand Down
58 changes: 45 additions & 13 deletions src/sync_template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ function arr_push() {
echo "::endgroup::"
}

function arr_push_prepare_pr_create_pr() {
info "push_prepare_pr_create_pr"
function arr_prepare_pr_create_pr() {
info "prepare_pr_create_pr"
if [ "$IS_DRY_RUN" == "true" ]; then
warn "dry_run option is set to on. skipping labels check, cleanup older PRs, push and create pr"
return 0
Expand Down Expand Up @@ -480,18 +480,50 @@ function arr_push_prepare_pr_create_pr() {
echo "::endgroup::"
}

declare -A arr
declare -A cmd_arr

arr["prechecks"]=arr_prechecks
arr["pull"]=arr_checkout_branch_and_pull
arr["commit"]=arr_commit
arr["push"]=arr_push
arr["pr"]=arr_push_prepare_pr_create_pr
cmd_arr["prechecks"]=arr_prechecks
cmd_arr["pull"]=arr_checkout_branch_and_pull
cmd_arr["commit"]=arr_commit
cmd_arr["push"]=arr_push
cmd_arr["pr"]=arr_prepare_pr_create_pr

${arr["prechecks"]}
${arr["pull"]}
${arr["commit"]}
${arr["push"]}
${arr["pr"]}
if [[ -z "${STEPS}" ]]; then
info "no steps provided. Default is to execute all."
for key in "${!cmd_arr[@]}";
do
debug "execute cmd ${key}"
${cmd_arr[${key}]}
done
else
info "steps provided."
readarray -t steps < <(awk -F',' '{ for( i=1; i<=NF; i++ ) print $i }' <<<"${STEPS}")
# check if steps are supported
not_supported_steps=""
for step in "${steps[@]}";
do
matched=false
for key in "${!cmd_arr[@]}";
do
debug "execute cmd ${key}"
if [[ "${step}" == "${key}" ]]; then
matched=true;
fi
done
if [[ "$matched" == 'false' ]]; then
not_supported_steps="${not_supported_steps} $step"
fi
done
if [[ -z "${not_supported_steps}" ]]; then
for step in "${steps[@]}";
do
debug "execute cmd ${step}"
${cmd_arr[${step}]}
done
else
err "following steps are not supported ${not_supported_steps}"
exit 1
fi
fi

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

0 comments on commit 0000d2b

Please sign in to comment.