标签: jenkins-declarative-pipeline

带有命名参数的自定义jenkins声明性管道dsl

我已经尝试了一段时间,开始努力将我们的自由风格项目转移到管道上.为此,我觉得最好建立一个共享库,因为我们的大多数构建都是一样的.我通读了Jenkins的这篇博客文章.我想出了以下内容

// vars/buildGitWebProject.groovy
def call(body) {
    def args= [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = args
    body()

    pipeline {
        agent {
            node {
                label 'master'
                customWorkspace "c:\\jenkins_repos\\${args.repositoryName}\\${args.branchName}"
            }
        }
        environment {
            REPOSITORY_NAME = "${args.repositoryName}"
            BRANCH_NAME = "${args.branchName}"
            SOLUTION_NAME = "${args.solutionName}"
        }
        options {
            buildDiscarder(logRotator(numToKeepStr: '3'))
            skipStagesAfterUnstable()
            timestamps()
        }
        stages {
            stage("checkout") {
                steps {
                    script{
                        assert REPOSITORY_NAME != null : "repositoryName is null. Please include it in configuration."
                        assert BRANCH_NAME != null : "branchName is null. Please …
Run Code Online (Sandbox Code Playgroud)

jenkins jenkins-pipeline jenkins-declarative-pipeline

2
推荐指数
1
解决办法
1135
查看次数

如何在 Jenkins 文件中声明多个代理,然后在后续阶段引用它们?

我正在尝试运行一个包含多个代理的 jenkins 文件,但遇到了错误。这是我的詹金斯文件的片段:

pipeline {
    agent {
        docker {
            label 'agentAAA'
            ...
        }
        node {
            label 'agentBBB'
            ...
        }
    }
    ...
    stages {
        stage('to run on AAA') {
            agent {
                label 'agentAAA'
            }
            ...
        }
        stage('to run on BBB') {
            agent {
                label 'agentBBB'
            }
            ...
        }
        stage('to run on BBB') {
            agent {
                label 'agentBBB'
            }
            ...
        }
Run Code Online (Sandbox Code Playgroud)

我收到这些错误:

  • 每个代理部分仅允许一种代理类型
  • 未指定代理类型。必须是 [any、docker、dockerfile、label、none] 之一

我在文档中找不到任何有关如何引用先前声明的代理的示例。我了解如何在每个单独的阶段声明代理,但最终我的文件中会出现许多重复的声明。

jenkins jenkins-pipeline jenkins-declarative-pipeline

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

Jenkins 声明式管道为并行构建创建自定义工作区

我正在构建一个带有声明性管道的项目,我想为并行步骤创建两个具有两个不同名称的不同工作区。我该怎么做?

即,像这样的东西

build1 workspace : /some/path/build-1
build2 workspace : /some/path/build-2
Run Code Online (Sandbox Code Playgroud)

jenkins jenkins-pipeline jenkins-declarative-pipeline

2
推荐指数
1
解决办法
8470
查看次数

Jenkins 中的“容器”关键字是什么?

我最近继承了一些 Jenkins 文件的代码库。在其中之一中,我遇到了这种语法

            stage('Prepare database for integration tests') {
                steps {
                    container('postgres') {
                        sh "..."
                    }
                }
            }
Run Code Online (Sandbox Code Playgroud)

我们的 Jenkins 在 Openshift 上运行,Pod 有多个容器,包括这个 postgres 一个。但是我找不到任何关于使用容器或连接到容器的参考container('containerName')

我试图用以下部分创建一个单独的管道

                        container('az-cli') {
                            try {
                                sh 'ls'
                            } catch (error) {
                                throw error
                            }
                        }
Run Code Online (Sandbox Code Playgroud)

ls列出代码存储库中的内容 - 而不是容器。显然container没有做我认为它做的事情,我找不到任何关于它的文档。有谁知道这是应该做什么?

提前致谢。

jenkins jenkins-declarative-pipeline

2
推荐指数
1
解决办法
1570
查看次数

在具有相同工作空间的同一节点中并行运行多个阶段

我有 3 个构建作业,它们在声明性 jenkins 文件中并行运行。它们运行在同一个节点,需要使用同一个工作空间。问题是 Jenkins 为每个阶段引用的工作空间,例如:

C:\UserData\Workspace                 \\Workspace for Job1
C:\UserData\Workspace@2               \\Workspace for Job2
C:\UserData\Workspace@3               \\Workspace for Job3
Run Code Online (Sandbox Code Playgroud)

Jenkins 为剩余 2 个阶段附加“@2”和“@3”,因此存在路径问题并且作业失败。有人可以帮我解决这个问题吗?

My code is:

pipeline {
    stages {
    stage('Build') {
                parallel {
                    stage('Job1') {
                        agent {
                              node {
                                    label 'label1'
                                    customWorkspace = "C:\UserData\Workspace"
                }
            }     
                   stage('Job2') { ... similar code ... }
                   stage('Job3') { ... similar code ... }
    }
}
Run Code Online (Sandbox Code Playgroud)

parallel-processing workspace jenkins jenkins-pipeline jenkins-declarative-pipeline

2
推荐指数
1
解决办法
2715
查看次数

禁用 Jenkinsfile 中的一个阶段

我想暂时禁用 Jenkinsfile 中的某些阶段。删除它们然后从版本历史记录中恢复似乎太麻烦了。我尝试添加这个:

    stage('Tests') {
      when {
        false
      }
    (...)
Run Code Online (Sandbox Code Playgroud)

但是当我触发作业时会导致错误:

WorkflowScript: 30: Expected a when condition @ line 30, column 7.

         when {

         ^

WorkflowScript: 30: Empty when closure, remove the property or add some content. @ line 30, column 7.

         when {
Run Code Online (Sandbox Code Playgroud)

有没有办法when: never在詹金斯声明性管道中做?

continuous-integration jenkins jenkins-pipeline jenkins-declarative-pipeline

2
推荐指数
1
解决办法
2758
查看次数

具有声明性 Jenkins 管道的主动选择参数


我正在尝试将活动选择参数与声明性 Jenkins 管道脚本一起使用。

这是我的简单脚本:


environments = 'lab\nstage\npro'

properties([
    parameters([
        [$class: 'ChoiceParameter',
            choiceType: 'PT_SINGLE_SELECT',
            description: 'Select a choice',
            filterLength: 1,
            filterable: true,
            name: 'choice1',
            randomName: 'choice-parameter-7601235200970',
            script: [$class: 'GroovyScript',
                fallbackScript: [classpath: [], sandbox: false, script: 'return ["ERROR"]'],
                script: [classpath: [], sandbox: false, 
                    script: """
                        if params.ENVIRONMENT == 'lab'
                            return['aaa','bbb']
                        else
                            return ['ccc', 'ddd']
                    """
                ]]]
    ])
])

pipeline {
    agent any
    tools {
        maven 'Maven 3.6'
    }
    options {
        disableConcurrentBuilds()
        timestamps()
        timeout(time: 30, unit: 'MINUTES')
        ansiColor('xterm')
    }
    parameters {
        choice(name: 'ENVIRONMENT', …
Run Code Online (Sandbox Code Playgroud)

jenkins jenkins-pipeline jenkins-declarative-pipeline

2
推荐指数
1
解决办法
3827
查看次数

Jenkins - 将 withCredentials 转换为声明式语法

我有一个 jenkins 管道,它是使用脚本语法编写的,我需要使用声明式样式将其变成一个新管道。

这是我与 jenkins 的第一个项目,我一直在研究如何在声明性 jenkins 中翻译 withCredentials 语法。

原始(脚本化)管道如下所示:

stage('stage1') {
     steps {
        script {
            withCredentials([usernamePassword(credentialsId: CredentialsAWS, passwordVariable: 'AWS_SECRET_ACCESS_KEY', usernameVariable: 'AWS_ACCESS_KEY_ID')]) {
                parallel (
                    something: {
                         sh 'some commands where there is no reference to the above credentials'
                    }
                )
            }
        }
     }
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已将有问题的凭据设置为环境变量,但由于在原始管道中这些未在命令中引用,但它们只是将命令包装为“withCredentials”,我不确定如何实现相同的结果。知道如何做到这一点吗?

jenkins jenkins-pipeline jenkins-declarative-pipeline

2
推荐指数
1
解决办法
1904
查看次数

Jenkins 'agent: none' 与脚本化管道等效的轻量级执行器

使用 Jenkins 声明式语法,可以在没有顶级代理的情况下运行并行阶段。这最终会消耗两个执行程序,因为顶级代理被标记为“无”:

pipeline {
    agent none
    stages {
        stage('Run on parallel nodes') {
            parallel {
                stage('Do one thing') {
                    agent any
                    steps {
                        ...
                    }
                stage('Do another thing') {
                    agent any
                    steps {
                        ...
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

对于需要顶级“节点”元素的脚本化管道,这似乎是不可能的。这最终消耗了三个执行者,即使只有两个在做真正的工作:

node {
  stage('Run on parallel nodes') {
    parallel ([
      'Do one thing': {
          node() {
            ...
          }
      },
      'Do another thing': {
          node() {
              ...
          }
      }
    ])
  }
}
Run Code Online (Sandbox Code Playgroud)

脚本化管道是否可以实现“轻量级”顶级执行程序?

jenkins jenkins-pipeline jenkins-declarative-pipeline

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

如何使用 Junit 在 Jenkins 中显示 Angular Lint 输出?

如何在 Jenkins 中显示“npm lint”结果?我知道如何在 Jenkins 中显示“pytest”结果:

  1. 像这样运行 pytest: pytest --junitxml=./path/to/pytestFile.xml ./path/to/code
  2. 运行这个 Jenkins 命令: junit testResults: "./path/to/pytestFile.xml"

一旦我这样做了,我的测试结果就会出现在 Jenkins 的“测试结果”窗口中。我想对我的 angular lint 结果做类似的事情。我尝试这样做,但没有用:

  1. 像这样运行 linter: npm lint --junitxml=./path/to/lintFile.xml
  2. 运行这个 Jenkins 命令: junit testResults: "./path/to/lintFile.xml"

问题是 npm 没有 --junitxml 开关。我怎样才能做到这一点?我正在使用 Jenkins 声明式管道。

junit lint jenkins angular-cli jenkins-declarative-pipeline

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

Jenkins 结帐管道步骤中是否允许“扩展”的多个值?

参考工作流 scm 步骤中的“扩展”属性,它被声明为具有“对象的嵌套选择”的数组/列表。

我想做类似以下的事情:

checkout(
  [
    $class: 'GitSCM',
    extensions: [
      [$class: 'CloneOption', timeout: 15],
      [$class: 'AuthorInChangelog']
    ],
    ...
  ]
)
Run Code Online (Sandbox Code Playgroud)

真的是checkout.extensions数组/数组/列表的列表吗?

jenkins-pipeline jenkins-declarative-pipeline

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