diff --git a/README.md b/README.md index 10c604d..595f5ad 100644 --- a/README.md +++ b/README.md @@ -239,7 +239,7 @@ jobs: | source_repo_ssh_private_key | `[optional]` private ssh key for the source repository. [see](#private-template-repository) | `false` | | | pr_branch_name_prefix | `[optional]` the prefix of branches created by this action | `false` | `chore/template_sync` | | pr_title | `[optional]` the title of PRs opened by this action. Must be already created. | `false` | `upstream merge template repository` | -| pr_body | `[optional]` the body of PRs opened by this action. | `false` | `Merge ${SOURCE_REPO_PATH} ${TEMPLATE_GIT_HASH}` | +| pr_body | `[optional]` the body of PRs opened by this action. | `false` | `Merge ${SOURCE_REPO} ${TEMPLATE_GIT_HASH}` | | pr_labels | `[optional]` comma separated list. [pull request labels][pr-labels]. | `false` | `sync_template` | | pr_reviewers | `[optional]` comma separated list of pull request reviewers. | `false` | | | pr_commit_msg | `[optional]` commit message in the created pull request | `false` | `chore(template): merge template changes :up:` | @@ -265,6 +265,7 @@ jobs: | output | description | | ------ | ----------- | | pr_branch | The name of the branch used for the pull request | +| template_git_hash | The template source repository git hash | **Remarks** Please consider following edge cases @@ -394,15 +395,14 @@ or you set the hooks input parameter within the action definition with a related The following hooks are supported (please check [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) for a better understanding of the lifecycles). -* `install` is executed after the container has started and after reading and setting up the environment. * `prepull` is executed before the code is pulled from the source repository * `precommit` is executed before the code is commited * `prepush` is executed before the push is executed, right after the commit * `precleanup` is executed before older PRs targeting the same branch are closed * `prepr` is executed before the PR is done -**Remark** The underlying OS is defined by an Alpine container. -E.q. for the installation phase you need to use commands like `apk add --update --no-cache python3` +**Remark** If you need to install aditional tools just install them in an additional step upfront the action invokation. +If using the docker image the underlying OS is defined by an Alpine container. ### Example for the hooks input parameter @@ -441,10 +441,6 @@ Please not the double quotes within the following `prepull echo` command ```yml hooks: - install: - commands: - - apk add --update --no-cache python3 - - python3 --version prepull: commands: - echo "hi, we are within the prepull phase ${MY_VAR}" diff --git a/action.yml b/action.yml index b781053..732db6a 100644 --- a/action.yml +++ b/action.yml @@ -71,6 +71,9 @@ outputs: pr_branch: description: "The name of the PR branch" value: ${{ steps.sync.outputs.pr_branch }} + template_git_hash: + description: "The git hash of the template source repository" + value: ${{ steps.sync.outputs.template_git_hash }} runs: using: "composite" # image: "src/Dockerfile" diff --git a/src/sync_template.sh b/src/sync_template.sh index 3b2036a..df0dfda 100755 --- a/src/sync_template.sh +++ b/src/sync_template.sh @@ -45,14 +45,30 @@ fi IS_FORCE_PUSH_PR="${IS_FORCE_PUSH_PR:-"false"}" GIT_REMOTE_PULL_PARAMS="${GIT_REMOTE_PULL_PARAMS:---allow-unrelated-histories --squash --strategy=recursive -X theirs}" -cmd_from_yml "install" - TEMPLATE_SYNC_IGNORE_FILE_PATH=".templatesyncignore" TEMPLATE_REMOTE_GIT_HASH=$(git ls-remote "${SOURCE_REPO}" HEAD | awk '{print $1}') -NEW_TEMPLATE_GIT_HASH=$(git rev-parse --short "${TEMPLATE_REMOTE_GIT_HASH}") -NEW_BRANCH="${PR_BRANCH_NAME_PREFIX}_${NEW_TEMPLATE_GIT_HASH}" -PR_BODY="${PR_BODY:-Merge ${SOURCE_REPO_PATH} ${NEW_TEMPLATE_GIT_HASH}}" -debug "new Git HASH ${NEW_TEMPLATE_GIT_HASH}" +SHORT_TEMPLATE_GIT_HASH=$(git rev-parse --short "${TEMPLATE_REMOTE_GIT_HASH}") + +export TEMPLATE_GIT_HASH=${SHORT_TEMPLATE_GIT_HASH} +export PR_BRANCH="${PR_BRANCH_NAME_PREFIX}_${TEMPLATE_GIT_HASH}" +: "${PR_BODY:="Merge ${SOURCE_REPO} ${TEMPLATE_GIT_HASH}"}" +: "${PR_TITLE:-"upstream merge template repository"}" + +# for some reasons the substitution is not working as expected +# so we substitute manually +# shellcheck disable=SC2016 +PR_BODY=${PR_BODY//'${TEMPLATE_GIT_HASH}'/"${TEMPLATE_GIT_HASH}"} +# shellcheck disable=SC2016 +PR_BODY=${PR_BODY//'${SOURCE_REPO}'/"${SOURCE_REPO}"} + +# shellcheck disable=SC2016 +PR_TITLE=${PR_TITLE//'${TEMPLATE_GIT_HASH}'/"${TEMPLATE_GIT_HASH}"} +# shellcheck disable=SC2016 +PR_TITLE=${PR_TITLE//'${SOURCE_REPO}'/"${SOURCE_REPO}"} + +debug "TEMPLATE_GIT_HASH ${TEMPLATE_GIT_HASH}" +debug "PR_BRANCH ${PR_BRANCH}" +debug "PR_BODY ${PR_BODY}" # Check if the Ignore File exists inside .github folder or if it doesn't exist at all if [[ -f ".github/${TEMPLATE_SYNC_IGNORE_FILE_PATH}" || ! -f "${TEMPLATE_SYNC_IGNORE_FILE_PATH}" ]]; then @@ -68,11 +84,14 @@ fi # set the gh action outputs if run with github action. # Arguments: # pr_branch +# template_git_hash ####################################### function set_github_action_outputs() { echo "::group::set gh action outputs" local pr_branch=$1 + local template_git_hash=$2 + info "set github action outputs" if [[ -z "${GITHUB_RUN_ID}" ]]; then @@ -80,6 +99,7 @@ function set_github_action_outputs() { else # https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter echo "pr_branch=${pr_branch}" >> "$GITHUB_OUTPUT" + echo "template_git_hash=${template_git_hash}" >> "$GITHUB_OUTPUT" fi echo "::endgroup::" } @@ -359,7 +379,7 @@ function prechecks() { warn "skipping prechecks because we force push and pr" return 0 fi - check_branch_remote_existing "${NEW_BRANCH}" + check_branch_remote_existing "${PR_BRANCH}" check_if_commit_already_in_hist_graceful_exit "${TEMPLATE_REMOTE_GIT_HASH}" @@ -373,8 +393,8 @@ function checkout_branch_and_pull() { echo "::group::checkout branch and pull" - debug "create new branch from default branch with name ${NEW_BRANCH}" - git checkout -b "${NEW_BRANCH}" + debug "create new branch from default branch with name ${PR_BRANCH}" + git checkout -b "${PR_BRANCH}" debug "pull changes from template" pull_source_changes "${SOURCE_REPO}" "${GIT_REMOTE_PULL_PARAMS}" @@ -437,7 +457,7 @@ function push_prepare_pr_create_pr() { echo "::group::push changes and create PR" cmd_from_yml "prepush" - push "${NEW_BRANCH}" "${IS_FORCE_PUSH_PR}" + push "${PR_BRANCH}" "${IS_FORCE_PUSH_PR}" cmd_from_yml "prepr" if [ "$IS_FORCE_PUSH_PR" == true ] ; then create_or_edit_pr "${PR_TITLE}" "${PR_BODY}" "${UPSTREAM_BRANCH}" "${PR_LABELS}" "${PR_REVIEWERS}" @@ -458,4 +478,4 @@ commit push_prepare_pr_create_pr -set_github_action_outputs "${NEW_BRANCH}" +set_github_action_outputs "${PR_BRANCH}" "${TEMPLATE_GIT_HASH}" diff --git a/templatesync.yml b/templatesync.yml index f8eb2ec..d01a773 100644 --- a/templatesync.yml +++ b/templatesync.yml @@ -1,10 +1,10 @@ # validate hooks: - install: + prepull: commands: - echo "foo ${MY_VAR}" - - echo 'hi, we are within the prepush phase' + - echo 'hi, we are within the prepull phase' prepush: commands: