繁体   English   中英

jenkins管道上用于用户和密码的环境变量

[英]enviroment variables on jenkins pipeline for user and password

我对使用针对詹金斯管道的groovy很新。 我有一个已经在运行的管道,正在执行一些步骤,例如运行unitest,sonnarqube分析等。步骤之一是使用curl -u将工件上传到工件。 我不想显示用户并传递输出脚本,所以我使用的是凭据插件,其中存储了用户和密码并具有ID。 但是我不知道如何使用变量将其传递给sh命令。 这就是我现在使用withCredentials的步骤。

stage ('Upload war to Artifactory') {
            withCredentials([usernamePassword(credentialsId: '7c9e8186-1f16-4920-837b-b571ea88a7e8', usernameVariable: 'willy11', passwordVariable: 'hello123')])
sh "sudo curl -u ${willy11}:{$hello123} -T $warPath 'https://artifactory.xxxxx.com:443/artifactory/Platform/$warFile'" 

我不知道如何在curl命令上使用或定义usernameVariablepasswordVariable的值。 现在,我进入输出脚本:

java.lang.IllegalStateException: **There is no body to invoke**
at org.jenkinsci.plugins.workflow.cps.CpsStepContext.newBodyInvoker(CpsStepContext.java:283)
at org.jenkinsci.plugins.workflow.cps.CpsStepContext.newBodyInvoker(CpsStepContext.java:95)

在此处输入图片说明

我该如何实现? 凭据插件,我读到应该在脚本的输出端加上***,这是如何工作的? 我应该声明“ will11”和“ hello123” else并用作环境变量吗?

谢谢。

def call(body) {

def config = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()

def artifactName = 'extractor'
def artifactExt = '.war'
def artifactVersion = '0.0.1'

def buildPath = 'target/'
def warFile = artifactName + '-' + artifactVersion + artifactExt
def warPath = buildPath + warFile
def warNoVersion = artifactName + artifactExt

def deployPath = '/var/lib/tomcat8/webapps/'
def deployFile = deployPath + warNoVersion

node {
    // Clean workspace before doing anything
    //deleteDir()

    try {

        stage ('Code Checkout') {
            git branch: 'master',
                credentialsId: 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
                url: 'ssh://git@bitbucket.org/xxxxxxx/xxxxxxxctor'
        }


stage ('Configure Application') {
            configFileProvider([configFile(fileId: config.confiFileId, variable: 'CONFIG_FILE_PATH')]) {
                sh 'cp $CONFIG_FILE_PATH resources/config.properties'
            }
            sh "echo 'job_name: $JOB_NAME' > WebContent/version.txt"
            sh "echo 'job_number: $BUILD_NUMBER' >> WebContent/version.txt"
        }

        stage ('Run Unitests') {
            sh 'mvn test'
        }

        /*stage ('SonarQube analysis') {
          withSonarQubeEnv('xxxxxxxxxxxxxxxx) {
            sh 'mvn sonar:sonar'
          }  
        }*/

        stage ('Compile and Build WAR') {
            sh 'mvn clean compile war:war'
        }

        stage ('Upload war to Artifactory') {
            withCredentials([usernamePassword(credentialsId: '7c9e8186-1f16-4920-837b-b571ea88a7e8', usernameVariable: 'USER', passwordVariable: 'PASSWORD')])
            sh "sudo curl -u ${USER}:{$PASSWORD} -T $warPath 'https://artifactory.xxxxxxx.com:443/artifactory/Platform/$warFile'" 

        }




    } catch (err) {
        notifyBuild('FAILURE', config.slackChannel)
        throw err
    }
}

当您使用凭据绑定插件时,凭据将绑定到环境变量,并且要执行的代码必须在withCredentials语句的花括号内,这是我们所遗漏的。

因此使用:

 stage ('Upload war to Artifactory') {
       withCredentials([usernamePassword(credentialsId: '7c9e8186-1f16-4920-837b-b571ea88a7e8', usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) {
           sh ("sudo curl -u $USER:$PASSWORD -T $warPath 'https://artifactory.xxxxx.com:443/artifactory/Platform/$warFile'")
       }
 }

代替:

 stage ('Upload war to Artifactory') {
     withCredentials([usernamePassword(credentialsId: '7c9e8186-1f16-4920-837b-b571ea88a7e8', usernameVariable: 'USER', passwordVariable: 'PASSWORD')])
         sh "sudo curl -u ${USER}:{$PASSWORD} -T $warPath 'https://artifactory.xxxxxxx.com:443/artifactory/Platform/$warFile'" 

 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM