Pushing back to git repo in Jenkinsfile using GIT_ASKPASS - jenkins-pipeline

Is there a way to push back to the repo that the Jenkins Pipeline checked out using the same process that the code was checked out with (using GIT_ASKPASS)?
I currently have a workaround solution for achieving this by grabbing the credentials like this:
withCredentials([usernamePassword(credentialsId: 'github', passwordVariable: 'GIT_PASS', usernameVariable: 'GIT_USER')]) {
sh('git push https://$GIT_USER:$GIT_PASS#github.com/orgname/private-repo.git master')
}
I'm not a Groovy developer, but found a method in the git-client-plugin that I would like to use. Is there a way to use the following method directly in the Jenkinsfile
launchCommandWithCredentials
https://github.com/jenkinsci/git-client-plugin/blob/master/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java#L1649

Related

Take credentialsId from user

I am building a pipeline template where I want an ability to get credentialId from user.
Currently I have this piece of code which works just fine, but I could not find a better way to parameterize it.
withCredentials([usernamePassword(credentialsId: 'mycredentialId', usernameVariable: 'mycredentialId_USR', passwordVariable: 'mycredentialId_PSW')
I tried using environment variable like this to substitute the credentialId.
environment {
userCredentialID = "${userCredentialID}"
}
and then use that variable inside pipeline step like this, but it does not work.
withCredentials([usernamePassword(credentialsId: '${userCredentialID}', usernameVariable: 'userCredentialID_USR', passwordVariable: 'userCredentialID_PSW')
Any leads on how to parameterize a credential would be very helpful.
Thank you very much!

Using a private go module on Gitlab as import : "Unknown revision"

I cannot get this to work, even after checking other topics on stackoverflow. My project on gitlab.com/my_company/backend needs a module, found at gitlab.com/my_company/pkg/auth.
Locally, I've setup GOPRIVATE / git's configuration to make it work (and it works), though in Gitlab's CI Pipelines on a merge request, this fails.
Pipeline log / go.mod
I've added some debugging logs just to make sure everything was setup like I thought. Here's a failing pipeline's log :
$ git config --global url."ssh://git#gitlab.com/my_company/".insteadOf "https://gitlab.com/my_company/"
$ git config --global url."git#gitlab.com:".insteadOf "https://gitlab.com/"
$ git config -l | grep instead
url.ssh://git#gitlab.com/my_company/.insteadof=https://gitlab.com/my_company/
url.git#gitlab.com:.insteadof=https://gitlab.com/
$ env | grep GOPRIVATE
GOPRIVATE=gitlab.com/my_company
$ go mod download
go: gitlab.com/my_company/pkg/auth#v1.1.0: reading gitlab.com/my_company/pkg/auth/auth/go.mod at revision auth/v1.1.0: unknown revision auth/v1.1.0
One weird part of this log I've found is :
reading gitlab.com/my_company/pkg/auth/auth/go.mod - why is it repeating auth/auth? It actually happened once before locally, but it was because I wrote "github" instead of "gitlab" :)
The relevant go.mod line just in case :
require (
gitlab.com/my_company/pkg/auth v1.1.0 // indirect
)
Repository tags
Here are the tags setup on the repository gitlab.com/my_company/pkg :
$ git tag -l
auth/v1.0.0
auth/v1.1.0
cache/v1.0.0
cache/v1.0.1
$ git ls-remote --tags
From git#gitlab.my_company/pkg.git
9efcb02d5489adaac9d525dcb496d868d65e856a refs/tags/auth/v1.0.0
13730d4f61df978c6d690fd2678e2ed924808e0c refs/tags/auth/v1.1.0
2b8dff0ec1b737d975290720933180a9b591a1db refs/tags/cache/v1.0.0
9a3e598bbf83bea57b29d8a908b514861ae37b12 refs/tags/cache/v1.0.1
I'm not that familiar with Gitlab CI so I'm out of things to try. Any ideas?
Thank you!
Update: I finally got gitlab-runner installed so I could try running the yml directly, no luck. It still works locally (not a big surprise).
In you project should be file .gitlab-ci.yml and you can add GOPRIVATE variable to your CI and runner will use it for you project.
More details how to add env vars:
https://docs.gitlab.com/ee/ci/variables/#create-a-custom-cicd-variable-in-the-gitlab-ciyml-file

ElasTest Jenkins Plugin does not send test logs

Currently I am evaluating ElasTest. I also try the Jenkins Plguin, but the console output is not sent to ElasTest.
I have tried the example code snippet:
elastest(surefireReportsPattern: '**/target/surefire-reports/TEST-*.xml', project: 'Jenkins Examples') {
stage ('Executing Test') {
echo 'Set up test environment'
mvnHome = tool 'maven-3.3.9'
echo 'Cloning repository'
git 'https://github.com/elastest/demo-projects'
echo 'Run test'
sh "cd ./unit/junit5-unit-test;'${mvnHome}/bin/mvn' -B -DforkCount=0 test"
step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*.xml'])
}
}
I need to know a few things:
Which version of ElasTest you are using and where you are deploying it.
Are you using your own Jenkins instance or the Jenkins instance integrated into ElasTest?
If you are using a Jenkins instance of your own, make sure that Jenkins can communicate with ElasTest and configure the plugin correctly. If ElasTest is on a server, open the necessary ports as indicated in the documentation: https://elastest.io/docs/deploying/ubuntu/
You can find more information on how to use the ElasTest plugin here: https://elastest.io/docs/tutorials/e2e-testing/

How to pull git repository using Rugged ruby gem

How do I pull changes from remote repo using Rugged ruby gem. Most of question on this topic are outdated as api has changed. I am using a simple repo with nothing complex. I found below code on one of the stackoverflow question but unfortunately its outdated
remote = repo.remotes['origin']
remote.fetch(credentials: credentials)
remote.save
Also remote.fetch gives {:total_objects=>0, :indexed_objects=>0, :received_objects=>0, :local_objects=>0, :total_deltas=>0, :indexed_deltas=>0, :received_bytes=>0} and no changes are pulled
The docs have this example:
remote = Rugged::Remote.lookup(#repo, 'origin')
remote.fetch({
transfer_progress: lambda { |total_objects, indexed_objects, received_objects, local_objects, total_deltas, indexed_deltas, received_bytes|
# ...
}
})

Does polling work with lightweight checkout?

I'm using "checkout build script from scm" option, paired with lightweight checkout.
I would like to add repository polling to that.
This is Jenkinsfile that I use:
pipeline {
agent any
triggers {
pollSCM('H/1 * * * *')
}
stages {
stage('Checkout') {
steps {
checkout([
$class : 'GitSCM',
branches : [[name: 'master']],
userRemoteConfigs : [[url: 'file:///home/my-secret-home/workspace/pipeline-test']]])
}
}
stage('Echo!') {
steps {
sh 'echo TEST'
}
}
}
}
Although job is running, git polling log tries to convince me that 'Polling has not run yet.'
Is configuring such behavior possible at all?
No, it doesn't work.
With lightweight checkout, the mapping to the remote branches are lost, so git doesn't know where to look for further updates.
You can also confirm this by running git pull on the local repository. It returns:
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> master

Resources