I am trying to use the credentials parameter for a git clone. But i am getting the error that the variables are not found
Param definition
credentials (credentialType: 'Username with password', defaultValue: 'fcb2d7c3-4b35-4ef2-bdf0-24fc4ff1137c', description: 'Git credential', name: 'git_credential', required: true)
Usage in stage
withCredentials([usernamePassword(credentialsId: params.git_credential, passwordVariable: 'DOCKER_PASSWORD', usernameVariable: 'DOCKER_USERNAME')]) {
sh 'git reset --hard; git clone https://${DOCKER_USERNAME}:${DOCKER_PASSWORD}#repo_url/scm/${params.repository}.git --branch master'
Error: Wrong variable used
This works for me:
pipeline {
agent any
parameters {
// credentials : defaultValue is Credentials ID
credentials(name: 'git_credential', description: 'Git credential', defaultValue: 'fcb2d7c3-4b35-4ef2-bdf0-24fc4ff1137c', credentialType: "Username with password", required: true )
}
stages {
stage('Print') {
steps {
withCredentials([usernamePassword(
credentialsId: '${git_credential}',
usernameVariable: 'DOCKER_USERNAME',
passwordVariable: 'DOCKER_PASSWORD',
)]) {
sh """
echo id:${DOCKER_USERNAME} pw:${DOCKER_PASSWORD}
"""
}
}
}
}
}
Related
Team,
I want to pull the Jenkinsfile from my private commit which is on private branch on gerrit server. When i set it to main pull works but somehow pull of commit is not working.
steps
I cloned main
created a private branch
pushed Jenkinsfile as a commit to this private branch
added scm config as shown in jenkins gui.
but i am observing that it is not finding my commit. any hint what could be missing in my jenkins gui config? or is this not supported and jenkinsfile needs to be on main branch of the repo?
Couldn't find any revision to build. Verify the repository and branch configuration for this job.
error i get on jenkins log is
Checking out git ssh://svc-repo_test-team-sa#git-av.company.com:12013/repo_test into /var/jenkins/workspace/repo_test/code-coverage-stage#script/7d000137d1c23eb647ed5a846ade258380196b11b7142f8fc6bebd5f9c212d93 to read src/jenkins/ci/Jenkinsfile
The recommended git tool is: git
using credential git-av-repo_test-ci-ssh
> git rev-parse --resolve-git-dir /var/jenkins/workspace/repo_test/code-coverage-stage#script/7d000137d1c23eb647ed5a846ade258380196b11b7142f8fc6bebd5f9c212d93/.git # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url ssh://svc-repo_test-team-sa#git-av.company.com:12013/repo_test # timeout=10
Fetching upstream changes from ssh://svc-repo_test-team-sa#git-av.company.com:12013/repo_test
> git --version # timeout=10
> git --version # 'git version 2.30.2'
using GIT_SSH to set credentials git-av-repo_test-ci-ssh
> git fetch --tags --force --progress -- ssh://svc-repo_test-team-sa#git-av.company.com:12013/repo_test +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse b2c27446fdccb414fb1e92984d528179babf5e78^{commit} # timeout=10
> git rev-parse origin/b2c27446fdccb414fb1e92984d528179babf5e78^{commit} # timeout=10
> git rev-parse b2c27446fdccb414fb1e92984d528179babf5e78^{commit} # timeout=10
ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.
config on jenkins gui is
SCM: git
Branches to build: commit_sha_id << here if I put MAIN it checksout repo but does not work be
Script Path: src/jenkins/ci/Jenkinsfile
sample Jenkinsfile
def checkoutRepo(containerName = 'main') {
container(containerName) {
sh 'rm -rf archive; rm -rf tmp'
checkout(
[
$class: 'GitSCM',
branches: [[name: '$GERRIT_REFSPEC']],
// branches: [[name: params.COMMIT_SHA_ID]],
extensions: [[
$class: 'BuildChooserSetting',
buildChooser: [$class: 'GerritTriggerBuildChooser'],
],
[$class: 'CloneOption', shallow: true, noTags: true, depth: 2, honorRefspec: true]
],
// userRemoteConfigs: scm.userRemoteConfigs
userRemoteConfigs: [[
credentialsId: 'git-product-ci-ssh',
name: 'origin',
refspec: '+$GERRIT_REFSPEC:$GERRIT_REFSPEC',
url: 'ssh://svc-product-org-sa#git.company.com:120/product']
]
]
)
}
}
def bazelInit(containerName = 'main') {
container(containerName) {
withCredentials([
string(credentialsId: 'product-ci-build-cache-jwt', variable: 'CACHE_TOKEN'),
usernamePassword(credentialsId: 'artifactory-build-suborg-ai-bazel', usernameVariable: 'ARTIFACTORY_USER', passwordVariable: 'ARTIFACTORY_TOKEN')
]) {
sh '''
BUILDAUTH_SILENT=1 ./buildauth static
set +e
BUILD_ID=nokill BUILD_TAG=nokill BUILD_VERSION=nokill BUILD_NUMBER= ./bazel version
if [ $? -ne 0 ]; then
./bazel shutdown || true
fi
'''.stripIndent()
}
}
}
pipeline {
agent {
label 'product-verify'
}
options {
// ansiColor('xterm')
parallelsAlwaysFailFast()
buildDiscarder( logRotator( artifactDaysToKeepStr: '30', artifactNumToKeepStr: '100', daysToKeepStr: '30', numToKeepStr: '100'))
throttleJobProperty(
throttleEnabled: true,
throttleOption: 'project',
maxConcurrentPerNode: 1,
maxConcurrentTotal: 20,
)
}
parameters {
booleanParam( name: 'Refresh', defaultValue: false, description: 'Reload job from the Jenkinsfile and then exit')
string( name: 'GERRIT_CREDENTIALS_ID', defaultValue: 'git-product-ci-http', description: 'Gerrit credentials')
string( name: 'GERRIT_API_URL', defaultValue: 'https://git.company.com/r', description: 'Gerrit API URL')
string(name: 'COMMIT_SHA_ID', defaultValue: '', description: 'commit ID')
}
stages {
stage('Checkout') {
steps {
preBuild(bazel_init)
script {
bazel_init = false
}
}
}
stage('Code Scan') {
agent {
label 'product-coverage-cli'
}
when { expression { !params.Refresh } }
steps {
checkoutRepo()
bazelInit()
container('main') {
sh '''
echo "Running on STAGE git"
sonar-scanner -v
echo "installed sonar scanner!"
echo "Start scan"
src/jenkins/ci/code-scan-all.sh
'''.stripIndent()
}
}
}
}
}
You can specify */main branch in job configured, and try the following workaround:
parameters { string(name: 'COMMIT_SHA_ID', defaultValue: '', description: 'commit ID') }
...
stages {
stage('Checkout') {
steps {
script {
if (params.COMMIT_SHA_ID?.trim()) {
echo "Build from ${params.COMMIT_SHA_ID}"
checkout([
$class: 'GitSCM',
branches: [[name: params.COMMIT_SHA_ID]],
userRemoteConfigs: scm.userRemoteConfigs])
}
} else {
echo "Build from main"
}
}
}
}
Might require a whitelisting/permission by an administrator to accessing scm fields.
I would like to create a Pipeline in Jenkins which get the value of a command executed with sshCommand.
I've got a file like this on a remote server :
VALUE 11
Here is my Pipeline :
pipeline {
agent any
stages {
stage('Get Init') {
steps {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId:'xxxxxxx', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
script {
def remote = [:]
remote.name = 'xxxxxx'
remote.host = 'xxxxxx'
remote.user = USERNAME
remote.port = 22
remote.password = PASSWORD
remote.allowAnyHosts = true
sshCommand remote: remote, command: "init=\$(cat /var/log/myFile | grep VALUE | awk '{print \$2}')"
}
}
}
}
stage("Test") {
steps {
script {
test = sh(script "echo $init")
}
}
}
}
}
}
I want to get the "11" in a variable to compare it later in my Jenkinsfile. How to do this ?
You can use in below way :
Example:
def Result = sshCommand remote: remote, command: "<Your command>"
# Declare variable init
def init
pipeline {
agent any
stages {
stage('Get Init') {
steps {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId:'xxxxxxx', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
script {
def remote = [:]
remote.name = 'xxxxxx'
remote.host = 'xxxxxx'
remote.user = USERNAME
remote.port = 22
remote.password = PASSWORD
remote.allowAnyHosts = true
# Execute your command and take return value in the init variable
init= sshCommand remote: remote, command: "cat /var/log/myFile | grep VALUE | awk '{print \$2}')"
echo "Initial Value: " + init
}
}
}
}
stage("Test") {
steps {
script {
test = sh(script "echo ${init}")
}
}
}
}
}
}
I recently modified the Jenkinsfile of my branch(for now, I've only one branch with this jenkins branch).
When I try to launch the multibranch pipeline for this branch, I've a lot of parameters requested, but not the new one I've added.
If I go into Jenkins(not BlueOcean), in the configuration I see them, and if I start a build from there, I also see them.
Here is my Jenkinsfile:
pipeline {
agent {
node{
label 'windows-node'
customWorkspace "D:\\ws\\${env.BRANCH_NAME}"
}
}
options{
skipDefaultCheckout()
}
triggers{
pollSCM 'H 23 * * *'
}
stages {
stage('Initialization started'){
steps{
echo "Job parameters:\n\t- Build X86: ${params.buildX86}\n\t- Build X64: ${params.buildX64}\n\t- Commit Version changes: ${params.commitVersionChanges}.${env.BUILD_NUMBER}\n\t- Setup Version: ${params.version}\n\t- Setup Configuration: ${params.setupConfiguration}\nCurrent repository: ${workspace}"
}
}
stage('Checkout'){
steps{
echo "Custom checkout: ${env.BRANCH_NAME}"
checkout scm
}
}
stage('ABC Solution Pre-build') {
steps {
changeAsmVer "${params.version}.${env.BUILD_NUMBER}"
bat 'nuget.exe restore Solution\\ABC.sln'
powershell 'ContinuousIntegration\\Scripts\\ChangeBindingVersion.ps1 "HDAPluginNet4" "Src\\Clients\\OpcServer\\Xms.OpcHda.Server\\HDANSrv.Net4.exe.config"'
}
}
stage('Preparing SonarQube'){
when{
expression{ params.runTests == true && env.BRANCH_NAME == 'develop'}
}
steps{
withSonarQubeEnv('XYZ SonarQube') {
script{
def sqScannerMsBuildHome = tool 'SonarQube.Runner-3.0'
}
bat "${sqScannerMsBuildHome}\\SonarQube.Scanner.MSBuild.exe begin /k:ABC /n:ABC /v:${params.version}.${env.BUILD_NUMBER} /d:sonar.host.url=%SONAR_HOST_URL% /d:sonar.login=%SONAR_AUTH_TOKEN% /d:sonar.cs.nunit.reportsPaths=TestResult.xml /d:sonar.cs.dotcover.reportsPaths=dotcover.html"
}
}
}
stage('Build ABC Solution') {
steps{
bat "\"${tool 'MSBUILD15'}\" Solution\\ABC.sln /p:Configuration=${params.setupConfiguration} /p:Platform=\"Any CPU\" /t:Rebuild"
}
}
stage('ABC Solution Pre-setup') {
when{
expression{ params.buildX64 == true || params.buildX86 == true}
}
steps{
bat "\"Src\\Obfuscation\\XmsApplicationsObfuscation\\Release\\obfuscationProcess.cmd\" \"${workspace}\" \"${workspace}\\output\\dotfuscator.zip\" \"XXXXXXXX\""
bat "Doc\\BuildDocumentation.bat"
}
}
stage('X64 Setup build') {
when{
expression{ params.buildX64 == true}
}
steps{
bat "\"${tool 'MSBUILD15'}\" Solution\\SetupWix.sln /p:Configuration=${params.setupConfiguration} /p:Platform=x64 /t:Rebuild /p:Version=\"${params.version}.${env.BUILD_NUMBER}\""
bat "move SetupWix\\SetupWix\\bin\\Release\\en-us\\ABCSetup.msi SetupWix\\SetupWix\\bin\\Release\\en-us\\ABCSetup_64_bit.msi"
}
}
stage('X86 Setup build') {
when{
expression{ params.buildX86 == true}
}
steps{
bat "\"${tool 'MSBUILD15'}\" Solution\\SetupWix.sln /p:Configuration=${params.setupConfiguration} /p:Platform=x86 /t:Rebuild /p:Version=\"${params.version}.${env.BUILD_NUMBER}\""
bat "move SetupWix\\SetupWix\\bin\\Release\\en-us\\ABCSetup.msi SetupWix\\SetupWix\\bin\\Release\\en-us\\ABCSetup_32_bit.msi"
}
}
stage('Post-setup'){
when{
expression{ params.buildX64 == true || params.buildX86 == true}
}
steps{
powershell 'ContinuousIntegration\\Scripts\\MoveSetups.ps1'
}
}
stage('Commit version change'){
when{
expression{ params.commitVersionChanges == true}
}
steps{
bat 'git add "./*AssemblyInfo.*"'
bat 'git commit -m "Assembly infos changed by Jenkins"'
bat "git push origin HEAD:${env.BRANCH_NAME}"
}
}
stage('Testing'){
when{
expression{ params.runTests == true}
}
steps{
bat 'dotcover.exe analyze ContinuousIntegration/DotCoverConfig.xml'
nunit testResultsPattern: 'TestResult.xml'
}
}
stage('Finishing SonarQube'){
when{
expression{ params.runTests == true && env.BRANCH_NAME == 'develop'}
}
steps{
withSonarQubeEnv('XYZ SonarQube') {
script{
def sqScannerMsBuildHome = tool 'SonarQube.Runner-3.0'
}
bat "${sqScannerMsBuildHome}\\SonarQube.Scanner.MSBuild.exe end"
}
}
}
}
post{
failure {
emailext body: "<b>Error while excuting the following job</b><br><br>Project: ${env.JOB_NAME} <br>Build Number: ${env.BUILD_NUMBER} <br>Build URL: ${env.BUILD_URL}", mimeType: 'text/html', recipientProviders: [brokenTestsSuspects(), brokenBuildSuspects()], subject: "ERROR CI: Project name -> ${env.JOB_NAME}"
}
unstable{
emailext body: "<b>Error while excuting the following job</b><br><br>Project: ${env.JOB_NAME} <br>Build Number: ${env.BUILD_NUMBER} <br>Build URL: ${env.BUILD_URL}", mimeType: 'text/html', recipientProviders: [brokenTestsSuspects(), brokenBuildSuspects()], subject: "ERROR CI: Project name -> ${env.JOB_NAME}"
}
}
parameters {
booleanParam(name: 'buildX86', defaultValue: false, description: 'Build for X86 platform')
booleanParam(name: 'buildX64', defaultValue: true, description: 'Build for X64 platform')
booleanParam(name: 'commitVersionChanges', defaultValue: false, description: 'Commit the version changes')
booleanParam(name: 'runTests', defaultValue: false, description: 'Run unit tests')
string(name: 'version', defaultValue: '3.6.0', description: 'Version of the setup to build')
choice(name: 'setupConfiguration', choices: '''Release
Debug''', description: 'Setup configuration to use')
}
}
The "new" parameters for which I don't get any request(only in BlueOcean) is the "runTests".
What can I do to get them? I tried to reboot, didn't changed anything.
According to the documentation example, parameters {} needs to be declared before stages{}, otherwise it does not know what values to place in the template variables because scripted pipelines are serially executed from top to bottom.
Also if this was just added to a Jenkinsfile, you may need to run it twice, it won't know the first time that there are params to deal with.
How can I check out code from git like this
stage('Checkout code') {
checkout scm
}
and modify the repository in the subsequent phase? Things like a git tag or a version commit.
Best I could come up with.
stage('Stage Checkout') {
checkout([
$class : 'GitSCM',
branches : scm.branches,
doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
extensions : [] + [
$class : 'LocalBranch'
],
userRemoteConfigs : scm.userRemoteConfigs,
])
}
//do git stuff
stage('Push Version Back to Git') {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'jenkins', usernameVariable: 'GIT_AUTHOR_NAME', passwordVariable: 'GIT_PASSWORD']]) {
sh 'echo ${GIT_AUTHOR_NAME} pushing '
sh 'git config --global user.email "user#test.com"'
sh 'git config --global user.name "Jenkins"'
sh 'git config --global push.default simple'
sh('git push https://${GIT_AUTHOR_NAME}:${GIT_PASSWORD}#github.com/a/a.git')
}
}
Having it as follows made it working to me. And another thing to remember is, if your password or username contains any special characters, make sure that they have been saved in the credentials with the encoded characters. Other it's never gonna work.
This happened to me too and I wasted hours... Crazzzy...
pipeline {
// ..
stages {
stage('Clone') {
steps {
sh "git config --global user.email 'root#my-domain.com'"
sh "git config --global user.name 'My Name'"
sh 'git config --global push.default simple'
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: '123123ghghjg13123', usernameVariable: 'GIT_AUTHOR_NAME', passwordVariable: 'GIT_PASSWORD']]) {
sh('git clone https://${GIT_AUTHOR_NAME}:${GIT_PASSWORD}#my-repo.git')
}
}
}
stage('Build') {
steps {
// ...
}
}
stage('Publish') {
steps {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: '123123ghghjg13123', usernameVariable: 'GIT_AUTHOR_NAME', passwordVariable: 'GIT_PASSWORD']]) {
sh('git push https://${GIT_AUTHOR_NAME}:${GIT_PASSWORD}#my-repo.git --tags -f --no-verify')
}
}
}
}
}
Say my Jenkinsfile is like this:
pipeline {
agent any
parameters {
string(name: 'flag', defaultValue: 'stop', description: 'How should I greet the world?')
}
stages {
stage('Example') {
steps {
checkout scm
sh "echo \"Current Branch: ${env.BRANCH_NAME}.... \""
sh
if [[ $(params.flag) == "run" ]]; then
echo "something"
fi
This is failing. It is not able to read $(params.flag) inside steps. How can I do this?? I am using multibranch pipeline job type.
It's like this (i think it was mostly the multi-line sh syntax that was off):
pipeline {
agent { label 'docker' }
parameters {
string(name: 'flag', defaultValue: 'stop', description: 'How should I greet the world?')
}
stages {
stage('Example') {
steps {
checkout scm
sh "echo \"Current Branch: ${env.BRANCH_NAME}.... \""
sh """
if [[ "${params.flag}" == "run" ]]; then
echo "something"
fi
"""
}
}
}
}