Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a script from file in another project using include in GitLab CI?

I'm trying to run a shell script from my template file located in another project via my include.

How should this be configured to work? Below scripts are simplified versions of my code.

Project A

template.yml

deploy:
  before_script:
    - chmod +x ./.run.sh
    - source ./.run.sh

Project B

gitlab-ci.yml

include:
 - project: 'project-a'
    ref: master
    file: '/template.yml'

stages:
  - deploy

Clearly, the commands are actually being run from ProjectB and not ProjectA where the template resides. This can further be confirmed by adding ls -a in the template file.

So how should we be calling run.sh? Both projects are on the same GitLab instance under different groups.

like image 442
ctwheels Avatar asked Sep 01 '20 17:09

ctwheels


1 Answers

If you have access project A and B, you can use multi-project pipelines. You trigger a pipeline in project A from project B.

In project A, you clone project B and run your script.

Project B

job 1:
  variables:
    PROJECT_PATH: "$CI_PROJECT_PATH"
    RELEASE_BRANCH: "$CI_COMMIT_BRANCH"
  trigger:
    project: project-a
    strategy: depend

Project A

job 2:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "pipeline" && $PROJECT_PATH && $RELEASE_BRANCH'
  script:
    - git clone -b "${RELEASE_BRANCH}" --depth 50 https://gitlab-ci-token:${CI_JOB_TOKEN}@${CI_SERVER_HOST}/${PROJECT_PATH}.git $(basename ${PROJECT_PATH})
    - cd $(basename ${PROJECT_PATH})
    - chmod +x ../.run.sh
    - source ../.run.sh
like image 97
Baltazardoung Avatar answered Sep 30 '22 12:09

Baltazardoung