如何使用GitLab CI中的include从另一个项目中的文件运行脚本? [英] How to run a script from file in another project using include in GitLab CI?

查看:200
本文介绍了如何使用GitLab CI中的include从另一个项目中的文件运行脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过我的包含文件从位于另一个项目中的模板文件运行shell脚本.

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.

template.yml

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

项目B

gitlab-ci.yml

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

stages:
  - deploy

很明显,命令实际上是从模板所在的 ProjectB 运行的,而不是 ProjectA 运行的.可以通过在模板文件中添加 ls -a 来进一步确认.

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.

那么我们应该如何调用 run.sh ?这两个项目都在不同组下的同一GitLab实例上.

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

推荐答案

如果可以访问项目A和项目B,则可以使用触发项目B中项目A中的管道.

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

在项目A中,您将克隆项目B并运行脚本.

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

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

项目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

这篇关于如何使用GitLab CI中的include从另一个项目中的文件运行脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆