jenkins - 如何将 jenkins 脚本化管道转换为声明性管道

标签 jenkins jenkins-pipeline

有人可以帮我将下面的 Jenkins 脚本管道转换为声明性管道吗

node('agent') {

if ( ! "${GIT_BRANCH}".isEmpty()) {
    branch="${GIT_BRANCH}"
} else {
    echo 'The git branch is not provided, exiting..'
    sh 'exit 1'
}

version = extract_version("${GIT_BRANCH}")

if ( "${GIT_BRANCH}".contains("feature")) {
    currentBuild.displayName = "${version}-SNAPSHOT-${env.BUILD_ID}"
}
else {
    currentBuild.displayName = "${version}-${env.BUILD_ID}"
 }
}

我正在尝试检查是否提供了 git branch 并根据 git branch 动态设置 jenkins build id

最佳答案

pipeline {
agent {
        label 'agent'
    }
    stages{
        stage('stag1'){
            steps {
                script {
                    if ( ! "${GIT_BRANCH}".isEmpty()) {
                        branch="${GIT_BRANCH}"
                    } else {
                        echo 'The git branch is not provided, exiting..'
                        sh 'exit 1'
                    }

                    version = extract_version("${GIT_BRANCH}")

                    if ( "${GIT_BRANCH}".contains("feature")) {
                        currentBuild.displayName = "${version}-SNAPSHOT-${env.BUILD_ID}"
                    }
                    else {
                        currentBuild.displayName = "${version}-${env.BUILD_ID}"
                    }
                }
            }           
        }
    }
}

关于jenkins - 如何将 jenkins 脚本化管道转换为声明性管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53329761/

相关文章:

jenkins - 使用 Spock 的 Groovy 共享库测试流水线步骤方法

jenkins - 用于并行执行的 Currying groovy CPS 闭包

python - Jenkins python api 身份验证 "403 forbidden"

jenkins - 从 ACR 拉出时 ImagePullBackOff 与 AKS 的 "rpc error: code = Unknown desc = failed to pull and unpack image"

java - 如何更改 Emma 的 HTML 报告的编码?

不同分支中来自 SCM 的 Jenkins 管道无法正常工作

jenkins - 如何在 Jenkins 管道中抛出异常?

jenkins - 在 Jenkins 管道的并行阶段设置依赖项或优先级

jenkins-pipeline - 使用 Groovy 脚本的 Jenkins Active Choice 参数调用函数

Jenkins Pipeline 将密码参数传递给下游作业