Unable to do git commit in jenkins dsl - jenkins

Is it possible to do git commit using jenkins dsl.I tried:
shell() - doesn't recognize git command
#library()- i have working library that does take care of git commit. It is working in jenkins pipeline job but not in dsl(throws error at first line where #libary() or library "libname" is used)
sshAgent(credentials){} - didn't work in dsl
I tried to use git plugins but there is no commit for existing plugins.

The shell command will work if your build agent contains a Git installation. Either pre-install Git on your agents or use the Global Tool Configuration to setup a Git installation and then use the Tool Environment Plugin to get a pointer to that installation.
If you setup a Git installation called GIT2 in Global Tool Configuration, the following should work depending on your OS and the installation method.
job('example') {
wrappers {
toolenv('GIT2')
}
steps {
shell('$GIT2_HOME/bin/git commit -am "test"')
}
}

Related

How to execute git bundle command on some branch on jenkins

My issue is what to write to execute git bundle command after cloning some branch, then copy the output file to remote machine :
stage('Cloning project') {
git branch: 'release_branch', url: 'repo_url'
}
stage('create bundle') {
git bundle ..........
}
Not all git commands are available as native workflow steps. As an alternate, I myself have successfully used shell scripts precisely for this purpose in which all non supported command are wrapped in a shell script. These scripts are part of the git repository itself (usually in build directory, can be anything you wish though).
For your use-case, you can use similar approach and wrap "git bundle ..." along with file transfer (using sftp, curl, aws cli or what ever works in your environment) in a single shell script. Say this file is checked in at path build/bundle_transfer.sh then you can invoke this script using following syntax within your pipeline.
stage('Cloning project') {
git branch: 'release_branch', url: 'repo_url'
}
stage('create bundle') {
sh 'build/bundle_transfer.sh'
}
Make sure your script is checked in with executable attributes set otherwise it'll fail execution in jenkins environment. Should you wish to use any write operation on remote repository (not needed for "git bundle ...") then you'll likely end up using Credentials Binding in your pipeline.

SVN tagging a release in Jenkins Pipeline

I'm fairly new to Jenkins Pipeline groovy scripts but I have written a script which performs an SVN Checkout, NuGet Restore, etc and eventually copying an msi file to the server. After successfully packaging the .msi file I would like to tag the source in SVN but I'm struggling to find a method of doing this.
The svn check out is performed as follows:
def svn = checkout scm
I was sort of hoping I could just to the following:
svn = copy scm "svn://svn/MyPath/MyApp/tag/${versionNumber}" -m "V${versionNumber}"
I could obviously use the bat command and specify the full svn command but then I'd have to enter the Jenkins credentials into the groovy script which is not ideal.
Any help/pointers would be greatly appreciated.
If you are tied to Subversion you could extract bits of code from this post, but I would HIGHLY recommend, if you can, to migrate your code to Git as the tooling support in Git is so much better and where the world has already gone.
In Git this is all I have to do tag a branch as part of my Jenkins multibranch pipeline:
success {
script {
if( "${env.BRANCH_NAME}" == "develop" ) {
bat "git tag ${JOB_NAME}_${BUILD_NUMBER}"
bat "git push ${env.REPO} --tags"
}
}
}
${env.REPO} is my clone URL.
SO much easier.

How do I chain Jenkins pipelines from a checked out git repo?

I want to checkout a git repo and then run its build so I tried:
sh "git clone --depth 1 -b master git#github.com:user/repo.git"
build './repo'
but that yields:
ERROR: No item named ./repo found
I've tried to use dir('repo') but apparently that errors when you run it from within docker (because kubernetes is stuck on an old version of docker that doesnt support this).
Any idea on how to run the build pipeline from the checked out repo?
The 'build' pipeline steps expect a job name, not a pipeline folder with a Jenkinsfile in its root folder.
The correct way to do this is to set the pipeline job with the Jenkinsfile, as described here ('In SCM' section), and call it by its Job name from your pipeline.
Pipelines are not built for chaining unless you use shared libraries where you put the Pipeline code in a Groovy class or as a step, but that it is a subject for a full article.

get git commit SHA within Jenkins pipeline with JGit

I have a Jenkins with the Github organization plugin that scans my organization and build all the branches/PRs of all the repositories within the organization that have a Jenkinsfile.
It works great, but I would like to retrieve for each build the commit SHA, in order to tag Docker images with both the branch name and the commit SHA.
Getting the branch name works great with env.BRANCH_NAME, however I cannot find any way to get the commit SHA.
The catch is that we are using JGit, so I cannot use git log to retrieve it.
I tried having a look at what is contained in the ENV using sh 'printenv', but there's nothing of any use there.
I also tried the following:
def checkoutResults = checkout scm
echo 'checkout results: ' + checkoutResults
but this yields the following result:
checkout results: [:]
Even though I cannot get the revision from my pipeline, Jenkins is getting it alright, as I can see in the logs:
...
Obtained Jenkinsfile from 98062e5f651ca698f4303c3bb8d20665ce491294
...
Checking out Revision 98062e5f651ca698f4303c3bb8d20665ce491294 (docker)
I am running the following versions:
Jenkins 2.73.3
Git plugin plugin 3.3.0
Pipeline: SCM Step plugin 2.6
Would appreciate any help in retrieving the commit SHA / revision in this particular situation.
The git plugin did not include the fix until the 3.3.2 release. You'll need to update to at least git plugin 3.3.2.
The current git plugin release as of 23 Nov 2017 is 3.6.4. It includes significant additions and changes for multi-branch pipelines.
There is also a known bug in the reporting of NAME and EMAIL values which has a regression test that confirms the bug affects all implementations (git and jgit). You can use that regression test as an example of using those values if needed.
If you cannot update from git plugin 3.3.0 to 3.3.2, you may be able to use the JGit classes from within the pipeline script to perform the same type of query as was offered with command line git in another answer to the question. I have never done it, but I believe it is possible.
I created a small groovy function
def getCommitSha(){
return sh(returnStdout: true, script: 'git rev-parse HEAD')
}
you can add it to your pipeline , or to your shared library if you are using one ( if not it's a good time to start ... :-) )

Jenkins Pipeline as Code with Docker Error

For one of my projects that I have on GitHub, I wanted to build it as a docker image and push it to my docker hub. The project is a sbt one with a Scala codebase.
Here is how my JenkinsFile is defined:
#!groovy
node {
// set this in Jenkins server under Manage Jenkins > Credentials > System > Global Credentials
docker.withRegistry('https://hub.docker.com/', 'joesan-docker-hub-credentials') {
git credentialsId: '630bd271-01e7-48c3-bc5f-5df059c1abb8', url: 'https://github.com/joesan/monix-samples.git'
sh "git rev-parse HEAD > .git/commit-id"
def commit_id = readFile('.git/commit-id').trim()
println comit_id
stage "build" {
def app = docker.build "Monix-Sample"
}
stage "publish" {
app.push 'master'
app.push "${commit_id}"
}
}
}
When I tried to run this from my Jenkins server, I get the following error:
java.io.FileNotFoundException
at jenkins.plugins.git.GitSCMFile$3.invoke(GitSCMFile.java:167)
at jenkins.plugins.git.GitSCMFile$3.invoke(GitSCMFile.java:159)
at jenkins.plugins.git.GitSCMFileSystem$3.invoke(GitSCMFileSystem.java:161)
at org.jenkinsci.plugins.gitclient.AbstractGitAPIImpl.withRepository(AbstractGitAPIImpl.java:29)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.withRepository(CliGitAPIImpl.java:65)
at jenkins.plugins.git.GitSCMFileSystem.invoke(GitSCMFileSystem.java:157)
at jenkins.plugins.git.GitSCMFile.content(GitSCMFile.java:159)
at jenkins.scm.api.SCMFile.contentAsString(SCMFile.java:338)
at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:101)
at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:59)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:232)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:404)
Finished: FAILURE
Since this is running inside a VM on Azure, I thought the VM was not able to reach outside, but that seems not to be the case as I was able to ssh into the VM and git pull from the Git repo. So what is the problem here? How could I make this work?
for me unchecking "lightweight checkout" fixed the issue
I experienced the exact same error. My setting:
Pipeline build inside a dockerized Jenkins (version 2.32.3)
In the configuration of the job, I specified a check out into a subdirectory: Open the configuration, e.g. https://myJenkins/job/my-job/configure. At the bottom, see section Pipeline -> Additional Behaviours -> Check out into a sub-directory with Local subdirectory for repo set to, e.g., my-sub-dir.
Expectation: Upon check out, the Jenkinsfile ends up in my-sub-dir/Jenkinsfile.
Via the option Script path, you configure the location of the Jenkinsfile so that Jenkins can start the build. I put my-sub-dir/Jenkinsfile as value.
I then received the exception you pasted in your question. I fixed it by setting Script Path to Jenkinsfile. If you don't specify a sub-directory for check out, then still try double checking values for Script Path.
Note: I have another Jenkins instance at work. There I have to specify Script Path including the custom check out sub-directory (as mentioned in Expectation above).
GO TO Job-->Config-->Pipline and uncheck checkbox lightweight checkout"
lightweight checkout : selected, try to obtain the Pipeline script contents >directly from
the SCM without performing a full checkout. The advantage of this mode
is its efficiency; however, you will not get any changelogs or polling
based on the SCM. (If you use checkout scm during the build, this will
populate the changelog and initialize polling.) Also build parameters
will not be substituted into SCM configuration in this mode. Only
selected SCM plugins support this mode.

Resources