标签: jenkinsfile

如何将变量从Jenkinsfile传递给shell命令

我想使用我在Jenkinsfile脚本中使用的变量,然后将其值传递给shell脚本执行(作为环境变量或命令行参数).

但是以下内容Jenkinsfile:

for (i in [ 'a', 'b', 'c' ]) {
    echo i
    sh 'echo "from shell i=$i"'
}
Run Code Online (Sandbox Code Playgroud)

给出输出:

a
from shell i=
b
from shell i=
c
from shell i=
Run Code Online (Sandbox Code Playgroud)

期望的输出是这样的:

a
from shell i=a
b
from shell i=b
c
from shell i=c
Run Code Online (Sandbox Code Playgroud)

知道如何将值传递i给shell scipt吗?

编辑:根据马特的回答,我现在使用这个解决方案:

for (i in [ 'a', 'b', 'c' ]) {
    echo i
    sh "i=${i}; " + 'echo "from shell i=$i"'
}
Run Code Online (Sandbox Code Playgroud)

优点是,我不需要"在shell脚本中转义.

shell groovy jenkins jenkins-pipeline jenkinsfile

16
推荐指数
2
解决办法
3万
查看次数

检索Jenkinsfile中env的所有属性

我想env在Jenkinsfile 中的对象中打印所有可用的属性(及其值).

当我做

print env
Run Code Online (Sandbox Code Playgroud)

我明白了:

org.jenkinsci.plugins.workflow.cps.EnvActionImpl@112cebc2
Run Code Online (Sandbox Code Playgroud)

所以看起来好像toString没有在那里实现,如果我不知道他们的名字怎么能访问这个对象中的属性?

groovy jenkins-workflow jenkinsfile

15
推荐指数
2
解决办法
9165
查看次数

如何在Jenkinsfile中为失败的构建执行操作

如果Jenkinsfile中的构建失败,有没有办法执行清理(或回滚)?

我想通知我们的Atlassian Stash实例构建失败(通过curl在正确的URL处执行).

基本上,当构建状态设置为失败时,它将是一个后续步骤.

我应该用try {} catch ()吗?如果是这样,我应该捕获哪种异常类型?

groovy jenkins-workflow jenkinsfile

14
推荐指数
2
解决办法
2万
查看次数

从Jenkinsfile设置管道名称和描述

我试图做一个jenkins管道作为代码.我正在使用Github组织文件夹插件来扫描Github组织并为每个分支创建作业.有没有办法明确定义从Jenkinsfile获取的管道作业的名称?我还想为这些工作添加一些描述.

jenkins devops jenkinsfile

14
推荐指数
3
解决办法
2万
查看次数

Jenkins管道代码通过GitHub Organization Folder Plugin自动触发多个存储库

此问题与具有多个存储库的Jenkins作业自动触发器有关.

在Jenkinsfile中定义了3个repo来结账.

 node('slave'){
 git clone github.com/owner/abc.git -b ${env.BRANCH_NAME}
 git clone github.com/owner/def.git -b ${env.BRANCH_NAME}
 git clone github.com/owner/ghi.git -b ${env.BRANCH_NAME}
 }
Run Code Online (Sandbox Code Playgroud)

使用Github组织插件配置Jenkins作业.

在这种情况下,我的Jenkinsfile在abc repo中,并且Jenkins自动触发器对于abc repo工作正常.它不适合其他回购.

反正有没有为2个或更多回购定义自动触发?

是否有任何插件可以自动触发2个或更多存储库的作业?

我需要在Jenkinsfile中以不同的方式定义"checkout scm"吗?

jenkins github-organizations jenkins-pipeline jenkinsfile

14
推荐指数
1
解决办法
9875
查看次数

在Jenkinsfile中配置includedRegions?

如果使用特定子目录中的文件更改,我如何才能将Jenkins管道项目的范围限制为仅构建Jenkinsfile

我有一个包含两个目录的Git存储库.每个目录都包含一个单独的子项目,我想用Jenkins分别构建每个子项目Jenkinsfile.该项目具有以下文件结构:

parent
 |
 +- subA
 |   |
 |   + Jenkinsfile
 |   + more files related to sub project A
 |
 +- subB
     |
     + Jenkinsfile
     + more files related to sub project B
Run Code Online (Sandbox Code Playgroud)

JenkinsfilesubA具有以下配置:

checkout scm: [
    $class: 'GitSCM',
    branches: [[name: '*/master']],
    userRemoteConfigs: [[url: 'https://[path]/parent.git']],
    extensions: [[
        $class: 'PathRestriction', includedRegions: 'subA/.*'
    ]]
]
Run Code Online (Sandbox Code Playgroud)

JenkinsfilesubB相似,唯一的区别是,它已指定subBincludedRegions.

在Jenkins服务器中,我创建了两个管道项目并Jenkinsfile分别指向它们.如果在文件夹中更改了文件subA,则会触发Jenkins管道项目A,如果在文件夹中更改了文件subB,则会触发Jenkins管道项目B,这是我所期望的.

问题是如果文件被更改,也会触发Jenkins管道项目A subB …

jenkins jenkinsfile

13
推荐指数
1
解决办法
2668
查看次数

如何在同一个Jenkins节点中加载另一个groovy脚本?

从另一个管道脚本加载管道脚本时,两个管道不会在同一节点上执行:第一个在我的主节点上执行,第二个在从节点上执行.

我正在使用Jenkins管道,Pipeline Script from SCM以这种方式为很多工作提供选项:

  • 我的每个作业都使用Poll SCM选项定义了相应的Git repo URL,以便在对我的代码进行更改(基本作业使用)时自动轮询存储库.

  • 我的每个作业都Jenkinsfile在其存储库的根目录中定义了一个简单的内容,而内部的管道脚本基本上只能加载一个更通用的管道.

例如:

node {
    // --- Load the generic pipeline ---
    checkout scm: [$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'http://github/owner/pipeline-repo.git']]]
    load 'common-pipeline.groovy'
}()
Run Code Online (Sandbox Code Playgroud)

我的common-pipeline.groovy管道可以实现诸如构建,发布或部署工件之类的实际内容,例如:

{ ->
    node() {
        def functions = load 'common/functions.groovy'

        functions.build()
        functions.release()
        functions.deploy()
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我不想强制每个作业的节点,所以两个管道都有,node("master")或者node("remote")因为我真的不想手动处理它,但是我希望一旦第一个管道在特定节点上运行(主机,slave1,slave2,slave3)第二个/加载的管道在同一个节点上执行,因为否则我的实际Git存储库代码不能从其他节点的工作区获得......

有没有什么方法可以指定我希望我的第二个管道在第一个管道的同一节点上执行,或者在使用该load步骤时可能传递一个参数?

jenkins jenkins-pipeline jenkinsfile

13
推荐指数
1
解决办法
5017
查看次数

Jenkinsfile构建日志

是否有任何内置变量可以访问当前正在执行的构建的文本?

我试过用类似的东西currentBuild.log,currentBuild.buildLog但没有运气.

jenkins jenkins-workflow jenkins-pipeline jenkinsfile

12
推荐指数
2
解决办法
2万
查看次数

如何在jenkinsfile中为docker.build设置自定义上下文

我正在尝试为jenkins调整一个docker build.我正在关注我们的docker-compose文件,我正在创建一个创建每个容器并将它们链接在一起的Jenkins文件.我遇到的问题是docker-compose文件声明了一个不是Dockerfile所在的上下文.据我所知,jenkins会将上下文设置为Dockerfile所在的位置,这会根据jenkinsfile或docker-compose文件的构建方式将文件复制到不同的相对位置.

文件夹结构是:

workspace
    |-docker
        |-db
           |-Dockerfile
           |-entrypoint.sh
Run Code Online (Sandbox Code Playgroud)

这就是Dockerfile如何为相关文件声明COPY指令

COPY docker/db/entrypoint.sh /
Run Code Online (Sandbox Code Playgroud)

这是我的jenkinsfile构建文件的方式.据我所知,将上下文放在该目录中

docker.build("db", "${WORKSPACE}/docker/db")
Run Code Online (Sandbox Code Playgroud)

docker-compose文件声明如下:

db:
build:
  context: .
  dockerfile: docker/db/Dockerfile
Run Code Online (Sandbox Code Playgroud)

它将上下文置于项目的根源.

有没有办法告诉jenkinsfile使用与docker-compose文件相同的上下文,以便Dockerfile的COPY指令可以保持不变并对Jenkins和docker-compose都有效?如果那是不可能的,有没有人知道任何替代解决方案?

docker jenkins-pipeline jenkinsfile

12
推荐指数
1
解决办法
1万
查看次数

如何将Jenkins管道脚本的各个部分提取到类中?

我想将我的Jenkins管道脚本重构为类以便于阅读和重用.

问题是我这样做时会遇到异常.我们来看一个简单的例子:

当我跑

echo currentBuild.toString()
Run Code Online (Sandbox Code Playgroud)

一切都好

但是当我把它提取到一个类中时:

class MyClass implements Serializable {
    def runBuild() {
        echo currentBuild.toString()
    }
}
new MyClass().runBuild()
Run Code Online (Sandbox Code Playgroud)

我得到一个例外:

Started by user admin
Replayed #196
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: currentBuild for class: MyClass
Run Code Online (Sandbox Code Playgroud)

将管道代码提取到类中的正确方法是什么?

groovy refactoring jenkins jenkins-pipeline jenkinsfile

10
推荐指数
1
解决办法
6494
查看次数