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

查看:19
本文介绍了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,然后部署到 staging.
  • 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).

您也可以使用 复制 Artifact 插件.

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

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) 将所需的工件从主机复制到目标项目.创建一个构建操作→从另一个项目复制工件→输入$Project name - Host project",它构建例如"Lastest successful build',Artifacts to copy '$host project folder',Target directory '$localfolder location'.

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天全站免登陆