diff --git a/.gitlab/ci/shared_script.gitlab-ci.yml b/.gitlab/ci/shared_script.gitlab-ci.yml index 88d4f6b73449d47256e06d6f4e70ad503280e94a..d3d1fe4e077cee2c01212f69792c2cd6b6868e8b 100644 --- a/.gitlab/ci/shared_script.gitlab-ci.yml +++ b/.gitlab/ci/shared_script.gitlab-ci.yml @@ -4,10 +4,11 @@ # - DEPENDENCY_NAME: Name of the dependency project # - DEPENDENCY_JOB: Name of the dependency job from which you want to pull artifacts script: + - apt-get -qq install -y jq + # Check if a branch with the same name exist on the dependency repository - BRANCH_TO_PULL="dev" # default branch - # Check if a branch with the same name exist on the dependance repository - > - if [ "$CI_MERGE_REQUEST_ID" ]; then + if [ "$CI_MERGE_REQUEST_ID" ]; then # CASE MERGE REQUEST echo "Merge request pipeline detected" if [ $(curl -s -o /dev/null -w "%{http_code}" "https://gitlab.eclipse.org/eclipse/aidge/$DEPENDENCY_NAME/-/tree/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME") -eq 404 ]; then echo "Pull from default" @@ -16,29 +17,55 @@ echo "Pull from ${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}" BRANCH_TO_PULL=${CI_MERGE_REQUEST_TARGET_BRANCH_NAME} fi - elif [ $(curl -s -o /dev/null -w "%{http_code}" "https://gitlab.eclipse.org/eclipse/aidge/$DEPENDENCY_NAME/-/tree/$CI_COMMIT_REF_NAME") -eq 404 ]; then - echo "Pull from default" - else - # Pulling from branch with same name - echo "Pull from branch name" - BRANCH_TO_PULL=${CI_COMMIT_REF_NAME} + else # CASE CASUAL COMMIT + # checking if given branch exist on official repo or on fork + if [[ $(curl -s -o /dev/null -w "%{http_code}" "https://gitlab.eclipse.org/eclipse/aidge/$DEPENDENCY_NAME/-/tree/$CI_COMMIT_REF_NAME") -ne 302 ]]; then + PULL_FROM_FORK=FALSE + BRANCH_TO_PULL=$CI_COMMIT_REF_NAME + elif [[ $(curl -s -o /dev/null -w "%{http_code}" "https://gitlab.eclipse.org/hrouis/$DEPENDENCY_NAME/-/tree/$CI_COMMIT_REF_NAME") -ne 302 ]]; then + PULL_FROM_FORK=TRUE + BRANCH_TO_PULL=$CI_COMMIT_REF_NAME + else + PULL_FROM_FORK=FALSE + echo "No branch \"$CI_COMMIT_REF_NAME\" found for \"$DEPENDENCY_NAME\" in official or forked repos." + fi fi - - apt-get install -y jq - # Check group namespace is valid, if not DEPENDENCY_ID=6437 (aidge group ID) + - echo "Pulling from branch:\"$BRANCH_TO_PULL\"" - > - if [ $(curl -s -o /dev/null -w "%{http_code}" "https://gitlab.eclipse.org/api/v4/groups/${CI_PROJECT_NAMESPACE_ID}") -eq 404 ]; then - echo "Using aidge group id by default" - DEPENDENCY_ID=$(curl "https://gitlab.eclipse.org/api/v4/groups/6437/projects?search=${DEPENDENCY_NAME}" | jq -r '.[0].id') - else + if [[ ! $PULL_FROM_FORK ]]; then # nominal case : we are in the official project DEPENDENCY_ID=$(curl "https://gitlab.eclipse.org/api/v4/groups/${CI_PROJECT_NAMESPACE_ID}/projects?search=${DEPENDENCY_NAME}" | jq -r '.[0].id') + else # case for fork + echo "Retrieving build_artifacts from hrouis' forked project." + USER_ID=17216 + DEPENDENCY_ID=$(curl --header "PRIVATE_TOKEN: $CI_JOB_TOKEN" "https://gitlab.eclipse.org/api/v4/users/${USER_ID}/projects?search=${DEPENDENCY_NAME}" | jq -r '.[0].id') fi - - echo "Project ID for ${DEPENDENCY_NAME} is ${DEPENDENCY_ID}" - - echo "curling from https://gitlab.eclipse.org/api/v4/projects/$DEPENDENCY_ID/jobs/artifacts/$BRANCH_TO_PULL/download?job=$DEPENDENCY_JOB" - - curl -f --location --output build_artifacts.zip "https://gitlab.eclipse.org/api/v4/projects/$DEPENDENCY_ID/jobs/artifacts/$BRANCH_TO_PULL/download?job=$DEPENDENCY_JOB" + # Retrieve latest successful $DEPENDENCY_JOB $BRANCH_TO_PULL + - PIPELINES=$(curl -s --header "PRIVATE_TOKEN:$CI_JOB_TOKEN" "https://gitlab.eclipse.org/api/v4/projects/$DEPENDENCY_ID/pipelines?ref=${BRANCH_TO_PULL}&order_by=updated_at&sort=desc") + - JOB_ID=-1 + # handle case where there is no branch with the same name + - > + for i in $(seq 0 $(jq length <<< "$PIPELINES")); do + PIPELINE_ID=$(jq --argjson idx $i '.[$idx].id' <<< $PIPELINES) + JOBS=$(curl -s --header "PRIVATE_TOKEN: $CI_JOB_TOKEN" "https://gitlab.eclipse.org/api/v4/projects/$DEPENDENCY_ID/pipelines/$PIPELINE_ID/jobs" | jq --arg job_name "$DEPENDENCY_JOB" 'map(select((.name == $job_name) and (.status == "success")))') + if [[ $(jq '. | length' <<< $JOBS) > 0 ]]; then + JOB_ID=$(jq '.[0].id' <<< $JOBS) + break + fi + done + - > + if( $JOB_ID == -1 ) ; then + echo "ERROR : no successful job \"$DEPENDENCY_JOB\" found in branch \"$BRANCH_TO_PULL\" for project \"$DEPENDENCY_NAME\"" + else + echo "Successful job \"$DEPENDENCY_JOB\" found in branch \"$BRANCH_TO_PULL\" for project \"$DEPENDENCY_NAME\"" + fi + + - echo "curling from \"https://gitlab.eclipse.org/api/v4/projects/$DEPENDENCY_ID/jobs/$JOB_ID/artifacts\"" + - curl --location --output build_artifacts.zip --header "PRIVATE_TOKEN:$CI_JOB_TOKEN" "https://gitlab.eclipse.org/api/v4/projects/$DEPENDENCY_ID/jobs/$JOB_ID/artifacts" + - unzip -q -o build_artifacts.zip -d . - rm -rf build_cpp - + .download_dependency_windows: # Note: