Is there a way to have a multiline environment variable in a nomad template? Trying it directly gives an error about not being able to find the closing quote.
In the docs the only function that's mentioned is | toJSON, but that translates the line feeds into \n so the receiving end needs to do a search-and-replace or some "unJSON" function.
I tried using HEREDOC syntax in the template, but that didn't seem to work either.
Using a here document works fine:
job "example" {
datacenters = ["dc1"]
type = "service"
group "cache" {
task "redis" {
driver = "docker"
config {
image = "redis:7"
}
env {
EXAMPLE = <<EOF
multiline
varibale
is
here
EOF
}
}
}
}
Related
I have a Jenkinsfile which I want to load variables into from a file during execution of the build, I also want to concatenate the variable into one line and print it out.
pipeline {
agent any
stages {
stage("foo") {
steps {
script {
env.name = readFile 'name.txt'
env.tag = readFile 'tag.txt'
}
echo "${env.name}:${env.tag}"
}
}
}
}
name.txt contains Uzodimma
path.txt contains latest
When I run the pipeline, I get
Uzodimma
:latest
I expected
Uzodimma:latest
Is there a way I can do this in Jenkinsfile?
The issue here is that your files have newline characters in them, so they are assigned to your variables as part of the String. You can remove the newlines with the trim method since readFile returns a String:
env.name = readFile('name.txt').trim()
env.tag = readFile('tag.txt').trim()
and the returned standard out will be as you expect.
I am trying to run a pipeline that has several servers. I want to do some actions in several servers at a time when selecting a choice parameter. My idea is to select a choice parameter 'APPLICATION' and execute some actions on 4 different servers sequentially (one server at a time). I am trying to put the environment variables assigning the value os the servers in an array and then ask for the environment variable to execute the actions.
pipeline {
agent {
node {
label 'master'
}
}
environment {
APPLICATION = ['veappprdl001','veappprdl002','veappprdl003','veappprdl004']
ROUTER = ['verouprdl001','verouprdl002']
}
parameters {
choice(name: 'SERVER_NAME', choices: ['APPLICATION','ROUTER'], description: 'Select Server to Test' )
}
stages {
stage ('Application Sync') {
steps {
script {
if (env.SERVER_NAME == 'APPLICATION') {
sh """
curl --location --request GET 'http://${SERVER_NAME}//configuration-api/localMemory/update/ACTION'
"""
}
}
}
}
} }
I want to execute the action on all the servers of the 'APPLICATION' variable if is selected the 'APPLICATION' parameter in 'Build with parameters'.
Any Help would be appreciate it.
Thanks
You can't store a value of an array type in the environment variable. Whatever you are trying to assign to the env variable gets automatically cast to the string type. (I explained it in more detail in the following blog post or this video.) So when you try to assign an array, what you assign is its toString() representation.
However, you can solve this problem differently. Instead of trying to assign an array, you can store a string of values with a common delimiter (like , for instance.) Then in the part that expects to work with a list of elements, you simply call tokenize(",") method to produce a list of string elements. Having that you can iterate and do things in sequence.
Consider the following example that illustrates this alternative solution.
pipeline {
agent any
environment {
APPLICATION = "veappprdl001,veappprdl002,veappprdl003,veappprdl004"
}
stages {
stage("Application Sync") {
steps {
script {
env.APPLICATION.tokenize(",").each { server ->
echo "Server is $server"
}
}
}
}
}
}
When you run such a pipeline you will get something like this:
I am trying to save the value fetched from a properties file in my jenkins pipeline but it is not working
script {
String content = readFile("gradle.properties")
Properties properties = new Properties()
properties.load(new StringReader(content))
backupVersion = ${properties.backupUrl} // this is not working
echo backupVersion
echo "property 'version' has value '${properties.backupUrl}'"// this is working
}
I have defined backupVersion globally
You don't use dollar syntax if you directly refer to variables. Dollar syntax is only for string interpolation.
Simply write:
backupVersion = properties.backupUrl
I am using branch name to pass it into build script. $(env.BRANCH_NAME).
I would like to manipulate the value before using it. For example in case we build from trunk I want suffix for the build output be empty but in case of branch I want it to be -branch name.
currently I am doing it by defining environment section.
environment {
OUTPUT_NAME_SUFFIX = ($(env.BRANCH_NAME) == 'trunk') ? '': $(env.BRANCH_NAME)
}
I am getting this error:
WorkflowScript: 4: Environment variable values must either be single quoted, double quoted, or function calls. # line 4, column 62.
(env.BRANCH_NAME) == 'trunk') ? '': $(en
^
What the best way to define variables and eval their values in scope of pipeline.
TIA
You can use string interpolation to evaluate the expression:
environment {
OUTPUT_NAME_SUFFIX = "${env.BRANCH_NAME == 'trunk' ? '': env.BRANCH_NAME}"
}
This will fix the error you're getting, however pipeline does not allow you to have environment variables that are of 0 length, aka empty string (JENKINS-43632).
That means that setting OUTPUT_NAME_SUFFIX to '' is like unseting it. You might want to precalculate the whole name of your output, so that your env variable is never an empty string.
I have solved it by adding following code. So far had no issues with empty strings.
stage('Set Environmnet'){
steps {
script {
if(BRANCH_NAME == 'trunk'){
env.OUTPUT_NAME_SUFFIX = ''
}else if (BRANCH_NAME.startsWith("branches")){
env.OUTPUT_NAME_SUFFIX = "-" + BRANCH_NAME.substring(BRANCH_NAME.lastIndexOf("/")+1)
}else{
env.OUTPUT_NAME_SUFFIX = ''
}
}
}
}
I have a text file :
export URL = "useful url"
export NAME = "some name"
What I do is executing this file with command source var_file.txt
But when I do echo $URL or env.URL it returns nothing.
Please I don't have the ability to change the file var_file.txt : it means it will still be export var= value var
I know that it is possible to use load file.groovy step in pipeline to load variables but the file must be a list of : env.URL = 'url', I can't use this because I can't change file.
And we may also work with withEnv([URL = 'url']) but I must first get the values from an other script. This will really be a complicated solution.
So is there a way to use the file with list of export var = var_value in Jenkins Pipeline ?
What I have done is :
def varsFile = "var_file.txt"
def content = readFile varsFile
Get content line by line and split change the each line of content to env.variable = value:
def lines = content.split("\n")
for(l in lines){
String variable = "${l.split(" ")[1].split("=")[0]}"
String value = l.split(" ")[1].split("=")[1]
sh ("echo env.$variable = \\\"$value\\\" >> var_to_exp.groovy")
}
And then load file groovy with step load in the pipeline:
load var_to_exp.groovy
Alternative suggestion: embed scripted pipeline (not sure if there is a genuine "declarative" way of doing this -- at least I haven't found it so far):
stage('MyStage') {
steps {
script {
<extract your variables using some Groovy>
env.myvar = 'myvalue'
}
echo env.myvar
}
}
I'm not entirely sure how much modification you are allowed to do on your input (e.g. get rid of the export etc.), or whether that has to remain an executable shell script.