在项目之间共享gitlab-ci.yml [英] Share gitlab-ci.yml between projects

查看:877
本文介绍了在项目之间共享gitlab-ci.yml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在考虑将ci从jenkins转移到gitlab.我们有几个项目具有相同的构建工作流程.现在,我们使用一个共享库,其中定义了管道,项目内部的jenkinsfile仅调用在共享库中定义的定义实际管道的方法.因此,仅需在单个点上进行更改即可影响多个项目.

We are thinking to move our ci from jenkins to gitlab. We have several projects that have the same build workflow. Right now we use a shared library where the pipelines are defined and the jenkinsfile inside the project only calls a method defined in the shared library defining the actual pipeline. So changes only have to be made at a single point affecting several projects.

我想知道gitlab ci是否可以做到这一点?据我发现,无法在存储库外部定义gitlab-ci.yml.是否有另一种方法来定义管道并与几个项目共享此配置以简化维护?

I am wondering if the same is possible with gitlab ci? As far as i have found out it is not possible to define the gitlab-ci.yml outside the repository. Is there another way to define a pipeline and share this config with several projects to simplify maintainance?

推荐答案

首先让我开始说:谢谢您提出这个问题!在我经常想知道自己是否有可能之后,这促使我(再次)寻找解决方案.我们也有20-30个项目,它们完全相同,并且具有大约400-500 loc的.gitlab-ci.yml文件,如果一件事情发生变化,则每个文件都必须更改.

First let me start by saying: Thank you for asking this question! It triggered me to search for a solution (again) after often wondering if this was even possible myself. We also have like 20 - 30 projects that are quite identical and have .gitlab-ci.yml files of about 400 - 500 loc that have to each be changed if one thing changes.

所以我找到了一个可行的解决方案:

受到自动DevOps .gitlab-ci.yml模板 Gitlab自己创建,并在其中使用一个模板作业来

Inspired by the Auto DevOps .gitlab-ci.yml template Gitlab itself created, and where they use one template job to define all functions used and call every before_script to load them, I came up with the following setup.

  • Multiple project repo's (project-1, project-2) requiring a shared set of CI jobs / functions
  • Functions script containing all shared functions in separate repo

文件

因此,请使用共享ci职位密谋:

#!/bin/bash

function list_files {
  ls -lah
}

function current_job_info {
  echo "Running job $CI_JOB_ID on runner $CI_RUNNER_ID ($CI_RUNNER_DESCRIPTION) for pipeline $CI_PIPELINE_ID"
}

常见且通用的.gitlab-ci.yml:

image: ubuntu:latest

before_script:
  # Install curl
  - apt-get update -qqq && apt-get install -qqqy curl
  # Get shared functions script
  - curl -s -o functions.sh https://gitlab.com/giix/demo-shared-ci-functions/raw/master/functions.sh
  # Set permissions
  - chmod +x functions.sh
  # Run script and load functions
  - . ./functions.sh

job1:
  script:
    - current_job_info
    - list_files

您可以从复制并粘贴文件project-1 project-2 ,它将使用相同的共享Gitlab CI功能.

You could copy-paste your file from project-1 to project-2 and it would be using the same shared Gitlab CI functions.

这些示例出于示例目的而非常冗长,可按自己喜欢的方式对其进行优化.

经验教训

因此,在将上述构造大规模应用(超过40个项目)之后,我想分享一些经验教训,因此您不必找出困难的方法:

So after applying the construction above on a large scale (40+ projects) I want to share some lessons learned so you don't have to find out the hard way:

  • 版本(标记/发布)您的共享ci函数脚本.现在,更改一件事可能会使所有管道失败.
  • 使用不同的Docker映像可能会导致bash加载功能的需求出现问题(例如,我将一些基于Alpine的映像用于默认情况下具有sh的基于CLI工具的作业)
  • 使用基于项目的CI/CD机密变量来个性化项目的构建作业.例如环境网址等.
  • Version (tag / release) your shared ci functions script. Changing one thing can now make all pipelines fail.
  • Using different Docker images could cause an issue in the requirement for bash to load the functions (e.g. I use some Alpine-based images for CLI tool based jobs that have sh by default)
  • Use project based CI/CD secret variables to personalize build jobs for projects. Like environment URL's etc.

这篇关于在项目之间共享gitlab-ci.yml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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