Why Bitbucket souce code repository not getting cloned using Jenkins? - jenkins

I am using latest Jenkins in my Linux box. I am trying to clone a Bitbucket repository and checkout master branch using Jenkins pipeline. What I am trying to achieve is, after getting my source code cloned, I can make use of it and proceed with build. Below is my code;
node {
stage('Build') {
git branch: 'master',
credentialsId: '12345678-1234-1234-1234-123456789123',
url: 'https://username#bitbucket.org/username/test.git'
}
stage('Test') {
sh """pwd"""
}
}
and below is my console output;
Started by user User
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/test_pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] git
The recommended git tool is: NONE
using credential 12345678-1234-1234-1234-123456789123
> git rev-parse --resolve-git-dir /var/lib/jenkins/workspace/test_pipeline/.git # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://username#bitbucket.org/username/test.git # timeout=10
Fetching upstream changes from https://username#bitbucket.org/username/test.git
> git --version # timeout=10
> git --version # 'git version 2.32.0'
using GIT_ASKPASS to set credentials bitbucket_token_1
> git fetch --tags --force --progress -- https://username#bitbucket.org/username/test.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision ae8839d74d91d4ae28ba37a4ec91fa2265058b75 (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f ae8839d74d91d4ae28ba37a4ec91fa2265058b75 # timeout=10
> git branch -a -v --no-abbrev # timeout=10
> git branch -D master # timeout=10
> git checkout -b master ae8839d74d91d4ae28ba37a4ec91fa2265058b75 # timeout=10
Commit message: "Initial commit"
> git rev-list --no-walk ae8839d74d91d4ae28ba37a4ec91fa2265058b75 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] sh
+ pwd
/var/lib/jenkins/workspace/test_pipeline
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
When I cd to /var/lib/jenkins/workspace/test_pipeline I can see nothing. Why is this happening?

Related

git checkouts many times despite checking out only one-time in Jenkins pipeline

I have a Jenkins pipeline code that has 4 stages viz
1. CHECKOUT <--- git checkout only happens here
2. DEV
3. DEV2
3. DEV3
I do git checkout only in the first stage i.e CHECKOUT
Below is my pipeline code
stages {
stage('checkout') {
steps{
script {
def git_params = checkout([$class: 'GitSCM'])
println "*/${GIT_BRANCH}"
// Loading environment variables
checkout([
$class: 'GitSCM',
branches: [
[name: "*/${GIT_BRANCH}"]
],
extensions: [
[$class: 'WipeWorkspace'],
],
userRemoteConfigs: [[credentialsId: MYAPP_GIT_CREDENTIAL_ID, url: MYAPP_GIT_REPOSITORY]]
])
PIPELINE_NODE = NODE_NAME
dir('temp1'){
checkout([
$class: 'GitSCM',
branches: [
[name: '*/master']
],
userRemoteConfigs: [[credentialsId: MY_GIT_CREDENTIAL_ID, url: MY_GIT_REPOSITORY]]
])
}
stash name: "temp1", includes: "temp1/*"
//Load variable file from GitSCM inorder to get Maven variable
load "temp1/${APP_NAME}_${STARTENV}_EnvFile"
//load "temp1/${APP_NAME}_*_EnvFile"
sh 'chmod 755 temp1/git_version.sh'
env.APP_VERSION = sh(script: "temp1/git_version.sh", returnStdout: true).toString().trim()
sh 'echo REL#=$APP_VERSION'
}
}
}
}
stage('DEV'){
agent any
when {
branch 'develop'
}
steps{
script {
env.APP_VERSION = sh(script: "temp1/git_version.sh", returnStdout: true).toString().trim()
}
}
}
stage('DEV2'){
agent any
when {
branch 'develop2'
}
steps{
script {
env.APP_VERSION = sh(script: "temp1/git_version.sh", returnStdout: true).toString().trim()
}
}
}
stage('DEV3'){
agent any
when {
branch 'develop3'
}
steps{
script {
env.APP_VERSION = sh(script: "temp1/git_version.sh", returnStdout: true).toString().trim()
}
}
}
However, in the output you can see that the checkout can be seen multiple times in the logs when i expected it to show only one time. Although DEV & DEV2 stages are skipped the git checkout gets logged after the skip as you can see in the below output.
......
Stage "DEV" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (DEV2)
[Pipeline] node
Running on JENKINS_SLAVE5-SLAVE-01 in /apps/jenkins/workspace/ASERVE_release_2.0.4#2
[Pipeline] {
[Pipeline] checkout
The recommended git tool is: /bin/git
using credential JENKINS_CREDEN
Fetching changes from the remote Git repository
Fetching without tags
Checking out Revision a547d4a01537e9869ae3fccc8debf66a9343f723 (release/2.0.4)
Commit message: "VelocityAdapter_Jenkinsfile edited online with Bitbucket"
> /bin/git config remote.origin.url https://bitbucket.mycomp.com/scm/mhmwp/adapter.git # timeout=10
> /bin/git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
> /bin/git config core.sparsecheckout # timeout=10
> /bin/git checkout -f a547d4a01537e9869ae3fccc8debf66a9343f723 # timeout=10
[Pipeline] withEnv
[Pipeline] {
> /bin/git rev-parse --is-inside-work-tree # timeout=10
> /bin/git config remote.origin.url https://bitbucket.mycomp.com/scm/mhmwp/adapter.git # timeout=10
Fetching upstream changes from https://bitbucket.mycomp.com/scm/mhmwp/adapter.git
> /bin/git --version # timeout=10
> git --version # 'git version 1.8.3.1'
using GIT_ASKPASS to set credentials JENKINS_CREDEN
> /bin/git fetch --no-tags --progress https://bitbucket.mycomp.com/scm/mhmwp/adapter.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> /bin/git config core.sparsecheckout # timeout=10
> /bin/git checkout -f a547d4a01537e9869ae3fccc8debf66a9343f723 # timeout=10
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
Stage "DEV2" skipped due to when conditional
........
......
Can you please suggest ?

Jenkins reports incorrect username and password for Github credentials

Problem Statement: I am getting error like "stderr: remote: Invalid username or password." for Jenkins build.
My Jenkinsfile is as follows:
node {
stage('SCM Checkout') {
git 'https://github.com/xxxxxxxxxxxx/java-app'
}
stage('Compile Package'){
// Get path of Maven Home
def MavenHome = tool name: 'MVN_HOME', type: 'maven'
sh "${MavenHome}/bin/mvn package"
}
}
I have saved my credentials in Jenkins as below:
My Jenkins looks like below:
Unfortunately, I am getting error/log output as below:
Checking out git https://github.com/xxxxxxxxxxx/java-app into /var/lib/jenkins/workspace/My-app-Jenkinspipeline#script to read Jenkinsfile
using credential githubcredentials
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://github.com/xxxxxxxxxxx/java-app # timeout=10
Fetching upstream changes from https://github.com/xxxxxxxxxxx/java-app
> git --version # timeout=10
using GIT_ASKPASS to set credentials Github Credentials
> git fetch --tags --force --progress -- https://github.com/xxxxxxxxxxx/java-app +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 691f79125928ce480176fd04e96c4f61297c8051 (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f 691f79125928ce480176fd04e96c4f61297c8051 # timeout=10
Commit message: "Added Checkout step"
> git rev-list --no-walk 371a72a8024ce467f287f5297efaa5662c3c5181 # timeout=10
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/My-app-Jenkinspipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (SCM Checkout)
[Pipeline] git
No credentials specified
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://github.com/xxxxxxxxxxx/java-app # timeout=10
Fetching upstream changes from https://github.com/xxxxxxxxxxx/java-app
> git --version # timeout=10
> git fetch --tags --force --progress -- https://github.com/xxxxxxxxxxx/java-app +refs/heads/*:refs/remotes/origin/* # timeout=10
ERROR: Error fetching remote repo 'origin'
hudson.plugins.git.GitException: Failed to fetch from https://github.com/xxxxxxxxxxx/java-app
at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:909)
at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1131)
at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1167)
at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:125)
at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:93)
at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:80)
at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: hudson.plugins.git.GitException: Command "git fetch --tags --force --progress -- https://github.com/xxxxxxxxxxx/java-app +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout:
stderr: remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/xxxxxxxxxxx/java-app/'
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2430)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:2044)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$500(CliGitAPIImpl.java:81)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:569)
at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:907)
... 11 more
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: Error fetching remote repo 'origin'
Finished: FAILURE
Please assist as I want to understand why I am getting an incorrect username and password even though I can login with the same credentials to Github
To get SCM Checkout working we need to add credentials. I did as below:
git changelog: false, credentialsId: 'githubcredentials', poll: false, url: 'https://github.com/xxxxxxxxxxxx/java-app'

Jenkins Pipeline - Jenkins SCM step. Checkout returns invalid data not referring to parameters provided

I am setting up CI/CD for my project and decided to use Jenkins Pipeline. What I need basically:
Build from chosen branch(chosen from dropdown)
Execute some stages conditionally(depending on branch)
I configured pipeline to fetch Jenkinsfile from scm:
Then I decided to write cutom scm checkout as following:
stage('Checkout specific branch') {
steps {
echo "Checking out ${params.branchToBuild}..."
script {
def actualBranchCheckout = checkout([$class: 'GitSCM',
branches: [
[name: "${params.branchToBuild}"]
],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'LocalBranch', localBranch: "**"]],
submoduleCfg: [],
userRemoteConfigs: [
[credentialsId: "${env.REPO_SSH_CREDS_ID}", url: "${env.REPO_SSH_URL}"]
]
])
}
}
}
And I have sveral concerns. GIT_ environment variables are set when checking out branch for Jenkinsfile.
GIT_BRANCH=origin/master
GIT_COMMIT=11a5800b6ca31bc81545fa4874d73fa275c820c2, GIT_LOCAL_BRANCH=master, GIT_PREVIOUS_COMMIT=11a5800b6ca31bc81545fa4874d73fa275c820c2
That is fine, but when my checkout is performed the are not getting updated with actual values and remain the same.
Okay, the method itself returns map which may contain what I need so I can manually reset them, right?
But they turned out to be the same when checking out develop branch:
Here are the logs:
Obtained Jenkinsfile from git git#bitbucket.org:team/repo.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/Repo/repo_pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
using credential jenkins-bitbucket
Cloning the remote Git repository
Cloning repository git#bitbucket.org:team/repo.git
> git init /var/lib/jenkins/workspace/Repo/repo_pipeline # timeout=10
Fetching upstream changes from git#bitbucket.org:team/repo.git
> git --version # timeout=10
using GIT_SSH to set credentials
> git fetch --tags --progress git#bitbucket.org:team/repo.git +refs/heads/*:refs/remotes/origin/*
> git config remote.origin.url git#bitbucket.org:team/repo.git # timeout=10
> git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
> git config remote.origin.url git#bitbucket.org:team/repo.git # timeout=10
Fetching upstream changes from git#bitbucket.org:team/repo.git
using GIT_SSH to set credentials
> git fetch --tags --progress git#bitbucket.org:team/repo.git +refs/heads/*:refs/remotes/origin/*
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 31e727bf81b3379c9aa80224b8f941bc867acd77 (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f 31e727bf81b3379c9aa80224b8f941bc867acd77
Commit message: "print out variables"
> git rev-list --no-walk 11a5800b6ca31bc81545fa4874d73fa275c820c2 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Checkout specific branch)
[Pipeline] echo
Checking out develop...
[Pipeline] script
[Pipeline] {
[Pipeline] checkout
using credential jenkins-bitbucket
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url git#bitbucket.org:team/repo.git # timeout=10
Fetching upstream changes from git#bitbucket.org:team/repo.git
> git --version # timeout=10
using GIT_SSH to set credentials
> git fetch --tags --progress git#bitbucket.org:team/repo.git +refs/heads/*:refs/remotes/origin/*
> git rev-parse origin/develop^{commit} # timeout=10
Checking out Revision 11a5800b6ca31bc81545fa4874d73fa275c820c2 (origin/develop)
> git config core.sparsecheckout # timeout=10
> git checkout -f 11a5800b6ca31bc81545fa4874d73fa275c820c2
> git branch -a -v --no-abbrev # timeout=10
> git checkout -b develop 11a5800b6ca31bc81545fa4874d73fa275c820c2
Commit message: "another try"
First time build. Skipping changelog.
[Pipeline] echo
[GIT_BRANCH:origin/master, GIT_COMMIT:31e727bf81b3379c9aa80224b8f941bc867acd77, GIT_LOCAL_BRANCH:master, GIT_PREVIOUS_COMMIT:11a5800b6ca31bc81545fa4874d73fa275c820c2, GIT_PREVIOUS_SUCCESSFUL_COMMIT:11a5800b6ca31bc81545fa4874d73fa275c820c2, GIT_URL:git#bitbucket.org:team/repo.git]
I have another option to do everything manually, like git rev-parse --abbrev-ref HEAD for branch and so on. Do anyone know how to solve this/ is it a known issue or I am doing something wrong way?

How do you print out the value of a class property in a Jenkinsfile?

I'm creating a Jenkinsfile for use with GitHub Enterprise. I used the GUI settings in the pipeline job to specify a Jenkinsfile from a GitHub repo.
I'm using the scripted syntax instead of the declarative syntax.
I am able to checkout the repo in my Jenkinsfile using checkout scm. I want to use some information about the checkout in my script, such as the branch name and commit hash. However, I can't figure out how to access variables of the scm class.
When I run the job, it fails in the Checkout stage. The checkout from git seems to work properly, but it fails without printing any errors. If I delete the echo scm.GIT_BRANCH line it works fine.
node {
stage('Checkout') {
checkout scm
echo scm.GIT_BRANCH
}
}
Here's the output:
Started by user spark
Obtained nightly/Jenkinsfile from git https://github.enterprise.instance.com/spark/ci_flow_test
[Pipeline] node
Running on jenkins-server in /home/spark/ci_flow_test/pipeline_test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Checkout)
[Pipeline] checkout
> /apps/git/git18/bin/git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> /apps/git/git18/bin/git config remote.origin.url https://github.enterprise.instance.com/spark/ci_flow_test # timeout=10
Fetching upstream changes from https://github.enterprise.instance.com/spark/ci_flow_test
> /apps/git/git18/bin/git --version # timeout=10
using GIT_ASKPASS to set credentials
> /apps/git/git18/bin/git fetch --tags --progress https://github.enterprise.instance.com/spark/ci_flow_test +refs/heads/*:refs/remotes/origin/*
> /apps/git/git18/bin/git rev-parse refs/remotes/origin/working^{commit} # timeout=10
> /apps/git/git18/bin/git rev-parse refs/remotes/origin/origin/working^{commit} # timeout=10
Checking out Revision 396f172c6061ba2760a71cba817df24836ec7e3b (refs/remotes/origin/working)
Commit message: "try echo"
> /apps/git/git18/bin/git config core.sparsecheckout # timeout=10
> /apps/git/git18/bin/git checkout -f 396f172c6061ba2760a71cba817df24836ec7e3b
> /apps/git/git18/bin/git rev-list 778c36171927020bd1afbd7206d86bf94abd1ed8 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] mail
[Pipeline] echo
Post script
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: FAILURE
You can use the checkout scm return value to get git infomation
node {
stage('Checkout') {
def d = checkout scm
echo "branch: " + d.GIT_BRANCH
echo "commit: " + d.GIT_COMMIT
}
}
// supported fields
GIT_AUTHOR_EMAIL
GIT_AUTHOR_NAME
GIT_BRANCH
GIT_COMMIT
GIT_COMMITTER_EMAIL
GIT_COMMITTER_NAME
GIT_LOCAL_BRANCH
GIT_PREVIOUS_COMMIT
GIT_PREVIOUS_SUCCESSFUL_COMMIT
GIT_URL

Jenkinsfile load groovy file doesn't work as expected

I have a Jenkinsfile with the following :
node('buildprod_iamsg'){
echo "\u001B[34mSending status to dynamodb pib-deployments ...\u001B[0m"
checkout scm
def dynamo = load "aws/dynamo.groovy"
dynamo.add_metadata(substring, stripped_env, "down", lz, slackChannelName, slackToken, awsAccount, stack, ldapCommaSepList, githuburl, approver_name, time)
echo "\u001B[34mJob status sent to dynamodb pib-deployments\u001B[0m"
}
When I execute this It seems to checkout scm but then doesn't print anything. after and exits out. Inside my dynamo.groovy I have :
public add_metadata(substring, stripped_env, statuss, lz, slackChannelName, awsAccount, stack, ldapCommaSepList, githuburl, approver_name, time){
echo "reached here"
//--projection-expression lz
def expression_attribute_values = readFile("expression_attribute_values.json").trim()
echo expression_attribute_values
parseralgo(expression_attribute_values)
}
heres the result of execution :
Running on build_iamsg in /home/jenkins/workspace/djin_aweb_sample
[Pipeline] {
[Pipeline] echo
Sending status to dynamodb pib-deployments ...
[Pipeline] checkout
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://githubnet/djin-productivity/Deptwo.0.git/ # timeout=10
Fetching upstream changes from https://githubnet/djin-productivity/Deptwo.0.git/
> git --version # timeout=10
> git fetch --tags --progress https://githubnet/djin-productivity/Deptwo.0.git/ +refs/heads/*:refs/remotes/origin/*
> git rev-parse refs/remotes/origin/dbint^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/dbint^{commit} # timeout=10
Checking out Revision 7daa4d196eafe4698f941f01631f5ccb13947793 (refs/remotes/origin/dbint)
Commit message: "adding dynamo integration with groovy load"
> git config core.sparsecheckout # timeout=10
> git checkout -f 7daa4d196eafe4698f941f01631f5ccb13947793
[Pipeline] load
[Pipeline] { (aws/dynamo.groovy)
[Pipeline] }
[Pipeline] // load
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // timeout
[Pipeline] // wrap
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: UNSTABLE
doesn't print reached here or the statement after in my jenkinsfile which is Job status sent to dynamodb pib-deployments
By the looks of it I think you forgot to add the slackToken parameter in your groovy script.
public add_metadata(substring, stripped_env, statuss, lz, slackChannelName, awsAccount, stack, ldapCommaSepList, githuburl, approver_name, time){

Resources