From ab4153bb937a2d7ad996f0693977cb943443e8b3 Mon Sep 17 00:00:00 2001 From: andy Augustin Date: Sat, 2 Mar 2024 11:41:03 +0100 Subject: [PATCH] fix(#476): :bug: output variable edge cases (#479) * fix(#476): :bug: issue with empty output variable * fix: :bug: --- README.md | 7 +++++++ src/sync_template.sh | 24 +++++++++++++----------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index af21a9c7..3ab5bf8e 100644 --- a/README.md +++ b/README.md @@ -262,6 +262,13 @@ jobs: | ------ | ----------- | | pr_branch | The name of the branch used for the pull request | +**Remarks** Please consider following edge cases + +* **pr_branch** + * If PR branch already exists (e.g. after a 2nd run) the action won't update the branch but will still output the branch name + * If the remote repository already contains the source repository changes the action will exit and the output variable will be undefined + * If there are no changes the action will exit and the output variable will be undefined + ### Docker There are docker images available. Please checkout [How to use docker](docs/DOCKER.md) for details. diff --git a/src/sync_template.sh b/src/sync_template.sh index 8f5f815a..a474dd38 100644 --- a/src/sync_template.sh +++ b/src/sync_template.sh @@ -48,11 +48,24 @@ debug "new Git HASH ${NEW_TEMPLATE_GIT_HASH}" echo "::group::Check new changes" +function set_github_action_outputs() { + echo "::group::set gh action outputs" + if [[ -z "${GITHUB_RUN_ID}" ]]; then + info "env var 'GITHUB_RUN_ID' is empty -> no github action workflow" + else + # https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter + echo "pr_branch=${NEW_BRANCH}" >> "$GITHUB_OUTPUT" + fi + echo "::endgroup::" +} + + function check_branch_remote_existing() { git ls-remote --exit-code --heads origin "${NEW_BRANCH}" || BRANCH_DOES_NOT_EXIST=true if [[ "${BRANCH_DOES_NOT_EXIST}" != true ]]; then warn "Git branch '${NEW_BRANCH}' exists in the remote repository" + set_github_action_outputs exit 0 fi } @@ -240,15 +253,4 @@ fi echo "::endgroup::" -function set_github_action_outputs() { - echo "::group::set gh action outputs" - if [[ -z "${GITHUB_RUN_ID}" ]]; then - info "env var 'GITHUB_RUN_ID' is empty -> no github action workflow" - else - # https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter - echo "pr_branch=${NEW_BRANCH}" >> "$GITHUB_OUTPUT" - fi - echo "::endgroup::" -} - set_github_action_outputs