Jenkins管道在工作之间共享信息 [英] Jenkins pipeline share information between jobs

查看:109
本文介绍了Jenkins管道在工作之间共享信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试在Jenkins上定义一组作业,这些作业将执行特定的动作. JobA1将构建maven项目,而JobA2将构建.NET代码,JobB将其上传到Artifactory,JobC将其从Artifactory下载,JobD将对其进行部署. 每个作业都有一组参数,因此我们可以将同一作业重复用于任何产品(大约100个).

We are trying to define a set of jobs on Jenkins that will do really specific actions. JobA1 will build maven project, while JobA2 will build .NET code, JobB will upload it to Artifactory, JobC will download it from Artifactory and JobD will deploy it. Every job will have a set of parameters so we can reuse the same job for any product (around 100).

其背后的想法是创建黑匣子,我用一些输入来调用作业,而我总是得到一些输出,之间发生的任何事情都是我不在乎的.另一方面,这使我们能够分别改善每个工作,增加所需的复杂性,并且所有产品都将立即受益.

The idea behind this is to create black boxes, I call a job with some input and I get always some output, whatever happens between is something that I don't care. On the other side, this allows us to improve each job separately, adding the required complexity, and instantly all products will get benefit.

我们要使用Jenkins Pipeline来协调动作的执行.我们将根据环境/使用情况建立一条管道.

We want to use Jenkins Pipeline to orchestrate the execution of actions. We are going to have a pipeline per environment/usage.

  • PipelineA会先调用JobA1,然后再调用JobB将其上传到工件.
  • PipelineB将下载软件包JobC,然后部署到暂存.
  • PipelineC将下载软件包JobC,然后基于一些内部验证将其部署到生产中.

我试图从注入到JobB的JobA1(POM基本内容,如ArtifactID或Version)中获取一些变量,但是信息似乎没有被传输. 下载文件时也会发生同样的情况,我称它为JobC,但文件在作业工作区中对其他任何人都不可用,而且恐怕外部工作区管理器"插件会增加太多的复杂性.

I have tried to get some variables from JobA1 (POM basic stuff such as ArtifactID or Version) injected to JobB but the information seems not to be transfered. Same happens while downloading files, I call JobC but the file is in the job workspace not available for any other and I'm afraid that"External Workspace Manager" plugin adds too much complexity.

除了共享工作空间之外,还有什么方法可以实现我的目的吗?我知道共享工作空间将使不可能同时运行两个管道 我是沿着正确的道路前进还是做一些奇怪的事情?

Is there any way rather than share the workspace to achieve my purpose? I understand that share the workspace will make it impossible to run two pipelines at the same time Am I following the right path or am I doing something weird?

推荐答案

有两种在作业之间共享信息的方式:

There are two ways to share info between jobs:

  1. 您可以使用stash/unstash在单个管道中的多个作业之间共享文件/数据.

  1. You can use stash/unstash to share the files/data between multiple jobs in a single pipeline.

stage ('HostJob') {
    build 'HostJob'
    dir('/var/lib/jenkins/jobs/Hostjob/workspace/') {
        sh 'pwd'
        stash includes: '**/build/fiblib-test', name: 'app' 
    }
}

stage ('TargetJob') {
    dir("/var/lib/jenkins/jobs/TargetJob/workspace/") {
    unstash 'app'
    build 'Targetjob'
}

通过这种方式,您始终可以将文件/exe/数据从一个作业复制到另一个作业.管道插件中的此功能比Artifact更好,因为它仅在本地保存数据.构建后将删除工件(有助于数据管理).

In this manner, you can always copy the file/exe/data from one job to the other. This feature in pipeline plugin is better than Artifact as it saves only the data locally. The artifact is deleted after a build (helps in data management).

您还可以使用复制人工插件.

复制工件需要考虑两件事:

There are two things to consider for copying an artifact:

a)将工件归档在宿主项目中并分配权限.

a) Archive the artifacts in the host project and assign permissions.

b)建立新作业后,选择``复制工件的权限''→选择允许复制工件的项目:*

b) After building a new job, select the 'Permission to copy artifact' → Projects to allow copy artifacts: *

c)创建一个构建后操作→归档工件→要归档的文件:选择文件"

c) Create a Post-build Action → Archive the artifacts → Files to archive: "select your files"

d)将所需的工件从主机复制到目标项目. 创建一个Build动作→从另一个项目中复制工件→输入'$ Project name-Host project',它会生成'e.g.最新成功构建",复制到"$ host项目文件夹"的工件,目标目录"$ localfolder位置".

d) Copy the artifacts required from host to target project. Create a Build action → Copy artifacts from another project → Enter the ' $Project name - Host project', which build 'e.g. Lastest successful build', Artifacts to copy '$host project folder', Target directory '$localfolder location'.

这篇关于Jenkins管道在工作之间共享信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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