How to use environment variables in a Jenkins pipeline job? - jenkins-pipeline

I posted this in the Jenkins users Google group, but thought I'd post it here too.
I have a Jenkins Pipeline job, and in its Configuration page, I use a "Pipeline script from SCM" as my pipeline. One of this block's parameters is "Branch to build" of course. How can I used an environment variable for the text block? I tried, for example, $branchToBuild, ${branchToBuild} or "${branchToBuild}" and it just takes those as literal values and does not interpolate the string. I do have that variable defined and use it in other jobs.
Someone suggested using ${env.branchToBuild}, so I tried env.branchToBuild, $env.branchToBuild, ${env.branchToBuild}, and "${env.branchToBuild}" all to NO avail, that is, they are also just taken as literal strings and not interpolated.
Is it just not possible to do this?

You have to uncheck Lightweight checkout box in order to use a variable as Branch name to build.
It's a known Jenkins bug, here is more information : How to pass project parameter as branch name to build in Jenkins
Apparently the code path is very different if you are using the
lightweight checkout, and that has not been resolved, apparently.
Another source : https://cleverbuilder.com/notes/jenkins-dynamic-git-branch/

Related

How do you reference defined variables in a SQL Server Database Project?

I've read many questions on this such as:
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/da4bdb11-fe42-49db-bb8d-288dd1bb72a2/sqlcmd-vars-in-create-table-script?forum=ssdt
and
How to run different pre and post SSDT pubish scripts depending on the deploy profile
What I'm trying to achieve is a way of defining a set of scripts based on the environment being deployed to. The idea is that the environment is passed in as a SQLCMD variable as part of the azure-devops pipeline into a variable called $(ServerName), which I've setup in the sql server database project under properties with a default of 'DEV'.
This is then used in the post deployment script like this:
:r .\PostDeploymentScripts\$(ServerName)\index.sql
This should therefore pick up the correct index.sql file based on the $(ServerName) variable. When testing this by publishing and entering 'QA' for the $(ServerName) variable and generating the script it was still displaying the 'DEV' scripts. However, the top of the script showed the variable had been set correctly:
How do I get the post deployment script to reference the $(ServerName) variable correctly so I can dynamically set the correct reference path?
Contrary to this nice post: https://stackoverflow.com/a/54482350/11035005 , it appears that the :r directive is evaluated at compile time and inserted into the DACPAC before the xml profiles are even evaluated so this is not possible as explained.
The values used are the defaults or locals from the build config and can only be controlled from there.

Go CD failure message available in environment

There is a list of env variables available for GoCD at:
https://docs.gocd.org/current/faq/environment_variables.html
However I'm looking for something like: GO_BUILD_ERROR or similar.
I would like to have the failure reason or message when a build fails to pass this to an external script or message.
There seems to be nothing in the documentation.
GoCD doesn't have any such variables. The reason I feel is mostly because GoCD is very generic in terms of what commands constitute a build for a material. You might want to parse the logs manually to figure that out.
Also in the context of GoCD environment variables are used as input to the stages and not as output from them. If you're planning to build a plugin / wrapper for the commands that run consider storing them as properties in the jobs that way they can also be queried upon later if required.

Jenkins to pass SVN_DIR variable to promoting job

I have a maven build job which provides drop-down list to select the project under SVN trunk. I have used promoted builds plugin to promote the build to different environments. I want to pass SVN_DIR variable to my promotional job as parameter. I have used predefined parameters and defined as SVN_DIR=${SVN_DIR}, to check if I am able to pass when I tried to echo SVN_DIR value it is showing as SVN_DIR itself. This means it is not passing the value. Any idea how to achieve this?jenkins
You can use Parameterized Trigger Plugin which will let you to pass parameters from one job to another which can be found here https://wiki.jenkins.io/display/JENKINS/Parameterized+Trigger+Plugin
OR you can use the Promoted Builds Plugin. The original SVN_DIR will be available as PROMOTED_SVN_DIR.

How to isolate multiple Teamcity agents from getting picked up by a specific job

I would like my build job to not build on specific teamcity agents and there is no common pattern for the build agent names. Is there a way I can isolate multiple agents from getting picked up by a single job.
For example I can make sure that the build job does not pick up 1 agent using the following parameter.
teamcity.agent.name
does not match
agent-001
How can I similarly tell the teamcity job to not run on the following agents as well.
"123-agent"
"my_agent"
"test_agent"
"agent_do_not_use"
I cannot use the same parameter, teamcity.agent.name with does not match for multiple agents.
Can you all teamcity experts help me out here please on what is the best way to achieve this.
You can add agent requirement with "does not match" condition which accepts regular expression and set it to:
123-agent|my_agent|test_agent|agent_do_not_use
Using an agent requirement based on presence (or absence) of a specific property coming from agent's buildAgent.properties file would probably be a better solution to using agent names in the requirement.
Alternative means to manage agent's compatibility are: use agent pools and use agent's Compatible Configurations set to a limited set.
You can add a specific parameter inside the agent configuration on the local machine inside: C:\BuildAgent\conf\buildAgent.properties
Then, you can add something specific like: system.Is<MyFeature>Available=True
Then, in teamcity configuration, you will add an Agent Requirement with this parameter.

Clarity on teamcity parameters and its types

I'm trying to understand about variables in teamcity. My understanding is there are 3 kinds of variables(System,Env,Config)
But in jetbrains documentation I saw more variables. Looks like agent variables and server side variables are separate.
But in TeamCity, parameters section when I select "kind" config or system or env, all kinds of values are populated (I Expected only relevant values should come)
Not really clear about when we have to use which variable. Is TeamCity having 6 variables for parameters(Serverside:env,sys,config and Agent:env,sys,config).
There are three types of parameters, they differ in a way they might be used in a build:
env parameters are passed to the build process (spawned by TeamCity) as environment variable
sys parameters set tool-specific variables (and therefore passed to the build scripts of the supported runners)
config parameters are meant to be used for build configuration customization
There're predefined parameters exposing server build properties, agent properties, agent build properties etc. These parameters are passed to the build as system parameters, some of them are also copied to the environment variables.
In addition, parameters might be defined
for a certain build via "Run Custom Build" dialog
in Parameters section of build configuration/project or build configuration/project template
in buildAgent.properties file on the agent
More details might be found in the docs.

Resources