首页 > 解决方案 > 将字符串参数传递给 Jenkins 声明性脚本

问题描述

我在 Jenkins 中声明一个字符串参数 -

Name is SLACKTOKEN 
Value is qwsaw2345

我的 Jenkins 文件有脚本

pipeline {

    agent { label 'trial' }

    stages {
        stage("Build Docker Image") {
            steps{
                sh 'docker build -t trial:latest --build-arg SLACK_SIGNING_SECRET=${SLACKTOKEN}'
            }
        }


    }
}

我试过这样,但它没有用。您能否让我知道如何将 Jenkins 字符串参数中的值传递给 Jenkins 声明性脚本文件。我在工作中添加了密码参数,如下所示

在此处输入图像描述

标签: jenkinsjenkins-pipeline

解决方案


parameters指令里面你可以提供参数,细节在这里

params.SLACKTOKEN在双引号内传递参数使用,而不是单引号:

pipeline {

    agent { label 'trial' }
    parameters {
        password(name: 'SLACKTOKEN', defaultValue: '', description: 'Slack Token')
    }
    stages {
        stage("Build Docker Image") {
            steps{
                sh "docker build -t trial:latest --build-arg SLACK_SIGNING_SECRET=${params.SLACKTOKEN}"
            }
        }
    }
}

推荐阅读