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.
Related
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.
I seem unable to create a Jenkins Pipeline job that builds a specific branch, where that branch is a build parameter.
Here's some configuration screenshots:
(i've tried with a Git Parameter and a String Parameter, same outcome)
(I've tried $BRANCH_NAME_PARAM, ${BRANCH_NAME_PARAM} and ${env.BRANCH_NAME_PARAM}, same outcome for all variations)
And the build log:
hudson.plugins.git.GitException: Command "git fetch --tags --progress origin +refs/heads/${BRANCH_NAME_PARAM}:refs/remotes/origin/${BRANCH_NAME_PARAM} --prune" returned status code 128:
stdout:
stderr: fatal: Couldn't find remote ref refs/heads/${BRANCH_NAME_PARAM}
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:1970)
I'm obviously doing something wrong - any ideas on what?
https://issues.jenkins-ci.org/plugins/servlet/mobile#issue/JENKINS-28447
Appears that its something to do with a lightweight checkout. if i deselect this option in my config, my parameter variables are resolved
a bit more detailed with examples combined with VonC answer
1. Configure extended choice parameter named BRANCH:
specify delimiter
specify groovy script or path to groovy file:
def command = "git ls-remote -h $gitURL"
def proc = command.execute()
proc.waitFor()
if ( proc.exitValue() != 0 ) {
println "Error, ${proc.err.text}"
System.exit(-1)
}
def branches = proc.in.text.readLines().collect {
it.replaceAll(/[a-z0-9]*\trefs\/heads\//, '')
}
return branches.join(",")
2. Set Branches to build: $BRANCH
3. Disable "Lightweight checkout" checkbox in the "Pipeline" secton of Jenkins job configuration:
Otherwise job will fail with following message:"stderr: fatal: Couldn't find remote ref refs/heads/${BRANCH"}"
4. Build with parameter executes groovy script and you will then get a dropdown list of branches
I have tried the above solution but it didn't work for me. I have chosen a slightly different approach. I am posting because it will help someone in future.
Goto configures the pipeline job.
Check the option "This project is parameterized"
Add git paramter.
Note: If it doesn't show the option, please goto manage plugins and install git parameter plugin.
My pipeline Configure looks like
Uncheck lightweight checkout and update the "branch to build" in pipeline section.
Save the configuration.
Each time I had a job based on a branch, I had to put a groovy script with EnvInject plugin in order to remove the ref/heads part of the git branch selected in parameters.
If you keep refs/heads/xxx, Jenkins will look for the branch ref/heads/ref/heads/xxx.
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.
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?
I installed the Feature Branch Notifier Plugin in my instance of Jenkins.
I have checked the "Show full length branch name in the build history view" checkbox at jenkins:8080/configure
I am expecting to see the branch names in build history view, but even after restarting Jenkins I am not seeing the branch names in the build history, as can be seen in the enclosed image.
The project issue queue lists no open issues, and when I try to log in to post an issue, I get the message "Proxy Error - The proxy server received an invalid response from an upstream server. The proxy server could not handle the request POST /account/doSignup. Reason: Error reading from remote server Apache/2.2.14 (Ubuntu) Server at jenkins-ci.org Port 443"
Does anyone know how to go about seeing the branch name of builds in the build history view of Jenkins? Thanks!
Albert.
You can use Build Name Setter Plugin, and set Set Build Name something like #${BUILD_NUMBER} - ${GIT_BRANCH}.
Build-Name-setter-plugin no longer works. I tried on 2.319.1, and the setting never appears in the pipline.
The solution I found is to use the build environment variables to apply to your display name for the build in a step script.
Adjust your Jenkinsfile to pull the branch name as a environmental variable (I am using CURRENT_BRANCH_NAME). Then I created a new stage / step, that runs before any other, and ran a script to adjust the displayname there:
pipeline {
agent {any}
environment {
CURRENT_BRANCH_NAME = "${GIT_BRANCH.split('/').size() > 1 ? GIT_BRANCH.split('/')[1..-1].join('/') : GIT_BRANCH}"
}
stages {
stage('Set branch name') {
steps {
script{
currentBuild.displayName = "#"+currentBuild.number+": "+CURRENT_BRANCH_NAME
}
}
}
stages {
stage('Ok now start doing testing') {
steps {
sh '''#!/bin/bash
echo "Im gona test everything"
'''
}
}
}
}
Now when your Jenkins test starts to build, the name will update once the step is complete.
Note: this solution was tested in a single pipeline (not multi-pipeline), and was for a SCM repo integration.
Sources:
Get git branch name in Jenkins Pipeline/Jenkinsfile
https://sleeplessbeastie.eu/2021/01/29/how-to-define-build-name-and-description-in-jenkins/