Jenkins: setting env variable from shell script - shell

I have a bash script that I execute from a Jenkins job, using "execute shell".
The script starts an EC2 instance and sets an host variable containing the host name of the new instance.
I would like to set the host name of the new instance (script variable: host ) to a Jenkins environment variable so that I can pass it down to a downstream job (possibly using the Build Flow plugin).
Any idea how to do so?
Thanks

I ended up using a file to propagate data between builds.
The first build creates a file containing the information I need to propagate (host name of the newly created EC2 instance).
The file looks like:
host.name=ec2.123.3345.amazon.com
I use the EnvInject plugin to read the file and "inject" the properties that are then available in the next build (I'm using the Build Flow Plugin to orchestrate builds).

There is a plugin that you can install for inject your variable: EnvInject Plugin
If I understood your problem, I think this simple plugin is what you need.

Related

How to download .tfvar files in to ADOagent machine directly before running Terraform plan?

We are using Terraform Enterprise Cloud and Azure DevOps YML pipelines for Azure infra deployments.
Requirement: We want to separate .tfvar files completely from the main terraform folder and keep them in different Repo called config Repository.
Solution 1: We can refer tfvars from the config repository while running the below command,
terraform plan --var-fil -We cannot implement
Note: Since we are using global templates, these terraform commands like fmt, validate, plan, and apply are managed by the template itself, we are not allowed to edit the template.
Here is the logic,
template expects only .tfvars file in the current directory, then there are some bash commands to rename it to .auto.tfvars.
We know that these auto.tfvars files will be automatically identified by Terraform.
Solution 2: We are expecting and struggling to implement and need some help
By default Template copies all terraform folders to ADO Agent Container. we want to make sure the .tfvar file from the Config repository is available in the agent container. Then this solution will be good.
May be,
We can achieve it by Copying the .tfvars file from the config repository to the agent container by writing some shell script. but it has to be inside the terraform folder. because only terraform folder will be copied to the agent container.
Or is there any way that we can integrate a shell script to terraform configuration which can download tfvars file from config repository to container in run time.
Any other solution or approach will be appreciated.
To make sure the config repo files are available during runtime you can add a second artifact to the release pipeline. This will allow you to modify your var argument with the appropriate file.
https://learn.microsoft.com/en-us/azure/devops/pipelines/release/artifacts?view=azure-devops
One approach is to have your tfvars file stored as a secure file, then just add a step in your pipeline to download it, however, if you're using Terraform Enterprise, is there any particular reason to not use Terraform workspace variables?

Pass a file as a property while calling a maven goal in Jenkins v2.263.1

I am migrating all my jobs from Jenkins v1.651.3 to Jenkins v2.263.1.
Currently I am passing a file stored in my Linux server as a property under Invoke top-level maven targets build step in Jenkins.
e.g. property.a=/home/user1/props/a.properties and property.b=/home/user1/props/b.properties
In Jenkins v2.263.1, I am running all my jobs in a docker container which is created dynamically.
Where and how can I store this file so that I can pass this as a property to maven build like -Dproperty.a=/home/user1/props/a.properties and -Dproperty.b=/home/user1/props/b.properties
I tried adding these files in Managed Files section under Manage Jenkins option and passing these as -Dproperty.a=a.properties to maven build through Jenkinsfile, however, it did not work. Not sure if it's the right way.
Please let me now if there's a way to handle this.
Thanks in advance!

Spring boot environment based configuration with docker in Pipeline

I have one micro-service, that is running using a pipeline on AWS EKS cluster. So it passes through each environment. I need to update this application, so that whenever, it will pass through from different environment, it takes specific environment variables. What I thought to do this to add application.properties file for different environment builds and add the SPRING_PROFILES_ACTIVE=prod|dev|test, whatever required. But I am new to this pipeline stuffs and need to understand, where I will add this profile specific properties so that each time, the build runs in different environment, it take the specific environment based profile to activate it.
If you are using jenkins pipeline or any other pipeline for the build, have environment variable for for each environment and during docker build it will automatically build based one environment you select
something like this
docker build --build-arg ENVIRONMENT=$environment-variable(specific env can be from pipeline parameter) .
SPRING_PROFILES_ACTIVE=$ENVIRONMENT
Or else
You can have one file for each env and have that specific file picked during docker build

Unable to access directory inside workspace in jenkins

So I have a Jenkins job wherein as part of the build.
I run one script which creates a folder in the workspace say "test"
so the script runs fine and creates the folder test.
After this, I have a maven command wherein I want to access this test folder(as an argument) I am trying it as $workspace/test
but it's not working.
Can anyone guide if i am missing anything here
Can you give us more information - maybe code snippets?
I'm not sure what you mean with 'maven command' and '$workspace/test'
If you want use a variable in a pom.xml like:
<path>${workspace}</path>
you will have to assign it in the properties:
<workspace>...location...</workspace>
Otherwise you can use maven preassigned variables like
${basedir}/

How to declare steps jenkinsfile to inject environment variables by reading a properties file

I have a properties file in my workspace which is not a groovy file , and data is in the format
var1=var1
var2=var2
How do I define the steps('Declarative') in a stage to inject these values as environment variable for the whole pipeline?
Have you checked the accepted answer to How to read properties file from Jenkins 2.0 pipeline script ? Sounds exactly like your problem.

Resources