How to set environment variables used by docker container using Bamboo - docker

I am using Bamboo for building and deploying my docker container. My code uses environment variables. I am using a shell script to set values of those variables with those values being hardcoded in .sh file. Ideally, I would like the values for those environment variables to be passed through bamboo variables. One option is to generate a shell script during bamboo build plan and call that shell script from startup file. Is there any better option to set system environment variables using bamboo variables?

When adding the Docker task in the Plan configuration, you have the option to pass environment variables.
For example, if your Dockerfile has ENV variable test_db_pass you should pass in the Docker task field "Container environment variables" the following: test_db_pass=${bamboo.test_db_pass}

It is possible to define either plan or global variables in Bamboo.
You can then use them in you build.
It's in the documentation :
https://confluence.atlassian.com/bamboo/defining-plan-variables-289276859.html

Related

Accessing Jenkins-set environment variables from a Jenkins Plugin

If there is a Jenkins shell script build step, environment variables are set so that, for example, if you echo $WORKSPACE you see the current working directory. I am writing a custom plugin which also can execute shell commands but WORKSPACE is not set when this plugin executes.
I could see that Jenkins sets all those env variables prior to executing the shell commands the Jenkins project specifies so that they would not be already set for a custom plugin. If that is the case, it would seem like there is no way to access those env variables.
But if there is a way to obtain the values of the env variables that would be useful.

Pipeline shell step and environment variables

I want to run a shell command in a sh step, that depends on some environment variables. Now I know that there is the environment directive. If I declare all environment variables there, the command works just fine.
However, I have several pipeline scripts that all run this command with the same environment variables. So instead of declaring these variables in each script, I want to declare them only once. I tried to set environment variables for the jenkins user, in which I didn't succeed. Finally I declared them system wide (in /etc/environment) only to find, that they're still not present for the command run in the shell step.
I conclude from this that jenkins runs shell step commands in a clean environment, ignoring variables I may have declared. My question is now: how can I set environment variables for the shell step for all pipeline scripts in jenkins?

Store Ansible variables in Jenkins

We recently integrated Ansible deployment with Jenkins. All looks good and the next step is to find a way to store all playbook variables. What is the best practice for that? We want to have different set of variables for each environment (Dev, QA, UAT, Prod). Many thanks.
Use a shell/batch script and set them as environment variables. Like
#!/bin/bash
export var1=value1
export var2=value2
Then execute the script just before running playbook.
source <script>.sh
bash <script>.sh
You can also parameterize the values which can be passed through jenkins during runtime.i.e
export var1=$1 and so on.....

Jenkins - How to read the environment variables in groovy post build step

I am trying to read the environment variables in Groovy Postbuild step. I am able to read the values of parameters passed to builds but unable to read the values of parameters which are set in one of my Execute Windows batch command.
In one example of my Execute Windows batch command I do this:
SET custom_param=myValue
if I use ${custom_param} in other jenkins steps/jobs, it gets my value. So I am sure it has the value. I just can't get it in groovy script
I have tried followings to do so, none of them have worked:
manager.envVars['custom_param']
build.buildVariableResolver.resolve('custom_param')
build.getEnvironment(listener).get('custom_param')
Any help here would be great
(Assuming you're not running your script in groovy sandbox)
Try the bellow:
build = Thread.currentThread().executable
String jobName = build.project.getName()
job = Hudson.instance.getJob(jobName)
my_env_var = job.getLastBuild().getEnvironment()["YOUR_ENV_VAR"]
Groovy Post build step run as separate process. It has access to environment as normal JVM process.
You could use EnvInject plugin as a a build step. Subsequent steps in build will able to read this via normal environment access (System.env in your groovy script)
When you set some custom variables in your "Windows command batch" step, these variables are available only during this Jenkins step.
Once Jenkins move on the next step, your variables are lost...
If you want to set some variables permanently, you can try to use the SETX command:
What is the difference between setx and set in environment variables in Windows?

Environment variable retrieve from NAnt script is not recognize in Jenkins nant build step

In my nant script I retrieve my environment variable in this way:
property name="ProjectSolutionPath" value="${environment::get-variable('MAIN_PROJECT_PATH')}"
but when I run it through jenkins using nant as build step I got an error like this.
Expression: ${environment::get-variable('MAIN_PROJECT_PATH')}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Environment variable "MAIN_PROJECT_PATH" does not exist.
Is there configuration for this? so that Jenkins will recognize environment variables access by my nant script?
Help is greatly appreciated.
Make sure you define this environment variable in "System variables".
Since the Jenkins process usually runs as "NT AUTHORITY\SYSTEM" user, the environment variables associated with your user account do not get appended to the env-vars of the process.
I've seen this answer on how to add env-vars to a Jenkins build (though I don't like spawning a cmd line process), if you do not want to use sys-env-vars.

Resources