Jenkins Does Not Execute Steps in Jenkinsfile - jenkins

Why does jenkins doesn't execute my jenkinsfile? The console ouput of my build is this:
20:06:19 Started by GitLab push by
20:06:19 Building in workspace /var/jenkins_home/workspace/repo
20:06:19 [WS-CLEANUP] Deleting project workspace...
20:06:19 [WS-CLEANUP] Done
20:06:19 Cloning the remote Git repository
20:06:19 Cloning repository
20:06:19 > git init /var/jenkins_home/workspace/ # timeout=10
20:06:19 Fetching upstream changes from https://gitlab.com/
20:06:19 > git --version # timeout=10
20:06:19 using GIT_ASKPASS to set credentials
20:06:19 > git fetch --tags --progress https://gitlab.com/t+refs/heads/*:refs/remotes/origin/*
20:06:21 > git config remote.origin.url https://gitlab.com/ # timeout=10
20:06:21 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
20:06:21 > git config remote.origin.url https://gitlab.com/ # timeout=10
20:06:21 Fetching upstream changes from https://gitlab.com/
20:06:21 using GIT_ASKPASS to set credentials
20:06:21 > git fetch --tags --progress https://gitlab.com/ +refs/heads/*:refs/remotes/origin/*
20:06:22 skipping resolution of commit remotes/origin/jenkins, since it originates from another repository
20:06:22 > git rev-parse refs/remotes/origin/jenkins^{commit} # timeout=10
20:06:22 > git rev-parse refs/remotes/origin/origin/jenkins^{commit} # timeout=10
20:06:22 Checking out Revision 44f2afa95f961b5b28a482516abf05a17180ea48 (refs/remotes/origin/jenkins)
20:06:22 > git config core.sparsecheckout # timeout=10
20:06:22 > git checkout -f 44f2afa95f961b5b28a482516abf05a17180ea48
20:06:22 Commit message: "chore: test jenkins"
20:06:22 > git rev-list --no-walk dfff4c2214daa01de600086d6d28cf8f9d53cdc5 # timeout=10
20:06:22 Finished: SUCCESS
In my jenkins file I have this sample pipeline
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building..'
}
}
stage('Test') {
steps {
echo 'Testing..'
}
}
stage('Deploy') {
steps {
echo 'Deploying....'
}
}
}
}
Is there something I missed?

You need to choose pipeline type after click new item on jenkins page, rather than choose freestyle/maven type. Input a name and click 'OK', then choose pipeline from SCM

Related

Jenkins - restarting a pipeline on a different node fails on 'git checkout' (git plugin)

There is a job that gets triggered on pull request creation or update. I do not have the permissions to edit the job itself. Sometimes it will fail due to network connection problems, timeouts, etc. In this case I'd like to be able to restart the build with the same parameters. However in case of a restart, the build fails when checking out. It also skips the merge step for some reason
Is it possible to force the git-plugin to 'start fresh' on every build through Jenkinsfile? I was hoping that 'CleanBeforeCheckout' extension would solve my problem, but unfortunately this did not help.
Now for the details. Here's what my checkout step looks like in Jenkinsfile:
gitBranches = [[name: 'origin/pull-requests/**'], [name:"origin/$fromBranch"]]
gitExtensions = [
[
$class: 'UserIdentity',
email: email,
name: name
],
[
$class: 'PreBuildMerge',
options:
[
fastForwardMode: 'FF',
mergeRemote : 'origin',
mergeTarget : toBranch
]
],
[
$class: 'CleanBeforeCheckout',
deleteUntrackedNestedRepositories: false
]
]
println "Cloning repo with url '$gitUrl'"
checkout changelog: true, poll: true, scm: [
$class : 'GitSCM',
branches : gitBranches,
doGenerateSubmoduleConfigurations: false,
extensions : gitExtensions,
gitTool : 'Default',
submoduleCfg : [],
userRemoteConfigs : [[credentialsId: credentialsId, url: gitUrl]]
]
On the first run, the git checkout never fails. Here's what it looks like:
Selected Git installation does not exist. Using Default
The recommended git tool is: NONE
using credential <credential_id>
Cloning the remote Git repository
Cloning repository <repo_url>
> git init /home/jenkins/agent/workspace/<workspace_id> # timeout=10
Fetching upstream changes from <repo_url>
> git --version # timeout=10
> git --version # 'git version 2.7.1'
using GIT_ASKPASS to set credentials
> git fetch --tags --progress <repo_url> +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
Merging Revision <my_feature_hash> (origin/feature/my_feature) to origin/develop, UserMergeOptions{mergeRemote='origin', mergeTarget='develop', mergeStrategy='DEFAULT', fastForwardMode='FF'}
Checking out Revision <target_hash> (origin/develop)
Commit message: "Merge commit '<my_feature_hash>' into HEAD"
First time build. Skipping changelog.
> git config remote.origin.url <repo_url> # timeout=10
> git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
<...>
Seen 27 remote branches
> git show-ref --tags -d # timeout=10
> git rev-parse origin/develop^{commit} # timeout=10
> git config core.sparsecheckout # timeout=10
> git checkout -f origin/develop # timeout=10
> git remote # timeout=10
> git config --get remote.origin.url # timeout=10
using GIT_ASKPASS to set credentials
> git merge --ff <my_feature_hash> # timeout=10
> git rev-parse HEAD^{commit} # timeout=10
<...>
Seen 27 remote branches
> git show-ref --tags -d # timeout=10
> git config core.sparsecheckout # timeout=10
> git checkout -f <target_hash> # timeout=10
However, when restarting the build from the Jenkins UI, the job fails with such output:
Selected Git installation does not exist. Using Default
The recommended git tool is: NONE
using credential <credential id>
Cloning the remote Git repository
Cloning repository <repository_url>
> git init /home/jenkins/agent/workspace/<workspace_id> # timeout=10
Fetching upstream changes <repository_url>
> git --version # timeout=10
> git --version # 'git version 2.7.1'
using GIT_ASKPASS to set credentials
> git fetch --tags --progress <repository_url> +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
Checking out Revision <target_hash> (origin/develop)
> git config remote.origin.url <repository_url> # timeout=10
> git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
<...>
Seen 27 remote branches
> git show-ref --tags -d # timeout=10
> git config core.sparsecheckout # timeout=10
> git checkout -f <target_hash> # timeout=10
Could not checkout <target_hash>
The problem supposedly arises because the git-plugin saves the previous build state somehow. This is bad because my builds will run on different nodes every time.

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 to override an agent in a jenkins pipeline

My goal is to create a pipeline, where every stage is having the same agent, except of 1 stage (Stage E in the example below)
pipeline {
agent {
dockerfile {
filename 'Dockerfile.tester'
args '-v $HOME/.docker:/root/.docker'
}
}
stages {
stage('A') { ... }
stage('B') { ... }
stage('C') { ... }
stage('D') { ... }
stage('E') {
agent {
dockerfile {
filename 'Dockerfile.deploy'
args '-v $HOME/.docker:/root/.docker'
}
}
}
stage('F') { ... }
}
}
What I can do so far is to set globally the agent to none and then set the agent Dockerfile.tester for each stage and for the 1 other stage I set it to Dockerfile.deploy. Any idea how I can set it globally once and then just override it?
Here the new error from if I do it like above:
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://bitbucket.org/*********/*********.git # timeout=10
Cleaning workspace
> git rev-parse --verify HEAD # timeout=10
Resetting working tree
> git reset --hard # timeout=10
> git clean -fdx # timeout=10
Fetching without tags
Fetching upstream changes from https://bitbucket.org/*********/*********.git
> git --version # timeout=10
using GIT_ASKPASS to set credentials BitBucket Login for checkout / pushing
> git fetch --no-tags --progress https://bitbucket.org/*********/********.git +refs/heads/dev:refs/remotes/origin/dev
Checking out Revision ab0d4a522d872a129f53a74f6ecadafb8fd82f11 (dev)
> git config core.sparsecheckout # timeout=10
> git checkout -f ab0d4a522d872a129f53a74f6ecadafb8fd82f11
Commit message: "chore: test"
Cleaning workspace
> git rev-parse --verify HEAD # timeout=10
Resetting working tree
> git reset --hard # timeout=10
> git clean -fdx # timeout=10
[Bitbucket] Notifying commit build result
docker login failed

How to hide the checkout scm step output in console output of Jenkins

My first stage in Jenkinsfile(scripted pipeline) is checkout scm, which gives a brief description about the GitHub checkouts and all revision related things which I don't want to display in Jenkins console output.
Is it possible to hide this in console output of Jenkins.
following console output is from Jenkins for checkout scm step, which I want to hide
Cloning the remote Git repository
Cloning repository https://github.com/forpi/cherry-pik.git
> git init /home/ubuntu/.jenkins/workspace/Dummy-project # timeout=10
Fetching upstream changes from https://github.com/forpi/cherry-pik.git
> git --version # timeout=10
> git fetch --tags --progress https://github.com/forpi/cherry-pik.git +refs/heads/*:refs/remotes/origin/*
> git config remote.origin.url https://github.com/forpi/cherry-pik.git # timeout=10
> git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
> git config remote.origin.url https://github.com/forpi/cherry-pik.git # timeout=10
Fetching upstream changes from https://github.com/forpi/cherry-pik.git
> git fetch --tags --progress https://github.com/forpi/cherry-pik.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 b80c4d6b655429d7f84347b4192461cc3d68283e (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f b80c4d6b69c429d7f84347b4192461cc3d68283e
> git branch -a -v --no-abbrev # timeout=10
> git checkout -b master b80c4d6b655429d7f84347b4192461cc3d68283e
Commit message: "lets try this again"
Checkout scm has option to provide arguments like this and there is a quietoperation mode availabe
checkout([$class: 'SubversionSCM',
additionalCredentials: [],
locations: [[cancelProcessOnExternalsFail: true,
credentialsId: '234243-45654-234randomstuff',
depthOption: 'infinity',
ignoreExternalsOption: true,
local: '.',
remote: 'https://starkindustries/ironman/superGlueForThanosFingers/repo']],
**quietOperation: true,**
workspaceUpdater: [$class: 'UpdateUpdater']])
You can also try this
checkout scm &> /dev/null
This will suppress stdout and stderr, unless the git command fails
or
If you want to checkout using git
git checkout origin master --quiet
git checkout origin master --q
Which is an available option in git.
Hope it helps :)
Try this handy plugin:
https://wiki.jenkins.io/display/JENKINS/Collapsing+Console+Sections+Plugin
It may not be super neat but, it can get the work done.

Resources