How to execute git bundle command on some branch on jenkins - 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.

Related

Docker compose command fails but doesn't trigger jenkins failure

I have a jenkins pipeline with docker for node/npm builds. If they fail, Jenkins still shows the build as succeeding. How do I make it detect the failure?
My jenkins file looks like this:
stage('Deploy Staging') {
when {
branch 'staging'
}
steps {
sh 'ssh root#xxx.xxx.xxx.xx "cd project; git checkout staging; git fetch origin staging; git reset --hard origin/staging; COMPOSE_FILE=build/staging/docker-compose.yml make build-all; COMPOSE_FILE=build/staging/docker-compose.yml make up;"'
discordSend description: "Staging pipeline build status: ${currentBuild.currentResult}", link: env.BUILD_URL, result: currentBuild.currentResult, title: JOB_NAME, webhookURL: 'url'
}
}
This is a solution that is not specific to npm build failures, but you could consider using the Log Parser plugin. This plugin allows you to set console parsing rules which takes the console output of the log and scans it for parsing rules that you specify. You can then make the build fail/pass/warn when text that matches these rules are encountered.
So in your case you could make a rule that matches to your npm build failure message and force the build to fail when the build failure occurs.
I ended up having to make sure the make build-all had an explicit || exit 1 at the end for this to exit correctly.

Unable to do git commit in jenkins dsl

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"')
}
}

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.

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.

Jenkins Workflow. Can't execute batch script with git command

I have installed instance of Jenkins on Windows and I use Workflow plugin to configure build steps of job.
Now I'am trying to get list of tags available in my branch.
It seems that the only way to do that is to call batch command(I've omitted specific options)
node('master') {
stage concurrency: 1, name: 'Test & Build'
git branch: branchName, credentialsId: bitbucketCredentialsId, url: repositoryUrl
bat 'call git.exe tag'
// bat 'git tag'
}
But when I build the job I always get the following error:
'git.exe' is not recognized as an internal or external command,
operable program or batch file.
Jenkins is configured to work with GIT.
System PATH variable contains path to git binary.
Running above command by using cmd directly in workspace folder gives successful result.
Could someone please suggest another points which I should check?
Thanks to all who have tried to help.
I found the issue.
I've forgot to reboot my machine after system PATH variable update.
Can you try this solution? (Setting Up section)
https://github.com/jenkinsci/workflow-plugin/blob/master/TUTORIAL.md
Delete your Git installation and add Jgit?

Resources