diff --git a/.github/workflows/test_all.yml b/.github/workflows/test_all.yml index d044ac5..9360d81 100644 --- a/.github/workflows/test_all.yml +++ b/.github/workflows/test_all.yml @@ -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 diff --git a/.github/workflows/test_steps.yml b/.github/workflows/test_steps.yml new file mode 100644 index 0000000..77f881f --- /dev/null +++ b/.github/workflows/test_steps.yml @@ -0,0 +1,45 @@ +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 + 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 + 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}" diff --git a/README.md b/README.md index 23b1669..654a22b 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: + steps: "prechecks,pull" + + - 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: + steps: "commit,push,pr" + +``` + ## Lifecycle hooks Different lifecycle hooks are supported. You need to enable the functionality with the option `is_allow_hooks` and set it to `true` diff --git a/src/sync_template.sh b/src/sync_template.sh index e5e9213..ce058dc 100755 --- a/src/sync_template.sh +++ b/src/sync_template.sh @@ -480,18 +480,50 @@ function arr_push_prepare_pr_create_pr() { echo "::endgroup::" } -declare -A 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 - -${arr["prechecks"]} -${arr["pull"]} -${arr["commit"]} -${arr["push"]} -${arr["pr"]} +declare -A cmd_arr + +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_push_prepare_pr_create_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 "follwoing steps are not supported ${not_supported_steps}" + exit 1 + fi +fi set_github_action_outputs "${PR_BRANCH}" "${TEMPLATE_GIT_HASH}"