I am using Jenkins configuration as code (CASC) plugin to create Jenkins job during server startup inside docker. I wrote a shell script as a step inside a job configuration.
- script: >
freeStyleJob('jenkins-job-sample') {
description('Sample')
triggers {
githubPush()
}
scm {
git {
remote {
url('${GIT_URL}')
credentials('github-credentials')
}
branch '*/dev'
extensions {localBranch('dev')}
}
}
steps {
shell("""
do
cat \$OUTPUT | while read line || [[ -n \$line ]];
do
CAPP_ENTRY=\$line
GROUP_ID_PATH=(\${CAPP_ENTRY[0]})
ARTIFACT_ID=(\${CAPP_ENTRY[1]})
VERSION=(\${CAPP_ENTRY[2]})
done
done
""")
}
publishers {
}
}
After the job is created I want the final script step to be shown as below.
CAPP_ENTRY=\$line
GROUP_ID_PATH=(${CAPP_ENTRY[0]})
ARTIFACT_ID=(${CAPP_ENTRY[1]})
VERSION=(${CAPP_ENTRY[2]})
But during server startup it throws the following error.
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: script: 31: unexpected char: '\' # line 31, column 34.
GROUP_ID_PATH=(\)
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310) at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:150) at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:120) at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:132) at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:350) at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:139) at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:110) at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:234) at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:168) at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:943) at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605) at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581) at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558) at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298) at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268) at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688) at groovy.lang.GroovyShell.parse(GroovyShell.java:700) at groovy.lang.GroovyShell$parse.call(Unknown Source) at javaposse.jobdsl.dsl.AbstractDslScriptLoader.parseScript(AbstractDslScriptLoader.groovy:134) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210) at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:174) at
javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScriptEngine(AbstractDslScriptLoader.groovy:101)
It seems that ${CAPP_ENTRY[0]} is getting evaluated before the job creation happens which results to be (\). I want to escape ${} from getting evaluated before job creation. I tried in below ways.
GROUP_ID_PATH=(\${CAPP_ENTRY[0]})
GROUP_ID_PATH=(\$\{CAPP_ENTRY[0]})
GROUP_ID_PATH=(\"${CAPP_ENTRY[0]}\")
None of the above worked. Highly appreciate any suggestions on how I can escape it from getting evaluated.
Found an answer. Adding a ^ solved my issue. :) (^${CAPP_ENTRY[0]})
Source: https://github.com/jenkinsci/configuration-as-code-plugin/issues/577
I have had the same issue, and so far havent found "the" solution however for a workaround I just break the string
e.g. instead of
def HOSTS_LOCATION = "../bootstrap/inventory/${ENVIRONMENT_MAP[params.region]}_aws_ec2.yaml"
I do
def HOSTS_LOCATION = "../bootstrap/inventory/" + ENVIRONMENT_MAP[params.region] + "_aws_ec2.yaml"
Related
I use Jmeter Backend Listener url:
influxdbUrl = http://XX.XXX.XX.XXX:8086/write?db=JMeter&u=jusr&p=C-UBBC-"<
I get an error:
java.lang.IllegalStateException: Failed calling setupTest Caused by: java.net.URISyntaxException: Illegal character in query at index 76: http://XX.XXX.XX.XXX:8086/write?db=JMeter&u=jusr&p=C-UBBC-"<
Problem in special characters in password: C-UBBC-"<
How to fix it?
Not every character is allowed in an URL, you can use alphanumeric and few special ones, in particular ; , / ? : # & = + $ - _ . ! ~ * ' ( ) #. Everything else needs to be percent-encoded
Wrap your password into __urlencode() function and it should resolve your issue.
More information on JMeter Functions concept: Apache JMeter Functions - An Introduction
I am trying to compare in bash using case (in a Jenkinsfile) a given value and act upon it. However, the case fails due to:
syntax error near unexpected token `Manager'
The error happens here:
...
...
def microServicesList = microServicesToUpdate.tokenize(",")
...
...
for (String microserviceName : microServicesList) {
sh """
...
...
case ${microserviceName} in
"Instances Manager")
// do something
;;
esac
"""
}
Had to surround it with double quotes:
case "${microserviceName}" in
I am using net.schmizz.sshj.xfer.scp.SCPFileTransfer class to upload file from local to remote server. It is failing with following error:
net.schmizz.sshj.xfer.scp.SCPException: EOF while expecting response
to protocol message. Additional info: bash: -c: line 0: unexpected EOF while looking for matching
bash: -c: line 1: syntax error: unexpected end of file
This issue I am facing only when remote machine is Windows. For Linux machine it is successfully uploading.
I have tried following steps in my code.
1. Download a file from remote machine to local
2. Upload same file again back to remote.
It is failing in step 2.
#Override
public boolean upload(String localLocation, String remoteLocation) throws SSHClientException {
this.ensureConnected();
SCPFileTransfer scp = this.sshj.newSCPFileTransfer();
try {
scp.upload(localLocation, remoteLocation);
} catch (IOException e) {
log.error("Failed to copy file {} from local path at {} to remote location {} at {}" + remoteLocation,
hostname, localLocation, e);
return false;
}
return true;
}
Any leads will be really helpful.
Thanks.
I got the solution.
The remote file path that I have used looks like :
'/cygdrive/c/Program Files/XXX/'
The issue is "'" in the path. Removal of "'" from the path results successful upload of the file.
Thanks to all who gave me leads.
Thanks,
Shruti
I'm trying to configure the following custom task:
task antecedeRelease(type: AntecedeReleaseTask) {
antecedeWithVersion = project.'antecede-with-version'
antecedeToVersion = project.'antecede-to-version'
}
The problem is that the properties antecede-with-version and antecede-to-version are to be set through the command line with a -P option. If they're not set and antecedeRelease isn't being called, that shouldn't be a cause for an error:
$ ./gradlew tasks
org.gradle.api.GradleScriptException: A problem occurred evaluating project ...
Caused by: groovy.lang.MissingPropertyException: Could not find property 'antecede-with-version' on project ...
I could conditionally define the antecedeRelease task such that it's defined only if those properties are defined but I'd like to keep the build.gradle file as clean as possible.
If you need the antecedeRelease task to run "lazily" as-in, at the end of the configuration phase, or at the beginning of the execution phase, your best bet is to use doFirst
task antecedeRelease(type: AntecedeReleaseTask) {
doFirst {
antecedeWithVersion = project.'antecede-with-version'
antecedeToVersion = project.'antecede-to-version'
}
}
One option might be to use Groovy's elvis operator like so:
task antecedeRelease(type: AntecedeReleaseTask) {
antecedeWithVersion = project.ext.get('antecede-with-version') ?: 'unused'
antecedeToVersion = project.ext.get('antecede-with-version') ?: 'unused'
}
If this fails still, you can consider project.ext.has('property') when setting the value.
I need to change the FreeMarker behavior about processing template&data in case some data are missing.
In missing case I need to let the template unchanged and continue in processing.
Example:
Template:
...
var1 = ${var1}
var2 = ${var2}
...
Data:
var1 = Hello
I need Result after processing:
...
var1 = Hello
var2 = ${var2}
...
My solution is:
class MyTemplateExceptionHandler implements TemplateExceptionHandler {
public void handleTemplateException(TemplateException te, Environment env, java.io.Writer out)
throws TemplateException {
try {
//2nd word on 2nd row is name of missing variable :(
String missingVariable = te.getMessageWithoutStackTop().split("\n")[1].split(" ")[1];
out.write("${" + missingVariable + "}");
} catch (IOException e) {
throw new TemplateException("Failed to print error message. Cause: " + e, env);
}
} }
and using:
...
Configuration cfg = new Configuration();
cfg.setTemplateExceptionHandler(new MyTemplateExceptionHandler());
...
My solution works, but I am not satisfied because of:
1) getting missing variable in handleTemplateException() is horrible. Does exist better way how to get a name of missing variable?
2) Even if I have my own TemplateExceptionHandler, FreeMarker writes enough information to output. Can I change it?
Example output for missing variable "environment_name":
22.10.2014 9:01:55 freemarker.log._JDK14LoggerFactory$JDK14Logger error
SEVERE: Error executing FreeMarker template
FreeMarker template error:
The following has evaluated to null or missing:
==> environment_name [in template "Osb-PrepareAndDeploy.ftl" at line 33, column 33]
Tip: If the failing expression is known to be legally null/missing, either specify a default value with myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthessis: (myOptionVar.foo)!myDefault, (myOptionVar.foo)??
The failing instruction (FTL stack trace):
----------
==> ${environment_name} [in template "Osb-PrepareAndDeploy.ftl" at line 33, column 31]
----------
Java stack trace (for programmers):
----------
freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...]
at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:98)
at freemarker.core.EvalUtil.coerceModelToString(EvalUtil.java:382)
at freemarker.core.Expression.evalAndCoerceToString(Expression.java:115)
at freemarker.core.DollarVariable.accept(DollarVariable.java:76)
at freemarker.core.Environment.visit(Environment.java:265)
at freemarker.core.MixedContent.accept(MixedContent.java:93)
at freemarker.core.Environment.visit(Environment.java:265)
at freemarker.core.Environment.process(Environment.java:243)
at freemarker.template.Template.process(Template.java:277)
at net.homecredit.scm.jenkinsTool.countrySettings.Country.createJobs(Country.java:282)
at net.homecredit.scm.jenkinsTool.Start.main(Start.java:83)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
You can't prevent the exception from being logged. TemplateExceptionHandler doesn't mean to make non-error from an error, it just lets you decide what to do after the error has happened. Like, you want to print an error page or something. In extreme case, you may want to continue page rendering after printing some error indicator, but that's still an error that need to be fixed.
Maybe it would help if you tell why do you need this. Maybe the solution isn't TemplateExceptionHandler-s.
As of more intelligent exception processing, first, in the handler you should check if the exception is a InvalidReferenceException, becaue I guess you don't want to deal with others. Then, with getBlamedExpressionString() (requires FreeMarker 2.3.21) you could get part of what you want to print to the output. It's only part of that, because if ${x + 1} fails because x is undefined, it will only return "x", not "x + 1".