How to build thirdparty library in submodule with gitlab-ci only when the submodule has changed?

55 Views Asked by At

My project is using a thirdparty library with git submodule.

Since building the thirdparty library takes too much time, i want to use rules in gitlab-ci to control the thirdparty building job.

Here is my .gitlab-ci.yml

stages:
  - extern
  - package

extern:
  stage: extern
  variables:
    GIT_SUBMODULE_STRATEGY: recursive
  script:
    - cd extern
    - cmake -B build -S . -A x64
    - cmake --build build --config Release --target install
  rules:
    - changes:
        - extern/**/*

package:
  stage: package
  variables:
    GIT_SUBMODULE_STRATEGY: none
  script:
    - echo "building..."

I want make the extern job only excuted if the submodule head changed in my main repository.

But it didn't meet my expectations. The extern job not running as I expected when I change the submodule head in my main repository.

BTW, I'm using GitLab CE 14.6.1

1

There are 1 best solutions below

0
On

I am assuming CI_COMMIT_BRANCH holds your feature branch name. This Shell script gets all the modified files and search for submodule/external file in the modified files. If it is present in modified files then it builds it.

changed_files=$(git show --pretty="" --name-only $CI_COMMIT_BRANCH)
SAVEIFS=$IFS   
IFS=$'\n'      
changed_files=(${changed_files})
IFS=$SAVEIFS   
          
for (( i=0; i<${#changed_files[@]}; i++ ))
do
    echo "Changed File - ${changed_files[$i]}"
    if [[ ${changed_files[$i]} != *"extern"* ]]; then
        echo "There are changes in Submodules hence building it"
        cd extern
        cmake -B build -S . -A x64
        cmake --build build --config Release --target install
    fi
done