jenkins parameter plugin
return ["DEV", "TEST", "STAGE", "PROD"]
above, it work but
jenkins can't get the list when you code looks that
def ENVS=["DEV", "TEST", "STAGE", "PROD"]
return ENVS
is there any good way to deal with it ?
Related
I have a jenkinsfile which is parametrized. Based on the input parameters I want to set certain environment variables. But I m not able to get the syntax right.
parameters {
choice choices: ['insure-base-docker/insure-base', 'insure-ide/insure-sd', 'insure-ide/insure-ansible','insure-ide/ansible-test-vini'], description: 'Auf welche repository sollte die Tag erstellt?', name: 'repository'
choice choices: ['tag', 'branch'], description: 'Tag oder branch erstellen', name: 'git_entity'
string defaultValue: '21.x.x', description: 'Version die als branch oder Tag ersellt werden muss', name: 'version', trim: false
}
environment {
GIT_URL = "${'https://my_repo/scm/'+param.repository+'.git'}"
GIT_BRANCH = "${'Release/'+param.version}"
CHECKOUT_BRANCH = '${${git_entity} == "tag" ? "master" : "develop"}'
}
the env vars are always wrong. How do I set the env vars correctly?
Nowadays, there aren't many differences between parameters and environment variables in Jenkins. Even the way you use them, preceded by the env. keyword, is the same.
Try something like this.
pipeline {
parameters {
choice choices: ['insure-base-docker/insure-base', 'insure-ide/insure-sd', 'insure-ide/insure-ansible','insure-ide/ansible-test-vini'], description: 'Auf welche repository sollte die Tag erstellt?', name: 'GIT_PROJECT'
string defaultValue: '21.x.x', description: 'Version die als branch oder Tag ersellt werden muss', name: 'GIT_BRANCH', trim: false
}
agent any
stages {
stage('Cloning Git repository') {
steps {
script {
git branch: "${env.GIT_BRANCH}", credentialsId: 'MY_GIT_CREDENTIALS_PREVIOUSLY_ADDED_TO_JENKINS', url: "http://github.com/user/${env.GIT_PROJECT}.git"
}
}
}
}
}
You can use as GIT_BRANCH not just branches, but also tags.
Best regards.
I am assuming you are using a freestyle project
here are the steps
Go to Build Environment part and check the option Inject environment variables to the build process
it will open a new set of input boxes.
enter your code in Groovy script
Here am just trying to update the Version and Full version to include the passing parameter say TestParam
here is a sample:
import hudson.model.*
import groovy.io.FileType
def build = Thread.currentThread().executable
def buildNumber = build.number
def workspace = build.getEnvVars()["WORKSPACE"]
def defaultBuildNo = build.getEnvVars()["BUILD_NUMBER"]
println "Hi from Groovy script "
println workspace
println defaultBuildNo
def map = [
"BUILD_NUMBER": defaultBuildNo,
"VERSION" : defaultBuildNo + build.getEnvVars()["TestParam"],
"FULL_VERSION": +defaultBuildNo + "." + build.getEnvVars(["TestParam"]
]
return map
Now in the execute shell part type these and see all will resolve successfully.
Execute shell
echo $TestParam
echo $BUILD_NUMBER
echo $VERSION
echo $FULL_VERSION
Now all these env variables are accessible throughout the Job.
I am trying to implement Machine learning in my jenkins pipeline.
For that I need output data of pipeline for each build.
Some parameters that i need are:
Which user triggered the pipeline
Duration of pipeline
Build number with its details
Pipeline pass/fail
If fail, at which stage it failed.
Error in the failed stage. (Why it failed)
Time required to execute each stage
Specific output of each stage (For. eg. : If a stage contains sonarcube execution then output be kind of percentage of codesmells or code coverage)
I need to fetch these details for all builds. How can get it?
There is jenkins api that can be implemented in python but i was able to get only JOB_NAME, Description of job, IS job Enabled.
These details werent useful.
There are 2 ways to get some of data from your list.
1. Jenkins API
For first 4 points from the list, you can use JSON REST API for a specific build to get those data. Example API endpoint:
https://[JENKINS_HOST]/job/[JOB_NAME]/[BUILD_NUMBER]/api/json?pretty=true
1. Which user triggered the pipeline
This will be under actions array in response, identyfi object in array by "_class": "hudson.model.CauseAction" and in it you will have shortDescription key which will have that information:
"actions": [
{
"_class": "hudson.model.CauseAction",
"causes": [
{
"_class": "hudson.triggers.SCMTrigger$SCMTriggerCause",
"shortDescription": "Started by an SCM change"
}
]
},
2. Duration of pipeline
It can be found under key: "duration". Example
"duration": 244736,
3. Build number with its details
I don't know what details you need, but for build number look for "number" key:
"number": 107,
4. Pipeline pass/fail
"result": "SUCCESS",
If you need to extract this information for all builds, run GET request for job API https://[JENKINS_HOST]/job/[JOB_NAME]/api/json?pretty=trueand extract all builds, then run above-mentioned request per build you have extracted.
I will write later a dummy python script to do just that.
2. Dump data in Jenkinsfile
There is also a possibility to dump some that information from Jenkinfile in post action.
pipeline {
agent any
stages {
stage('stage 1') {
steps {
sh 'echo "Stage 1 time: ${YOUR_TIME_VAR}" > job_data.txt'
}
}
}
post {
always {
sh 'echo "Result: ${result}" > job_data.txt'
sh 'echo "Job name: ${displayName}" > job_data.txt'
sh 'echo "Build number: ${number}" > job_data.txt'
sh 'echo "Duration: ${duration}" > job_data.txt'
archiveArtifacts artifacts: 'job_data.txt', onlyIfSuccessful: false
}
}
}
List of available global variables for pipeline job can be found:
https://[JENKINS_HOST]/pipeline-syntax/globals#env
For rest, you will need to implement your own logic in Jenkinsfile.
Ad. 5
Create a variable which holds information about current stage. At the beginning of each stage change its value to the ongoing stage. At the end dump to file like rest variables. If pipeline will fail let's say on stage foo in post action this variable will have exact same value because if pipeline fails it won't go to next stage.
Ad. 6
I'm not sure what you want, a traceback, error code?
I guess you will probably need to implement your own logging function.
Ad. 7
Make a function for measuring time for each stage and dump value at the end.
Ad. 8
Also not sure what you mean. Like, build artifacts?
At the end of each build this file job_data.txt will be archived as build artifact which can be later downloaded.
If i will find more elegant and simple solution I'll edit this post.
Hope it helps in any way
EDIT 1
Here is the script I've mentioned earlier.
import requests
username = "USERNAME"
password = "PASSWORD"
jenkins_host = "JENKINS_HOST"
jenkins_job = "JOBNAME"
request_url = "{0:s}/job/{1:s}/api/json".format(
jenkins_host,
jenkins_job,
)
job_data = requests.get(request_url, auth=(username, password)).json()
builds = []
for build in job_data.get('builds'):
builds.append(build.get('number'))
for build in builds:
build_url = "{0:s}/job/{1:s}/{2:d}/api/json".format(
jenkins_host,
jenkins_job,
build,
)
build_data = requests.get(build_url, auth=(username, password)).json()
build_name = build_data.get('fullDisplayName')
build_number = build_data.get('number')
build_status = build_data.get('result')
build_duration = build_data.get('duration')
for action in build_data.get('actions'):
if action.get("_class") == "hudson.model.CauseAction":
build_trigger = action.get('causes')
print(build_name)
print(build_status)
print(build_duration)
print(build_number)
print(build_trigger)
Please note you might need to authorize with API Token depending on your security settings.
I am able to integrate jenkins with artifactory to store my artifacts whenever a build is carried out, however whenever new artifacts are being pushed, the older versions will be removed automatically.
So I will like to check is there anyway for me to label each artifacts according to the version number or build number for easy version control.
Be sure to include the Jenkins ${env.BUILD_NUMBER} in the artifact path and name.
Assuming your Artifactory repository is set up as a Maven repository your pipeline should include something like this:
def uploadSpec = """{
"files": [
{
"pattern": "${env.PACKAGING_FOLDER}/${env.REPOSITORY_APPLICATION}-${env.BUILD_NUMBER}.zip",
"target": "${env.REPOSITORY_CODE}/${env.REPOSITORY_GROUP}/${env.REPOSITORY_APPLICATION}/${env.BUILD_NUMBER}/${env.REPOSITORY_APPLICATION}-${env.BUILD_NUMBER}.zip",
"regexp": "true"
}
]
}"""
def buildInfo = Artifactory.newBuildInfo()
buildInfo.env.capture = true
buildInfo = server.upload(uploadSpec)
server.publishBuildInfo(buildInfo)
I use Jenkins Artifactory Plug-in for promotion. I try to create separate job to promote the artifact. Here the stage of promotion
stage('promote artifact') {
steps {
script {
String buildName = "${BUILD_NAME}"
String buildNumber = "${BUILD_NUMBER}"
def promotionConfig = [
//Mandatory parameters
'buildName' : buildName,
'buildNumber' : buildNumber,
'targetRepo' : "tst",
//Optional parameters
'comment' : "this is the promotion comment",
'sourceRepo' : "dev",
'status' : "Released",
'includeDependencies': true,
'failFast' : true,
'copy' : true
]
// Promote build
server.promote promotionConfig
}
}
}
As BUILD_NAME I try name of commit job and name of artifact at snapshot repository
As BUILD_NUMBER I use number of build like 1 without version number like 1.0.0.
But after all I got
Performing dry run promotion (no changes are made during dry run) ...
ERROR: Promotion failed during dry run (no change in Artifactory was done): HTTP/1.1 404 Not Found
{
"errors" : [ {
"status" : 404,
"message" : "Cannot find builds by the name 'artifact-name' and the number '1'."
} ]
}
Do you have some idea how to run it successfully? Or some flag to get more information about this error?
The reason why it fails for me that I skipped part with upload build info to Artifactory. You should create the upload spec during build task and upload it to Artifactory. BuildInfo will be stored separately and contains service infromation. After that promotion will work fine.
def uploadSpec = """{
"files": [
{
"pattern": "bazinga/*froggy*.zip",
"target": "bazinga-repo/froggy-files/"
}
]
}"""
server.upload(uploadSpec)
I created a pipeline job and would like to get the svn version number to enable further downstream processing in a call to a shell script. I am using a pipeline script similar to the following:
node {
// Mark the code checkout 'stage'....
stage 'Checkout'
// Get some code from a SVM repository
checkout(
[
$class: 'SubversionSCM',
additionalCredentials: [],
excludedCommitMessages: '',
excludedRegions: '',
excludedRevprop: '',
excludedUsers: '',
filterChangelog: false,
ignoreDirPropChanges: false,
includedRegions: '',
locations: [
[
...
]
],
workspaceUpdater: [$class: 'UpdateUpdater']
]
)
def svnversionnumber=${SVN_VERSION}
sh "/.../someshellscript ${svnversionnumber};"
}
Is there documentation on the checkout function available? Is it possible to get hold of the svn revision number? I can see that the revision is output to the log.
I had the same issue, but you can solve it by using the map that is returned from calling SCM checkout. It contains a value for SVN_REVISION.
// Get some code from a SVM repository
def scmVars = checkout(
...
)
def svnversionnumber = scmVars.SVN_REVISION
In Groovy pipeline script it's possible to get results of checkout scm command into TreeMap variable and then get what you need:
def checkoutResults = checkout([
poll: false,
scm: [
$class: 'SubversionSCM',
...
]
])
echo 'checkout results' + checkoutResults.toString()
echo 'checkout revision' + checkoutResults['SVN_REVISION']
echo 'checkout revision' + checkoutResults['SVN_REVISION_1']
echo 'checkout revision' + checkoutResults['SVN_REVISION_2']
I ended up invoking a shell to get the svn revision number as follows
def svnVersionNumber = sh(
script: "svn info --show-item last-changed-revision $url",
returnStdout: true
)
This was the only way I could get it to work correctly.
this code work for me in jenkins pipeline:
String url = 'svn+ssh:...'
SVN_REVISION_IN = sh returnStdout: true, script: 'svn info --show-item last-changed-revision ' + url
currentBuild.displayName = "Rev: ${SVN_REVISION_IN}"
There is a file called revision.txt in the build dir. The SubversionSCM provides methods to read this file.
//Here remote returns url#revision but the revision part is across the entire repo
//We will use the url part to get the revision for our branch
def remote = scm.locations.first().remote
def url = remote.split('#').first()
//The revision file has the revision for our branch. Parse returns a map.
def revmap = scm.parseRevisionFile(currentBuild.rawBuild)
revmap[url]
The scm variable is available on Jenkinsfiles. If you are not using a Jenkinsfile you should be able to create the scm object and pass it into the checkout method.
I think one of the best choice can be use a simple little "groovy console script" to get the revision number then put into a Jenkins variable..
Something like this to give you an idea:
Link
Take also a look at this question: Link