From f5a64704bd040aca0765142568b5b27c4ed43472 Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Thu, 16 Jan 2020 13:11:22 +0100 Subject: [PATCH] Fix branch checkout for PRs with GitHub workflow --- .github/workflows/cicd.yml | 1 + docker/build.sh | 46 +++++++++++++++++++++++++------------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index 40acbc44f3..ff0923f9b6 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -6,6 +6,7 @@ on: - master - 'WIP**' - '4.*' + - 'CURA-*' pull_request: jobs: build: diff --git a/docker/build.sh b/docker/build.sh index 6aa0678ca3..5b035ca08a 100755 --- a/docker/build.sh +++ b/docker/build.sh @@ -17,24 +17,40 @@ cd "${PROJECT_DIR}" # Clone Uranium and set PYTHONPATH first # -# Check the branch to use: -# 1. Use the Uranium branch with the branch same if it exists. -# 2. Otherwise, use the default branch name "master" +# Check the branch to use for Uranium. +# It tries the following branch names and uses the first one that's available. +# - GITHUB_HEAD_REF: the branch name of a PR. If it's not a PR, it will be empty. +# - GITHUB_BASE_REF: the branch a PR is based on. If it's not a PR, it will be empty. +# - GITHUB_REF: the branch name if it's a branch on the repository; +# refs/pull/123/merge if it's a pull_request. +# - master: the master branch. It should always exist. + +# For debugging. echo "GITHUB_REF: ${GITHUB_REF}" +echo "GITHUB_HEAD_REF: ${GITHUB_HEAD_REF}" echo "GITHUB_BASE_REF: ${GITHUB_BASE_REF}" -GIT_REF_NAME="${GITHUB_REF}" -if [ -n "${GITHUB_BASE_REF}" ]; then - GIT_REF_NAME="${GITHUB_BASE_REF}" -fi -GIT_REF_NAME="$(basename "${GIT_REF_NAME}")" - -URANIUM_BRANCH="${GIT_REF_NAME:-master}" -output="$(git ls-remote --heads https://github.com/Ultimaker/Uranium.git "${URANIUM_BRANCH}")" -if [ -z "${output}" ]; then - echo "Could not find Uranium banch ${URANIUM_BRANCH}, fallback to use master." - URANIUM_BRANCH="master" -fi +GIT_REF_NAME_LIST=( "${GITHUB_HEAD_REF}" "${GITHUB_BASE_REF}" "${GITHUB_REF}" "master" ) +for git_ref_name in "${GIT_REF_NAME_LIST[@]}" +do + if [ -z "${git_ref_name}" ]; then + continue + fi + git_ref_name="$(basename "${git_ref_name}")" + # Skip refs/pull/1234/merge as pull requests use it as GITHUB_REF + if [[ "${git_ref_name}" == "merge" ]]; then + echo "Skip [${git_ref_name}]" + continue + fi + URANIUM_BRANCH="${git_ref_name}" + output="$(git ls-remote --heads https://github.com/Ultimaker/Uranium.git "${URANIUM_BRANCH}")" + if [ -n "${output}" ]; then + echo "Found Uranium branch [${URANIUM_BRANCH}]." + break + else + echo "Could not find Uranium banch [${URANIUM_BRANCH}], try next." + fi +done echo "Using Uranium branch ${URANIUM_BRANCH} ..." git clone --depth=1 -b "${URANIUM_BRANCH}" https://github.com/Ultimaker/Uranium.git "${PROJECT_DIR}"/Uranium