How to run a script from repo A to the pipeline B in Gitlab

Viewed 599

I have two repositories in GitLab, repositories A and B let's say.

Repo A contains:

  • read_ci.yml
  • read_ci.sh

read_ci.yml contains:

stages:
  - initialise

create checksum from pipeline:
  stage: initialise
  script:
    - chmod +x read_ci.sh
    - source ./read_ci.sh

Repo B contains:

  • gitlab-ci.yml

gitlab-ci.yml contains:

include:
  project: 'Project/project_name'
  file: 
    - '.gitlab-ci.yml'
  ref: main

Obviously, this doesn't do what my intention is. What I want to achieve is in the project B pipeline to run the project A script. The reason is that I want project A to be called from multiple different pipelines and run there.

an alternative to this for GitLab: Azure Pipelines. Run script from resource repo

2 Answers

If you want to run the project A script in the project B pipeline, you can add the repository B as a git submodule in A

git submodule add -b <branch-B> <git-repository-B> <target-dir>

You need also to add in the CI job, the variable GIT_SUBMODULE_STRATEGY: recursive.

Submodules would absolutely work as Davide mentions, though it's kinda like using a sledgehammer to hang a picture. If all you want is a single script from the repository, just download it into your container. Use the v4 API with your CI_JOB_TOKEN to download the file, then simply run it using sh. If you have many files in your secondary repository and want access to them all, then use Submodules as Davide mentiones, and make sure your CI job retrieves them by setting the submodule strategy like this:

variables:
  GIT_SUBMODULE_STRATEGY: normal
Related