Jenkins plugin - environment variables - variables

I am using Jenkins with Testswarm and this plugin (forked sources).
I want to get a "job name" for Testswarm containing the Jenkins job name, build number and svn revision number.
Putting JOB_NAME in the configuration field does not help, the variable is not replaced by its value.
So I modified the plugin source code to get the Jenkins environment variables but all I get are "null"s.
Here is the culprit code. (in src/main/java/com/javaclimber/jenkins/testswarmplugin/TestSwarmBuilder.java from line 205)
I researched a lot concerning this functionnality and I did not find a working example for getting a variable.
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
...
EnvVars envVars = build.getEnvironment(listener);
...
envVars.get("JOB_NAME")
}
I am not at ease in Java and I am stuck at this point.
Any idea anyone, please ?
Update: java used version
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.5) (6b24-1.11.5-0ubuntu1~10.04.2)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)

Replacing
EnvVars envVars = build.getEnvironment(listener);
By
EnvVars envVars = new EnvVars();
envVars = build.getEnvironment(listener);
did the trick...

What version of Java are you using?
According to this, to get an environment variable, you need to add the following:
String job_name = System.getenv("JOB_NAME");
Have you tried this instead?
Also, I'm not sure what the configuration field looks like, but did you try using $JOB_NAME instead of JOB_NAME ?

Jenkins pipelines appear to execute on the server, so the original post may be missing a variable defined for the agent (either via the server's agent definition or via the agent's OS).
I don't know the Jenkins pipeline way of obtaining the agent's environment.

I stumbled over this and searched a while for a solution, so for the next user the answer: If you need the environment variables, overwrite
public void perform(Run<?, ?> run, FilePath workspace, EnvVars env, Launcher launcher, TaskListener listener)
instead of
public void perform(Run<?, ?> run, FilePath workspace, Launcher launcher, TaskListener listener)
and use the EnvVars from the parameters. (See also https://issues.jenkins.io/browse/JENKINS-29144)

Related

Spring Profiles in combination with ConfigServer

I have a very basic Spring Boot Config Server (just added the dependency and annotated mainclass with #EnableConfigServer).
In general I would like to support multiple environments with different propertysources for each of my applications, here is the example of the ConfigServer itself:
Profile: default (application.yml on classpath):
Profile: docker (application-docker.yml on classpath):
Profile: default (application.yml in repository of ConfigServer):
So in my case all of the properties from all of the three screenshots should be active, I'd expect the order/priority as follows:
application.yml from classpath
application-ANY_PROFILE.yml from classpath
application.yml from config repo
APP-NAME.yml from config repo (does not exists in this case)
So far this works flawlessly, except the issue that I'm having is that my application-docker.yml on classpath is beeing ignored when I start the application with the command (of course inside the container):
java -jar -Dspring-boot.run.profiles=docker *.jar
as you can see here:
My question is, even when I provide the profile as command line argument its not beeing picked up.
Why is that?
UPDATE, here is the Dockerfile and entrpoint.sh:
To activate one or more profiles do one of the following:
Activate using the VM parameters -Dspring.profiles.active=<profiles>
Activate using program arguments --spring.profiles.active=<profiles>
Following your example, the following should work:
java -jar -Dspring.profiles.active=docker *.jar

How configure Azure function project in Intellij/Pycharm run/debug configurations on mac

How to configure Azure function project in Intellij/Pycharm run/debug configurations on Mac because I've tried to set it by my own but it doesnt work.
I would like to replace shell command: func start with run config
The image below from Pycharm
UPDATE
I've added path to Azure-CLI and imported my app-settings
I'm trying to configure the run configs but it asks to select the module but there is no any module in dropdown (see pic)
UPDATE-UPDATE:
Here is what azure-tools-for-intellij team me answered:
They dont support pure python functions run yet
I think you shouldn't use row shell scrip with IntelliJ/PyCharm instead of this you should use Azure Toolkit for IntelliJ and run/debug your functions like in this guide.
Also when you install Azure Toolkit for IntelliJ you will have the opportunity to create run/debug configuration with predefined Azure Function ready template.
Just example:
I found the way to debug in Intellij Idea/PyCharm.
add these lines at the top of your file/module:
import pydevd_pycharm
pydevd_pycharm.settrace('127.0.0.1', port=9091, stdoutToServer=True, stderrToServer=True)
set Python Debug Server with host and port in Run/Debug Configs
run your azure functions as used to (func host start) and press debugging button.
The way I do it in PyCharm is to define a shell script configuration (Edit Configurations > Shell Script) and set:
Execute: Script set
Script text: func start
Working directory: should be to project directory where host.json etc. is located
Environment variables: leave empty
Execute in the terminal: checked
Run this configuration, which will start the test server in terminal. Then go to Run > Attach to Process... and select the process (usually it's the one without any path after the number).

Get null use System.getenv() in gradle

def CATALINA_HOME = System.getenv("CATALINA_HOME")
task t << {
println CATALINA_HOME
}
CATALINA_HOME is null when I run with idea (double click t from Gradle Project panel), but when gradle t is executed in terminal I get a correct path.
It seem this is only happen in OSX (my version is 10.11.14 El Capitan).
IntelliJ IDEA has problems with recognizing environment variables on OS X (not sure if on other systems as well). Please have a look at this post - it should solve the problem (some time ago I had similar issue and found it very useful).
Since the linked page no longer exists you can find it in the archive here. Thanks #AlexeyStepanov!
Want to add, that sometimes it insufficient for Intellij Idea to configure Run/Debug Configuration option of Environment variable.
Instead you need to set them in File -> Settings -> Build, Execution, Deployment -> Build Tools -> Runner -> Environment variables

Play 2 & Cloudbees: Could not resolve substitution to a value

I am deploying my Play! 2.1 application on Cloudbees.
I have in my application.conf:
# Database configuration
# ~~~~~
db.default.driver=com.mysql.jdbc.Driver
db.default.url=${MYSQL_URL_DB}
db.default.user=${MYSQL_USERNAME_DB}
db.default.password=${MYSQL_PASSWORD_DB}
I defined those values in Cloudbees configuration:
$ bees config:list -a myself/my-app
Application Parameters:
proxyBuffering=false
MYSQL_URL_DB=jdbc:cloudbees://my-app
MYSQL_USERNAME_DB=my-app
MYSQL_PASSWORD_DB=yummy
Runtime Parameters:
java_version=1.7
I publish my app using git (git push cloudbees cloudbees:master) which triggers Jenkins. But when it comes to deploying application, I get in Jenkins logs:
[error] (compile:compile)
com.typesafe.config.ConfigException$UnresolvedSubstitution:
conf/application.conf: 16: Could not resolve substitution to a value:
${MYSQL_PASSWORD_DB}
Is there anything else to do to make Jenkins aware of the configuration? Did I misunderstand something?
Thanks for your help!
Alban
You can add "?" to the beginning, so it will be treated as an override.
db.default.url=${?MYSQL_URL_DB}
You can also handle fallback situations with this approach, if you like.
db.default.url=mysql://fallback_url
db.default.url=${?MYSQL_URL_DB}
If MYSQL_URL_DB does not exist, fallback_url will be used.
This configuration is injected at runtime, not build time. You have to find a way to make the sbt build ignore unresolved substitution.
It seems a possible workaround is to set MYSQL_URL_DB=foo, etc as build environment variables, so that the check don't break, as they won't be actually injected in your configuration
I use a config like this:
https://github.com/CloudBees-community/play2-clickstart/blob/master/conf/application.conf
and a build command like this:
java -Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=384M -jar /opt/sbt/sbt-launch-0.11.3-2.jar -Dsbt.log.noformat=true clean compile test dist
And it does not worry about the missing environment variables.
My guess is that there is a scala macro or something that triggers the compiler to resolve those variables. Adding them in is fine.
I have amended the clickstart to set default values in case they are needed.

Running ScalaTest in IntelliJ removes everything but idea_rt.jar from classpath

I am trying to run a ScalaTest test case from IntelliJ
I specify the class path to use in the run/debug configuration dialogue and this appears to work ok.
However, if I try to interrogate this classpath using System.getProperty("java.class.path")
I only get the idea_rt.jar
This wouldn't be a problem except I am trying to do this inside an Integration test:
val execArgs = Array("java",
"-classpath",
System.getProperty("java.class.path"),
mainClass,
args)
val process = Runtime.getRuntime.exec(execArgs)
And, of course, my mainClass is not found.
My config.
scalatest_2.9.2-1.8.RC1
IntelliJ IDEA 11.1.3
scala-library-2.9.2
scala plugin version 0.5.977
Try to edit bin/idea.properties or Info.plist if you are on Mac, set this to true:
#---------------------------------------------------------------------
# Configure if a special launcher should be used when running processes from within IDE.
# Using Launcher enables "soft exit" and "thread dump" features
#---------------------------------------------------------------------
idea.no.launcher=true