Habba
Habba

Reputation: 515

Sharing different repositories between jobs for Gitlab CI

In our project we have a number of repositories that are all needed to build a few different docker containers.

The Dockerfiles of all containers are located in a single repository.

I would like to be able to pull all repositories I need for the builds before building the containers. This would avoid pulling a repository multiple times which can take almost a minute for some of these.

Essentially what I would like to do is:

stages:
  - initialize
  - build

repo1 pull:
  stage: initialize
  script:
    - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.instance/group/repo1.git

repo2 pull:
  stage: initialize
  script:
    - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.instance/group/repo2.git

image1:
  stage: build
  script:
      - cp repo1/ image1/
      - cd image1
      - docker build -t image1 --network=host .
      - docker login -u gitlab-runner -p gitlab docker.registry
      - docker push docker.registry/image1:latest

image2:
  stage: build
  script:
      - cp repo2/ image2/
      - cd image2
      - docker build -t image2 --network=host .
      - docker login -u gitlab-runner -p gitlab docker.registry
      - docker push docker.registry/image2:latest

What is the best way to share the repositories over the stages while still allowing for parallel building within the stage?

Upvotes: 0

Views: 3415

Answers (1)

barro32
barro32

Reputation: 2708

I'm still learning Gitlab-ci but maybe cache would help?

Add this before you pull the repos and the runner should check if the repo is already in the cache.

cache:
  paths:
    - repo1/
    - repo2/

Gitlab cache reference: https://docs.gitlab.com/ee/ci/caching/

Edit:
Or maybe using Git submodules for the dependant repos would be a good idea? The runner can then be configured to fetch those submodules to the workspace and you should have access to them throughout the ci job.

You need to add a .gitmodules file that would looks something like this:

[submodule "repo1"]
  path = repo1
  url = ../repo1.git
[submodule "repo2"]
  path = repo2
  url = ../repo2.git

And then in your .gitlab-ci.yml

 variables:
   GIT_SUBMODULE_STRATEGY: normal

Gitlab submodule reference: https://docs.gitlab.com/ce/ci/git_submodules.html

Upvotes: 2

Related Questions