How to use jenkins version number plugin in Jenkinsfile? - jenkins

Trying this step in Jenkinsfile with "version number plugin" installed:
stage("Build") {
echo "Building..."
TAG = ${BUILD_DATE_FORMATTED, "yyyyMMdd"}-develop-${BUILDS_TODAY}
sh "docker build -t $IMAGE:$TAG ."
}
And getting this error:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 15: unexpected token: BUILD_DATE_FORMATTED # line 15, column 15.
TAG=${BUILD_DATE_FORMATTED, "yyyy-MM-dd"}-develop-${BUILDS_TODAY}
^
1 error
What is correct way to use this plugin in Jenkinsfile?

You need to use it as a step.
tag = VersionNumber (versionNumberString: '${BUILD_DATE_FORMATTED, "yyyyMMdd"}-develop-${BUILDS_TODAY}')
Take a look at https://your_jenkins_url.com/pipeline-syntax/ and check all the options for the VersionNumber step in the snipped generator.

in Jenkinsfile.. try this
env.BN = VersionNumber([
versionNumberString :'${BUILD_MONTH}.${BUILDS_TODAY}.${BUILD_NUMBER}',
projectStartDate : '2017-02-09',
versionPrefix : 'v1.'
])

In stage use the script block to run for version numbering
stage("Build") {
script {
TAG = VersionNumber(versionNumberString: '${BUILD_DATE_FORMATTED, "yyyyMMdd"}-develop-${BUILDS_TODAY}')
echo "Building..."
sh "docker build -t $IMAGE:$TAG ."
}
}

Related

Jenkinsfile is failing with error #tmp/durable-df843027/script.sh: line 1: terraform: command not found

I'm trying to run a terraform commands from Jenkinsfile stage. The code I'm using is as below:
node {
checkout(scm)
stage ('Templates Deployment'){
sh "terraform init"
}
}
This fails with the error as :
+terraform init
/var/lib/jenkins/workspace/Terraform-Code/#tmp/durable-df843027/script.sh: line 1: terraform: command not found
Terraform is installed on the Jenkins server. When I execute the terraform init command from the server(CLI), it works fine.
But while running it from the Jenkinsfile(console) it's throwing this error.
Can someone please suggest how this error can be resolved? Any help to execute terraform commands via Jenkinsfile is highly appreciated.
Configure Terraform
Go to Manage Jenkins > Global Tool Configuration > It will display Terraform on the list.
give full path of terraform binary or set PATH before terraform init
`node {
checkout(scm)
stage ('Templates Deployment'){
sh """
PATH=/bin/terraform
terraform init"
}
}`
You can set the PATH inside the environment block:
pipeline {
agent any
environment {
PATH = "/usr/local/bin/:$PATH"
}
stages{
stage("first stage"){
steps{
sh "cd /Users/<user>/Terraform/proj1"
sh "pwd"
sh "terraform"
}
}
}
}

Class path not recognized in jenkins when using command "mvn test"

I have a project that I want to run via Jenkins and one of the steps in my JenkinsFile is like this (example):
stage ('Testing Stage') {
steps {
sh 'mvn -Dtest=com\folder1\folder1\myClass test'
}
}
If I run the command in the IDE terminal, the tests run without problem but in Jenkins, the slashes are trimmed so I get an error.
How should be the syntax in the Jenkins file?
EDIT:
This is the error I get:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 37: unexpected char: '\' # line 37, column 35.
sh "mvn -Dtest=com\folder1\folder1\myClass test"
^
The solution is to use the following syntax:
sh "mvn -Dtest=com/folder1/folder1/myClass test"

What is a rule to make target in Docker container?

To learn how to manipulate Docker images from within Jenkins, I am reading this link.
What is a "rule to make a target" in Docker? The simple example below is failing because there is "no rule to make a target". What does this mean in Docker?
The Error And The Code That Triggers The Error
The sh 'make test' line of code in a Jenkinsfile from the link above is throwing an error when run inside the following block:
testImage.inside {
sh "whoami"
sh 'make test'
}
The actual error that Jenkins throws when trying to interpret the sh 'make test' line of code is:
make test— Shell Script<1s
[ple-dockere-containered-app-WWNVRTE6XFKMI4JPEVK2F2U3HOGDZICATW6XBFM2IQUW5PAG5FWA] Running shell script
+ make test
make: *** No rule to make target 'test'. Stop.
The complete Jenkinsfile is:
node {
// Clean workspace before doing anything
deleteDir()
try {
stage ('Clone') {
checkout scm
}
stage ('Build') {
def testImage = docker.build("test-image", "./app")
testImage.inside {
sh "whoami"
sh 'make test'
}
}
} catch (err) {
currentBuild.result = 'FAILED'
throw err
}
}
Note that the make test command is being run inside the container.

Jenkins: Unable to create directory using the date

I want to create a directory using jenkins with the days date.
I'm using the new jenkins declarative syntax.
When i run the build as described in the job below it fails.
The mkdir command though, works perfectly well on the console.
pipeline {
agent any
stages {
stage('Prepare') {
steps {
echo "Checking for the existence of a debian packages directory for this package"
sh "mkdir -p {env.JENKINS_HOME}/workspace/debian_packages/api-config/$(date +"%d-%m-%Y")"
}
}
}
}
This is the error I get (I've tried escaping the $ characters but it still fails)
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup
failed:
WorkflowScript: 19: illegal string body character after dollar sign;
solution: either escape a literal dollar sign "\$5" or bracket the
value expression "${5}" # line 19, column 17.
sh "mkdir -p
{env.JENKINS_HOME}/workspace/debian_packages/api-
config/$(date +'%d-%m-%Y')"
What could be the issue? Isn't the jenkins "sh" meant to take commands as they would have been issued directly on the console?
This is workaround that I could come up:
stage('Prepare') {
steps {
script{
sh '(date +"%d-%m-%Y") > outFile'
def curDate = readFile 'outFile'
echo "The current date is ${curDate}"
sh "mkdir -p {env.JENKINS_HOME}/workspace/${curDate}"
}
}
}

Jenkins Pipeline Error

I am using the pipeline plugin in Jenkins, but unable to run shell commands. I am receiving the following error:
[develop - pipeline] Running shell script
nohup: failed to run command ‘sh’: No such file or directory
The node is an Ubuntu instance.
node ('aws-ondemand') {
//println env.BUILD_NUMBER
try {
stage 'Checkout and Build'
git url: 'git#github.com:MyAndroidRepo.git',
branch: 'develop'
sh 'git submodule init'
sh 'git submodule update'
sh './gradlew clean build'
}catch (e) {
//currentBuild.result = "FAILED"
//notifyFailed()
throw e
}
}
Nevermind. Script is fine. I was injecting env variables in the build step. I removed it and now its working.

Resources