Commit a66b68cf authored by Ben Boeckel's avatar Ben Boeckel

gitlab-ci: fix fetch_submodule.cmake to work with 13.4

13.4 now puts some pipelines in the target project. Adapt the script to
support these cases.

The script now also fetches all relevant information from the
environment itself.
parent a961a98b
cmake_minimum_required(VERSION 2.8.8)
# This should be run with two command line arguments. The first
# is the base url name to search for user forks (-Durl_prefix:STRING=...)
# The second is the username to check (-Dusername:STRING=...)
# Check if we're in a merge request (nothing should be needed if not).
if ("$ENV{CI_MERGE_REQUEST_ID}" STREQUAL "")
return ()
endif ()
# Find Git.
find_program(GIT_COMMAND NAMES git git.cmd)
execute_process(COMMAND "${GIT_COMMAND}" "remote" "-v"
OUTPUT_VARIABLE remotes)
string(REGEX MATCH "origin.*\\(fetch" line "${remotes}")
string(REGEX MATCH "[^ \t]+[ \t]+([^ \t]+)[ \t]+[^\t ]+" myurl "${line}")
string(REGEX MATCH "[^/]+$" repoName "${CMAKE_MATCH_1}")
# used for testing before submodules moved to gitlab with lowercase origin URLS
string(TOLOWER "${repoName}" repoName)
set(userFork "${url_prefix}/${username}/${repoName}")
if (NOT GIT_COMMAND)
message(FATAL_ERROR
"`git` was not found.")
endif ()
# Get the remote URL for this submodule.
execute_process(
COMMAND "${GIT_COMMAND}" remote get-url origin
OUTPUT_VARIABLE remote_url
RESULT_VARIABLE res
ERROR_VARIABLE err
OUTPUT_STRIP_TRAILING_WHITESPACE)
if (res)
message(FATAL_ERROR
"Failed to get the remote url for the submodule: ${err}")
endif ()
# Take the "directory" of the source project path and append the "filename" of
# the origin's URL. We can assume that the user's fork will use the same
# repository name in the vast majority of cases.
get_filename_component(user_namespace "$ENV{CI_MERGE_REQUEST_SOURCE_PROJECT_PATH}" DIRECTORY)
get_filename_component(project_name "${remote_url}" NAME)
set(user_fork "$ENV{CI_SERVER_URL}/${user_namespace}/${project_name}")
# When accessing non-existent forks, sometimes the fetch call just blocks!
# Providing a bogus username and password overcomes that blocking.
string(REGEX REPLACE "^(http[s]*://)" "\\1buildbot:buildbot@" userFork "${userFork}")
string(REGEX REPLACE "^(http[s]*://)" "\\1buildbot:buildbot@" user_fork "${user_fork}")
message("Removing old 'remote', if any")
execute_process(COMMAND "${GIT_COMMAND}" "remote" "rm" "fetch_submodule_tmp")
message("Adding 'remote' for ${userFork}...")
execute_process(COMMAND "${GIT_COMMAND}" "remote" "add" "fetch_submodule_tmp" "${userFork}")
message("Fetching..")
execute_process(COMMAND "${GIT_COMMAND}" "fetch" "fetch_submodule_tmp"
RESULT_VARIABLE fetchResult)
# Use a remote per merge request (aid in debugging if needed).
set(remote "ci-fetch-$ENV{CI_MERGE_REQUEST_ID}")
if(fetchResult)
message("Fetch failed, continuing...")
endif()
message("Removing old 'remote', if any")
execute_process(
COMMAND "${GIT_COMMAND}" remote rm "${remote}")
message("Adding 'remote' for ${user_fork}...")
execute_process(
COMMAND "${GIT_COMMAND}" remote add "${remote}" "${user_fork}")
message("Fetching...")
execute_process(
COMMAND "${GIT_COMMAND}" fetch "${remote}"
RESULT_VARIABLE res)
if (res)
message("Fetch failed, continuing...")
endif ()
message("Removing 'remote' for cleanup.")
execute_process(COMMAND "${GIT_COMMAND}" "remote" "rm" "fetch_submodule_tmp")
execute_process(COMMAND "${GIT_COMMAND}" remote rm "${remote}")
......@@ -44,7 +44,7 @@
- export PATH=$PWD/.gitlab:$PWD/.gitlab/cmake/bin:$PATH
- cmake --version
- "git submodule update --init --recursive || :"
- git submodule foreach --recursive cmake "-Dusername:STRING=$CI_PROJECT_NAMESPACE" "-Durl_prefix:STRING=https://$CI_SERVER_HOST" -P "$PWD/.gitlab/ci/fetch_submodule.cmake"
- git submodule foreach --recursive cmake -P "$PWD/.gitlab/ci/fetch_submodule.cmake"
- git submodule sync --recursive
- git submodule update --init --recursive
- sccache --start-server
......
......@@ -51,7 +51,7 @@
- cmake -P .gitlab/ci/download_qt.cmake
- export CMAKE_PREFIX_PATH=$PWD/.gitlab/qt
- "git submodule update --init --recursive || :"
- git submodule foreach --recursive cmake "-Dusername:STRING=$CI_PROJECT_NAMESPACE" "-Durl_prefix:STRING=https://$CI_SERVER_HOST" -P "$PWD/.gitlab/ci/fetch_submodule.cmake"
- git submodule foreach --recursive cmake -P "$PWD/.gitlab/ci/fetch_submodule.cmake"
- git submodule sync --recursive
- git submodule update --init --recursive
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment