3

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

spiros_m
  • 41
  • 1
  • 4

2 Answers2

1

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.

Davide Madrisan
  • 1,969
  • 2
  • 14
  • 22
1

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
Patrick
  • 2,885
  • 1
  • 14
  • 20
  • 'just download it into your container'. Uhhh.. how? Can you elaborate? Or provide code? or links to examples? Your link isn't very helpful.. it seems overly complicated and talks about keys and authentication.. sort of like using a remote-controlled drone to hang a picture. – john k Mar 25 '22 at 14:00
  • 1
    Hey @johnktejik - I'm not sure using `curl` to call an API is super complicated, but here ya go: `curl -o my_file.sh --header "PRIVATE-TOKEN $CI_JOB_TOKEN" "https://gitlab.example.com/api/v4/projects//repository/files/"`. If you'd prefer to use sub-modules to do that, that's perfectly fine (and as mentioned, the above solution would work for that). – Patrick Apr 06 '22 at 04:07