带有WithCredentials和Powershell的声明性Jenkins

时间:2018-05-18 10:17:53

标签: jenkins jenkins-plugins jenkins-pipeline

stage('Deployment') {
steps {
    withCredentials([string(credentialsId: 'Test', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
        powershell '$pass = ConvertTo-SecureString -AsPlainText "${PASSWORD}" -Force'
        powershell '$SecureString = "${pass}"'
        powershell '$MySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "${USERNAME}","${SecureString}"'
        powershell 'New-PSSession -ComputerName 192.123.123.123 -Credential "${MySecureCreds}"'
     }
     powershell 'Copy-Item "${ARTIFACT_PATH}" -Destination "${DESTINATION_PATH}" -ToSession -Recurse -Force'
     powershell 'Start-Process "iisreset.exe" -NoNewWindow -Wait'
     powershell 'Remove-Website -Name WebCareRecord'
     powershell 'Remove-WebAppPool WebCareRecord'
     powershell 'Get-WebBinding -Port 85 -Name WebCareRecord | Remove-WebBinding'
     powershell 'Start-Process "iisreset.exe" -NoNewWindow -Wait'
     powershell 'New-WebAppPool -Name WebCareRecord'
     powershell 'Set-ItemProperty "${POOL_PATH}" managedPipelineMode 0'
     powershell 'Set-ItemProperty "${POOL_PATH}" managedRuntimeVersion ""'
     powershell 'New-WebSite -Name WebCareRecord -Port 85 -PhysicalPath "${PHYSICAL_PATH}" -ApplicationPool WebCareRecord'
     powershell 'Start-Process "iisreset.exe" -NoNewWindow -Wait'
 }
}

我正在尝试获取Jenkins凭据ID,保护它并使用相同的凭据登录到远程服务器。登录到远程服务器后,将工件从jenkins服务器复制到远程服务器。为此,我收到错误

org.jenkinsci.plugins.credentialsbinding.impl.CredentialNotFoundException:Credentials' Test'是'用户名密码'其中' org.jenkinsci.plugins.plaincredentials.StringCredentials'是预期的。

2 个答案:

答案 0 :(得分:0)

可能存在多个问题,我现在正在经历类似的过程,并感到很难在Groovy中将其正确地放入Powershell中,所以这是我到目前为止注意到的:

您要在一个Powershell步骤中创建一个$pass变量,然后尝试在另一个Powershell步骤中访问它,我认为它不会那样工作,因为另一步骤可能会在不同的Powershell会话中启动,并且该powershell变量不再存在。?

我会尝试这样的事情:

stage('Deployment') {
  steps {
    withCredentials([string(credentialsId: 'Test', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
        powershell """
          \$pass = ConvertTo-SecureString -AsPlainText $PASSWORD -Force
          \$SecureString = \$pass
        """
    }
  }
}

首先,您使用多行语法""",以便不同的powershell语句位于同一会话中,并且这些变量在powershell命令中可用。

第二,您转义了Powershell变量\$pass\$SecureString,因此groovy不会尝试扩展它们,也不会在实际上指的是{{1} }。 请注意,$PASSWORD不必用引号引起来,因为powershell参数可以接受不带引号的字符串,但是,如果在方法中使用了它,则应将其放在引号$PASSWORD中。

通常,我建议在进行故障排除时回显每个变量,以查看您是否获得了期望的结果。

答案 1 :(得分:0)

我喜欢简短而准确的答案。

使用以下内容:

stage('Deployment') {
        steps {
            withCredentials([usernamePassword(credentialsId: 'UatServer', passwordVariable: 'passVar', usernameVariable: 'userVar')]) {
                powershell '''
                    $passVar = ConvertTo-SecureString "$($ENV:passVar)" -AsPlainText -Force
                    $credential = New-Object System.Management.Automation.PSCredential ("$ENV:userVar", $passVar)
                    $session = New-PSSession -ComputerName x.x.x.x -Credential $credential
                    Invoke-Command -Session $session -ScriptBlock {hostname}
                '''
            }
            
        }
    }