To begin i'm writing the pipeline entirely as groovy to be checked in to git. Please do not provide any gui necessary solutions. My Problem statement is:
Extract a variable from a file and set it equal to a groovy object.
What i've tried
def SERVICE_MAJOR_VERSION
node {
runGitClone(GIT_REPO_URL, GIT_HASH)
def conf = readFile("gradle.properties")
echo conf
//THE BELOW COMMENT DOESN'T WORK
//SERVICE_MAJOR_VERSION = loadEnvFromFile("SERVICE_VERSION_MAJOR", "gradle.properties", true, SERVICE_VERSION_MAJOR)
}
def runGitClone(git_repo_url, git_hash) {
checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: git_hash]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'WipeWorkspace']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '85572032-4284-4095-9eec-4df70ddfdb68', url: git_repo_url]]]
}
def loadEnvFromFile(string_name, file_path, should_print_load) {
def par1 = null
def content = readFile file_path
def matcher = content =~ /${string_name}\=(.+)/
if (matcher) {
par1 = string_name + "='" + matcher[0][1] + "'"
new GroovyShell(this.binding).evaluate(par1)
if (should_print_load) {
println par1
}
}
return par1
}
I've tried other suggestions to no avail. Particularly the below two.
Get values from properties file using Groovy
Parsing string as properties
If you have a working example of extracting a variable from a file and setting it equal to a groovy object it would solve my problem.
SOLVED:
def content = readFile 'gradle.properties'
Properties properties = new Properties()
InputStream is = new ByteArrayInputStream(content.getBytes());
properties.load(is)
def runtimeString = 'SERVICE_VERSION_MINOR'
echo properties."$runtimeString"
SERVICE_VERSION_MINOR = properties."$runtimeString"
echo SERVICE_VERSION_MINOR
Related
I premise that I am not very familiar with Jenkins and Groovy.
I am trying to set a timeout with a time value that can change based on a specific condition. I was wondering if it is possible to do this and how.
This is a shortened example for simplicity, of the pipeline I'm dealing with:
/**
* prepare tests for parallel
* #param filename Name of the file that contains the testsuites list
*/
def doDynamicParallelSteps(filename){
tests = [:]
echo "doDynamicParallelSteps"
def w = pwd()
def path = "$w/$filename"
// read all the lines into a list, each line is an element in the list
def fh = new File(path)
def lines = fh.readLines()
for (line in lines) {
def values = line.split(":")
def testsuite_name = values[0].trim()
def test_path = values[1].trim()
def test_filename_or_directory = test_path.split("/").getAt(-1);
def is_file = test_filename_or_directory.matches("(.*).php")
def is_custom_mycondition = test_filename_or_directory.matches("MyMatchCondition")
if (is_custom_mycondition){ // large timeout
def time_val = 10
} else { // default timeout
def time_val = 5
}
tests["${test_filename_or_directory}"] = {
stage("UnitTest ${test_filename_or_directory}") {
timeout(time: time_val, unit: 'MINUTES') { // scripted syntax
// other stuff here
} // end timeout
} // end stage
} // end MAP
}
parallel tests
}
If I run this pipeline I got the following output:
hudson.remoting.ProxyException: groovy.lang.MissingPropertyException: No such property: time_val for class: mycustomJob
Then I've tried to set it as global value and it worked but not as expected, because its value doesn't seems to changed, it outputs "5" ignoring my condition.
What am I doing wrong? Can anyone show me the right way or a better approach?
What you are doing looks more pythonic than groovy. You must define the variable 'time_val' on a higher level to make it visible in your scope, like:
def time_val = 5
if (is_custom_mycondition){ // large timeout
time_val = 10
}
Instead of your else part.
You can also define it in a single line, like:
def time_val = is_custom_mycondition ? 10 : 5
Your 'timeout' usage looks correct to me. Just define the variable properly.
I'm trying to create an issue, issuelink and copy attachment from triggered issue at the same time using scriptrunner.
For now, the code below are able to create issue and attachment, but I can not link the issue I created, does someone ever deal with this situation?
import org.apache.http.entity.ContentType;
def issueKey = issue.key
def result = get('/rest/api/2/issue/' + issueKey)
.header('Content-Type', 'application/json')
.asObject(Map)
def projectkey = "PILOTLV"
if (result.body.fields.customfield_10078.id == "10124"){
projectkey = "SPCLRQ"
}
def issuetypekey = "10018"
def ticketno = result.body.fields.customfield_10060
if (result.body.fields.issuetype.id == "10015"){
issuetypekey = "10017"
ticketno = result.body.fields.customfield_10059
}
def description = result.body.fields.description
def summary = result.body.fields.summary
def sysname = result.body.fields.customfield_10078.id
logger.info(description)
logger.info(summary)
logger.info(sysname)
logger.info(ticketno)
// create issue
def createReq = Unirest.post("/rest/api/2/issue")
.header("Content-Type", "application/json")
.body([
fields: [
summary : summary,
description: description,
customfield_10078: [
id: sysname
],
customfield_10060: ticketno,
project : [
key: projectkey
],
issuetype : [
id: issuetypekey
]
],
update: [
issuelinks: [
add: [
type:[
name: "Blocks",
inward: "is blocked by",
outward: "blocks"
],
outwardIssue: [
key: issuekey
]
]
]
]
])
.asObject(Map)
assert createReq.status >= 200 && createReq.status < 300
def clonedIssue = createReq.body
// copy attachments
if (issue.fields.attachment) {
issue.fields.attachment.collect { attachment ->
def url = attachment.content as String
url = url.substring(url.indexOf("/secure"))
def fileBody = Unirest.get("${url}").asBinary().body
def resp = Unirest.post("/rest/api/2/issue/${clonedIssue.id}/attachments")
.header("X-Atlassian-Token", "no-check")
.field("file", fileBody, ContentType.create(attachment['mimeType'] as String), attachment['filename'] as String)
.asObject(List)
assert resp.status >=200 && resp.status < 300
}
}
And there is a minor question, I found that the attachment name on new issue can not display Chinese character
https://community.atlassian.com/t5/Jira-questions/rest-api-3-issue-issue-key-attachments-upload-file-with-a/qaq-p/1070389\
Looks like I'm missing library
Simply put, you can't create and link an issue via the REST API at the same time. You have to create the issue first, then link the issue separately.
I have the following input in one of my Jenkins Pipeline Scripts:
def IMAGE_TAG = input message: 'Please select a Version', ok: 'Next',
parameters: [choice(name: 'IMAGE_TAG', choices: imageTags, description: 'Available Versions')]
imageTags is a List of map e.g. :
imageTags : [
[targetSuffix: "", sourceSuffix: "v2.17.1"],
]
When I run the script, I can select only [targetSuffix: "", sourceSuffix: "v2.17.1"] from the dropdown choice as expected.
In my script I can also see the value that gets selected:
echo "Selected Version = ${env.SELECTED_IMAGE_TAG}"
[Pipeline] echo Selected Version = {targetSuffix=, sourceSuffix=v2.17.1}
Now I wanted to find out which item from the original imageTags List got selected, but my script does not work as expected:
def selectedImageTag = imageTags.find { it.targetSuffix == "${env.SELECTED_IMAGE_TAG.targetSuffix}" }
I end up with the following exception:
groovy.lang.MissingPropertyException: No such property: targetSuffix for class: java.lang.String
My question is: How do I get the selected item of my choice out of the original List of maps?
The input step returns a string, so you can't write env.SELECTED_IMAGE_TAG.targetSuffix. You have to extract the substring, e. g. using a regular expression like this:
def match = ( env.SELECTED_IMAGE_TAG =~ /\{targetSuffix=(.*?), sourceSuffix=(.*?)\}/ )
if( match ) {
def selectedTargetSuffix = match[0][1]
def selectedImageTag = imageTags.find { it.targetSuffix == selectedTargetSuffix }
}
currently I wish to added a multliline text parmeter to a groovy pipeline. If the text parameter is not left column alighed (no space before paramter), then whitespace is injected into the text parameter list.
Any ideas on how to resolve this?
Here is the code
#!/usr/bin/env groovy
node {
def startTime = new Date()
println "Build start time : " + startTime
// Load system parameters
def projectProperties = [
[$class: 'EnvInjectJobProperty', info: [loadFilesFromMaster: false, secureGroovyScript: [classpath: [], sandbox: false, script: '']], keepBuildVariables: true, keepJenkinsSystemVariables: true, on: true]
]
// Set project parameters
projectProperties.add(parameters([
string(name: 'infraRepo', description: 'Repo Name', defaultValue: 'my-infrastructure' ),
string(name: 'infraBranch', description: 'Repo Branch', defaultValue: 'develop' ),
string(name: 'projectName', description: 'Project name', defaultValue: 'think-more' ),
// Text field not left side aligned now whitespace will be injected
text(name: 'ecrRepoAndVersion', description: 'ECR Docker name and version number',
defaultValue:'''address=3.0.1
address-details=3.0.1
auth=3.2.1'''),
choice(name: 'clusterName', description: 'Ecs cluster name', choices: '---Select---\nblue-ci\ngreen-ci', defaultValue: '---Select---'),
]))
properties(projectProperties)
// Print system variables
sh 'env | sort'
}
And here is an image of how the Jenkins Job UI looks after this pipeline is executed. Note the whitespace in the ecrRepoAndVersion field.
Thank you - that worked perfectly.
text(name: 'ecrRepoAndVersion', description: 'ECR Docker name and
version number',defaultValue:"""address=3.0.7-RC\n
address-details=3.0.3-RC\nauth=3.2.3-RC""")
Setting aside the need for this logic, I would add a bit more readability and ease of maintenance by joining a list of items, instead of verbatim specification:
def ecrRepoAndVersionItemsDefault = [
"address=3.0.7-RC",
"address-details=3.0.3-RC",
"auth=3.2.3-RC",
]
...
// then construct an ArrayList
def jobParams = []
jobParams << ...
...
jobParams << text(
name: 'ecrRepoAndVersion',
description: 'ECR Docker name and version number',
defaultValue: ecrRepoAndVersionItemsDefault.join('\n')
)
// then add the properties
...
projectProperties.add(parameters(jobParams))
...
properties(projectProperties)
...
// etc.
I've got a JSON file that I am slurping with groovy.
{
"team": "${GLOBAL_TEAM_NAME}",
"jobs": [
{
In the JSON above is a property 'team' that contains a groovy-like variable I want to be resolved at runtime.
teamList.each { tl ->
try
{
def teamSlurper = new JsonSlurperClassic()
def t = teamSlurper.parseText(tl.text)
println "*********************"
println "PROVISIONING JOB FOR: " + t.team
Output:
PROVISIONING JOB FOR: ${GLOBAL_TEAM_NAME}
The above outputs the raw value, but I would like it to resolve the global variable that has been defined for the system.
How can I resolve ${GLOBAL_TEAM_NAME} to its actual system value?
You can do this with Groovy Templates.
def slurper = new groovy.json.JsonSlurperClassic()
def engine = new groovy.text.SimpleTemplateEngine()
def binding = ["GLOBAL_TEAM_NAME": "Avengers"]
def json = '{"team":"${GLOBAL_TEAM_NAME}"}'
def t = slurper.parseText(engine.createTemplate(json).make(binding).toString())
t.team // "Avengers"