Search code examples
gitlabgit-submodulescicd

how can I switch the branch of my submodule within a gitlab ci job


I want to trigger a pipeline on repository that has a submodule from the submodule ci. Every time I do a new change in the submodule I would like to be able to check if the repo using the submodule is still working. The CI on my submodule looks like this

# only run the CI for merge requests
workflow:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'

stages:
  - build
  - integration

default:
  interruptible: true

build:
  stage: build
  script:
    - ./ci/build_all.sh 

trigger_job:
  stage: integration
  variables:
    SUBMODULE_INTEGRATION_NAME: "my_project_name"
    SUBMODULE_INTEGRATION_BRANCH: "$CI_COMMIT_REF_NAME"
  trigger:
    project: main_repository 
    branch: main
    strategy: depend
  needs: ["build"]

And my main_repostitory ci looks like this

stages:
  - build

# This variables are overridden by the upstream pipeline
variables:
  GIT_SUBMODULE_STRATEGY: recursive
  SUBMODULE_INTEGRATION_NAME: "none"
  SUBMODULE_INTEGRATION_BRANCH: "none"
...

submodule-integration-build:
  stage: build
  script:
    - echo "Going to update submodule $SUBMODULE_INTEGRATION_NAME on branch $SUBMODULE_INTEGRATION_BRANCH"
    - cd submodules/$SUBMODULE_INTEGRATION_NAME
    - git switch $SUBMODULE_INTEGRATION_BRANCH
    - cd ../../
    - ./ci/build.sh
  needs: []
  rules:
    - if: $SUBMODULE_INTEGRATION_BRANCH != "none" && $SUBMODULE_INTEGRATION_NAME != "none"
...

However the git switch does not seem to work. It fails with the error: fatal: invalid reference: submodule_branch

I tried to do git submodule update --remote within the script but it didn't work. I tried to do cd submodule/submodule_name && git fetch && git switch submodule_branch but it didn't work. I also tried to add this variable

  variables:
    GIT_SUBMODULE_UPDATE_FLAGS: --remote

to my job but it seems to be updating all the submodules and I don't want that.

Any idea on how can I switch the branch of my submodule within a gitlab ci job?


Solution

  • Finally I managed to switch to the branch by adding --no-single-branch flag to the GIT_SUBMODULE_UPDATE_FLAGS. My triggered job now looks like this

    stages:
      - build
    
    # This variables are overridden by the upstream pipeline
    variables:
      GIT_SUBMODULE_STRATEGY: recursive
      SUBMODULE_INTEGRATION_NAME: "none"
      SUBMODULE_INTEGRATION_BRANCH: "none"
    ...
    
    submodule-integration-build:
      stage: build
      variables:
        GIT_SUBMODULE_UPDATE_FLAGS: --no-single-branch
      script:
        - echo "Going to update submodule $SUBMODULE_INTEGRATION_NAME on branch $SUBMODULE_INTEGRATION_BRANCH"
        - cd submodules/$SUBMODULE_INTEGRATION_NAME
        - git switch $SUBMODULE_INTEGRATION_BRANCH
        - cd ../../
        - ./ci/build.sh
      needs: []
      rules:
        - if: $SUBMODULE_INTEGRATION_BRANCH != "none" && $SUBMODULE_INTEGRATION_NAME != "none"
    ...