No credentials specified Fetching changes from the remote Git - jenkins

I'm trying to do my first Jenkins job. I want to run a java project that uploaded to git but I get this error while building the job:
No credentials specified Fetching changes from the remote Git
repository ERROR: Error fetching remote repo 'origin'
hudson.plugins.git.GitException: Failed to fetch from
https://gitlab/engineering/automation/create_pass_criteria.git at
hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:908) at
hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1123) at
hudson.plugins.git.GitSCM.checkout(GitSCM.java:1159) at
org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:129)
at
org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:97)
at
org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:84)
at
org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown
Source) at java.util.concurrent.FutureTask.run(Unknown Source) at
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at
java.lang.Thread.run(Unknown Source) Caused by:
hudson.plugins.git.GitException: Command "git fetch --tags --progress
https://gitlab/engineering/automation/create_pass_criteria.git
+refs/heads/:refs/remotes/origin/" returned status code 128: stdout: stderr: fatal: unable to access
'https://gitlab/engineering/automation/create_pass_criteria.git/':
Peer's certificate issuer has been marked as not trusted by the user.
This is my groovy jenkinsfile:
#!groovy
pipeline {
agent { node { label "agent_314" } }
stages {
stage("Build") {
steps {
echo "Building..."
git 'https://gitlab/engineering/automation/create_pass_criteria.git'
sh './mvnw clean compile'
}
}
}
}

Add credentials in Jenkins for your git repository and then supply the credentials in pipeline for git checkout
To Add credentials -
Jenkins -> Manage Jenkins ->Manage Credentials
Under Stores scoped to Jenkins , click on Jenkins then click on Global Credentials (unrestricted) .
From left side click on Add Credentials and Define the username and password for your git repository and note down credentialsID
Add below line in your jenkinsfile
git credentialsId: 'your git credentials ID', url: 'https://gitlab/engineering/automation/create_pass_criteria.git'
For more information you can refer here

Related

Jenkins pipeline store SSH credentials as parameter

I have the following pipeline code:
pipeline {
agent any
environment {
SSH_KEY_PARAMETER = credentials('SSH_KEY')
}
stages {
stage('Checkout') {
steps{
script{
git credentialsId: 'SSH_KEY_PARAMETER', url: 'git#github.com:SomeUser/PrivateRepo.git', branch: 'main'
}
}
}
}
}
Expected output is cloning the repo.
Actual output:
ERROR: Error fetching remote repo 'origin'
hudson.plugins.git.GitException: Failed to fetch from git#github.com:SomeUser/PrivateRepo.git
at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:1001)
at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1242)
at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1302)
at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:129)
at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:97)
at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:84)
at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: hudson.plugins.git.GitException: Command "git fetch --tags --force --progress -- git#github.com:SomeUser/PrivateRepo.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout:
stderr: git#github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2681)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:2102)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$500(CliGitAPIImpl.java:86)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:624)
at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:999)
If I change the pipeline code to use the credential ID instead of the variable it works:
pipeline {
agent any
environment {
SSH_KEY_PARAMETER = credentials('SSH_KEY')
}
stages {
stage('Checkout') {
steps{
script{
git credentialsId: 'SSH_KEY', url: 'git#github.com:SomeUser/PrivateRepo.git', branch: 'main'
}
}
}
}
}
In my pipeline I need to manage a bunch of SSH keys depending on the environment, so I need to be able to parameterize the SSH key, am I missing something here or is it not possible to assign an SSH key as variable?
Your environment directive with use of the credentials helper method is syntactically correct. Your syntax for accessing the variable is incorrect. You are assigning a literal string value 'SSH_KEY_PARAMETER' to the credentialsId argument. The actual value also is stored in the env object, so you would access it with the appropriate key env.SSH_KEY_PARAMETER. The usage would then look like:
credentialsId: env.SSH_KEY_PARAMETER
However, the git step is expecting the credentials ID for that argument and not the actual credentials. Therefore, you need to also assign the value of the credentialsId in the environment directive instead:
SSH_KEY_PARAMETER = 'SSH_KEY'
Putting it all together, we have:
environment {
SSH_KEY_PARAMETER = 'SSH_KEY'
}
stages {
...
git credentialsId: env.SSH_KEY_PARAMETER, url: 'git#github.com:SomeUser/PrivateRepo.git', branch: 'main'
}

Jenkins pipeline not creating workspace folders when other pipelines are

I am creating a new Pipeline on an existing, functioning Jenkins server.
At the moment it is a simple multibranch pipeline with the Jenkinsfile, and a deploy.yml
The issue is that the workspace folder is not being generated.
When I do ls /var/lib/jenkins/workspace I get a list of all the other deploy jobs that have created workspaces, and a workspace.txt file where I can see the names of the workspaces that SHOULD have been created, but there is no folder for that workspace.
If I do mkdir /var/lib/jenkins/workspace/expectedname and touch /var/lib/jenkins/workspace/expectedname/deploy.yml and run it, it creates a workspace called /var/lib/jenkins/workspace/expectedname#tmp which is empty, and puts the retreived files in the one i created. If I delete the #tmp folder, it works. but it breaks again without advance notice, and it isn't feasible to keep having to create the fodler manually.
Jenkinsfile
#!groovy
node {
currentBuild.displayName = "${env.BRANCH_NAME}-${env.BUILD_NUMBER}"
config = readYaml file: "${WORKSPACE}/deploy.yml"
}
pipeline {
agent any
stages {
stage ('Fiddling'){
steps {
echo "***************** TAG: ${config.scm.tag} *****************"
}
}
}
}
deploy.yml
env:
wwwroot: "/var/www/siteroot"
type: "test"
ssh:
servername: "mysecretserver.url"
username: "jenkins"
conf:
siteTitle: "Site Title"
siteFQDN: "site.subdomain.domain.tld"
defaultFromEmail: "test#domain.tld"
sessionname: "SessionName"
mysql:
host: "localhost"
database: "mycooldb"
username: "dbuser"
password: "s3cret"
scm:
tag: "v.1.2"
jenkins log
15:28:41 Started by user JoSSte
15:28:41 > git rev-parse --is-inside-work-tree # timeout=10
15:28:41 Setting origin to git#bitbucket.org:t******/****.git
15:28:41 > git config remote.origin.url git#bitbucket.org:t******/****.git # timeout=10
15:28:41 Fetching origin...
15:28:41 Fetching upstream changes from origin
15:28:41 > git --version # timeout=10
15:28:41 > git --version # 'git version 2.17.1'
15:28:41 > git config --get remote.origin.url # timeout=10
15:28:41 using GIT_SSH to set credentials YYYYYY
15:28:41 > git fetch --tags --progress -- origin +refs/heads/*:refs/remotes/origin/* # timeout=10
15:28:42 Seen branch in repository origin/master
15:28:42 Seen branch in repository origin/noscm
15:28:42 Seen branch in repository origin/stable
15:28:42 Seen branch in repository origin/working
15:28:42 Seen 4 remote branches
15:28:44 Obtained Jenkinsfile from 5374b39f33bf6ye904134e845d60fae76a97bb09
15:28:44 Running in Durability level: MAX_SURVIVABILITY
15:28:44 [Pipeline] Start of Pipeline
15:28:44 [Pipeline] node
15:28:44 Running on Jenkins in /var/lib/jenkins/workspace/tc-d-tr_noscm
15:28:44 [Pipeline] {
15:28:44 [Pipeline] readYaml
15:28:44 [Pipeline] }
15:28:44 [Pipeline] // node
15:28:44 [Pipeline] End of Pipeline
15:28:44 java.io.FileNotFoundException: /var/lib/jenkins/workspace/tc-d-tr_noscm/deploy.yml does not exist.
15:28:44 at org.jenkinsci.plugins.pipeline.utility.steps.conf.ReadYamlStep$Execution.doRun(ReadYamlStep.java:113)
15:28:44 at org.jenkinsci.plugins.pipeline.utility.steps.AbstractFileOrTextStepExecution.run(AbstractFileOrTextStepExecution.java:32)
15:28:44 at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
15:28:44 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
15:28:44 at java.util.concurrent.FutureTask.run(FutureTask.java:266)
15:28:44 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
15:28:44 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
15:28:44 at java.lang.Thread.run(Thread.java:748)
15:28:44 Finished: FAILURE
Why is this job failing when all the others are not? - the only thing I can see different is that the other jobs I have don't use readYaml
Update
I made a new repo, with basically the same structure, without readYaml, on the same jenkins server - this hasn't failed in the same way - I will try to ad that to it, and see if it breaks...

Unable to logon to Github using Jenkins pipeline

I want to clone a github repository using Jenkins decriptive pipeline. I use the following code :
pipeline {
agent any
stages {
stage('Checkout') {
steps {
script {
// The below will clone your repo and will be checked out to master branch by default.
git credentialsId: 'jenkins-user-github', url: 'https://github.com/aakashsehgal/FMU'
// Do a ls -lart to view all the files are cloned. It will be clonned. This is just for you to be sure about it.
//bat "ls -lart ./*"
// List all branches in your repo.
bat "git branch -a"
// Checkout to a specific branch in your repo.
}
}
}
}
}
But Jenkins cannot log on to the Github account. I have double checked the credentials and they are correct. Here is the error :
Started by user Aakash Sehgal
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in C:\Program Files (x86)\Jenkins\workspace\GitHub
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Checkout)
[Pipeline] script
[Pipeline] {
[Pipeline] git
using credential jenkins-user-github
> git.exe rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git.exe config remote.origin.url https://github.com/aakashsehgal/FMU # timeout=10
Fetching upstream changes from https://github.com/aakashsehgal/FMU
> git.exe --version # timeout=10
using GIT_ASKPASS to set credentials
> git.exe fetch --tags --force --progress -- https://github.com/aakashsehgal/FMU +refs/heads/*:refs/remotes/origin/* # timeout=10
ERROR: Error fetching remote repo 'origin'
hudson.plugins.git.GitException: Failed to fetch from https://github.com/aakashsehgal/FMU
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:124)
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(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: hudson.plugins.git.GitException: Command "git.exe fetch --tags --force --progress -- https://github.com/aakashsehgal/FMU +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout:
stderr: Logon failed, use ctrl+c to cancel basic credential prompt.
warning: invalid credential line: get
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/aakashsehgal/FMU/'
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] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: Error fetching remote repo 'origin'
Finished: FAILURE
I also searched online and someone mentions that it could be because of https. However not clear how to resolve it. Any idea what could i be doing wrong ?
So after some research I finally found the answer. The problem was that Jenkins wasnt able to resolve the path of the Git executable.
Go to Manage Jenkins -> Global Tool Configuration. Under the Git Tab, we need to give the absolute path of the Git executable. For a standard installation, the default path is C:\Program Files\Git\bin\git.exe. Save this and tried again. Works.

Trying to run simple JenkinsFile getting 403 forbidden error

I am trying to run a simple JenkinsFile
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'java -version'
}
}
stage('Test') {
steps {
echo 'Testing..'
}
}
}
}
As you can see there isn't much here. But I am getting a forbidden error even though I configured an account which is the owner of the repository. I posted the error as-it-is just changed the username/repository to my-account/my-repo
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://github.com/my-account/my-repo.git # timeout=10
Fetching without tags
Fetching upstream changes from https://github.com/my-account/my-repo.git
> git --version # timeout=10
using GIT_ASKPASS to set credentials Pull Request Builder
> git fetch --no-tags --progress https://github.com/my-account/my-repo.git +refs/heads/master:refs/remotes/origin/master
ERROR: Error fetching remote repo 'origin'
hudson.plugins.git.GitException: Failed to fetch from https://github.com/my-account/my-repo.git
at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:817)
at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1084)
at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1115)
at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:109)
at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:83)
at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:73)
at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1$1.call(AbstractSynchronousNonBlockingStepExecution.java:47)
at hudson.security.ACL.impersonate(ACL.java:221)
at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1.run(AbstractSynchronousNonBlockingStepExecution.java:44)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:473)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:748)
Caused by: hudson.plugins.git.GitException: Command "git fetch --no-tags --progress https://github.com/my-account/my-repo.git +refs/heads/master:refs/remotes/origin/master" returned status code 128:
stdout:
stderr: error: The requested URL returned error: 403 Forbidden while accessing https://github.com/my-account/my-repo.git/info/refs
fatal: HTTP request failed
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:1924)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1643)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$300(CliGitAPIImpl.java:71)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:352)
at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:815)
... 13 more
One solution is to switch to SSH when checking out the code.(Easier and always works)
If switching is not an option, then use username/password or better, generate a secret key and use that to access your repo.
Since your using it for Pull Request I'm guessing checkout scm, if this is case by default it will use https- which failed for me using 403, unfortunately the issue lied with my git being an old version 1.7/1.8 not sure exactly. Upgrading it worked like a charm :)
Hope it helps :)

Clone from bitbucket private repository using jenkins Pipeline as code

im using jenikins pipeline as code to clone a git project which is in private bitbucket repository(stash repository). i used this code block to clone the project in my pipeline script.
node {
//checkout from master
stage 'checkout'
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'MyID', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) {
git url: 'https://paulrda#devMyCompany.org/stash/scm/test_automation.git' , branch: 'development'
}
}
'MyID' is the credential ID and my username and password is correct.i save my credentials in global credentials feature in jenkins. but i get this error when i build the jenkins task.
ERROR: Error fetching remote repo 'origin'
hudson.plugins.git.GitException: Failed to fetch from https://paulrda#devMyCompany.org/stash/scm/test_automation.git
at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:803)
at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1063)
at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1094)
at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:109)
at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:83)
at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:73)
at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1$1.call(AbstractSynchronousNonBlockingStepExecution.java:47)
at hudson.security.ACL.impersonate(ACL.java:221)
at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1.run(AbstractSynchronousNonBlockingStepExecution.java:44)
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:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: hudson.plugins.git.GitException: Command "git fetch --tags --progress https://paulrda#devMyCompany.org/stash/scm/test_automation.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout:
stderr: fatal: Authentication failed for 'https://paulrda#devMyCompany.org/stash/scm/test_automation.git/'
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:1745)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1489)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$300(CliGitAPIImpl.java:64)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:315)
at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:801)
In my mac machine under my paulrda account i can successfully clone my project using jenkins pipeline script but when i change to another account and run jenkins i get this error. still i cant understand why i get this error. please provide a solution to this problem.
my configurations.
Jenkins version : 2.19.2
Credentials Plugin : 2.1.8
Git plugin : 3.0.0
Git client plugin : 2.1.0
It's failing to authenticate because you are not passing the credentials to the git call correctly.
Since you are using the Git plugin and not a shell command, there's really no need to use withCredentials at all. You can pass the credentialsId directly to git call, like that:
stage('checkout') {
git credentialsId: 'MyID', url: 'https://devMyCompany.org/stash/scm/test_automation.git', branch: 'development'
}

Resources