Accessing OS environment variables within a Jenkinsfile - bash

Is this possible? I'm trying to access the linux env variable M2_REPO inside my Jenkinsfile using the Jenkins pipeline plugin, however it's constantly empty. I can run it on my bash shell just fine:
root#host-VirtualBox:~# echo $M2_REPO
/media/sf_apache-maven-repository
But running the command in a Jenkinsfile doesn't print anything:
sh 'echo $M2_REPO'
Does the Jenkinsfile have access to OS env variables?

When I set the variable using the Environment Inject plugin in jenkins it worked..

Related

Setting enviroment variable of the container through bash script is not working

I'm trying to set a enviroment variable of a docker container through bash script.
CMD ["/bin/bash", "-c","source runservice.sh"]
runservice.sh
#!/usr/bin/env bash
export "foo"="bar"
Now after pushing it when I go inside the container and do printenv, it is not setting up enviroment variable.
But if I run the same command inside the container, env variable is getting set up.
What's the correct way I can export using bash script?
it is not setting up enviroment variable
It's setting the environment for the duration of CMD build stage. Sure, it has no effect on anything else - the shell run at CMD stage is then exiting. See dockerfile documentation.
What's the correct way I can export using bash script?
There is no correct way. The correct way to affect the environment, is to use ENV.
There is a workaround in contexts that use entrypoint - you can set entrypoint to a shell (or a custom process) that will first source the variables.
ENTRYPOINT ["bash", "-c", "source runservice.sh && \"$#\"", "--"]

How to echo Jenkins Workspace in Cygwin

I am trying to build a job from jenkins pipeline, my pipeline is calling one shell script which is on windows server, and this script is in cygwin terminal, question is how to use the Jenkins workspace in my shell script in cygwin, tried below commands are not working, can someone please advise. Thanks.
In jenkins Pipeline:
sh "/home/test.sh $WORKSPACE"
In Cygwin:
#!/cygdrive/d/cygwin64/bin/bash --login
WORKSPACE=$1
echo "$WORKSPACE"
Out put from above command is
D:Jenkinsworkspacey_test123_feature_test
but actual workspace is(multibranch pipeline)
Running on win01 in D:\Jenkins\workspace\y_test123_feature_test
Not sure will cygwin take the different command to retrieve the workspace as you can see above output does not contains slashes in between words.
Workspace can be accessed using the enviornmental variable inside the pipeline.
In jenkins Pipeline:
// Get workspace
// This will give you the workspace of the agent that is available in the current stage
def Workspace=env.WORKSPACE
// Convert path with \\ for cygwin
Workspace.replace("\\", "\\\\");
println(Workspace)
sh "/home/test.sh ${Workspace}"

In Azure Devops, how can i use pipeline variables in a "Azure CLI task v.2" running shell script?

I am trying to execute a task using Azure CLI and bash in Azure Devops. I am using Azure CLI task v.2 and choosing shell as script type as below.
I want to use pipeline variables in bash script. I run the command below inside the script:
#!/bin/bash
az role assignment create --role "Lab Admin" --assignee $(ownerEmail) -g $(rgName)
and i got the error below:
line 2: ownerEmail: command not found
line 2: rgName: command not found
I don't understand. Normally, i should be able to use azure cli in a bash script.
Does anybody have an idea?
There are a couple of ways you could read the pipeline variables in your bash script:
(Suggested) Read them straight as environment variables in the script. Azure DevOps pipeline variables are added as environment variables that can be accessed by your bash script. So just by defining/setting the pipeline variables you can access them from the bash script. i.e in your script reference them as $OWNERNAME and $RGNAME.
See > https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables#environment-variables
Reference the direct argument in the bash script. Bash arguments are numbered and can be referenced as such. e.g for your aruments $1 is the string "-labOwner" $2 is the value contained within the ownerEmail pipeline variable.
See > https://tecadmin.net/tutorial/bash-scripting/bash-command-arguments/
Variables are passed in as environment variables. The variables are all uppercased and case-sensitive on linux.
Thus, $OWNERNAME and $RGNAME are likely the values you're after.
See the section on variable usage in scripts:
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#environment-variables
There are some tasks that don't populate the environment at all, these have an environment section where you can manually pass in the environment variables.

Execute environment file in ansible

I am trying to setup a playbook which will execute the env file of a service before running the command to stop the service.
Note that stop or start service command will only work after execute that env file.
The command to execute the env file is . ./.env_file_name or ksh .env_file_name
I am not able to execute the file using the above command in command module and shell module.
How to execute the above env file in ansible ?
How to run the stop command after executing the env file ?
If env file contains some environment variables that you need to set before running stop, then you can read the file, store results in a variable, then use environment keyword to provide them to the task, that actually stops the service.
Alternatively, you can add multiple commands in shell module at once, like so:
- name: Example for a shell module
shell:
cmd: |
./.env_file_name
./stop.sh (insert command to stop the service here)

How to Set docker container ip to environment variable dynamically on startup?

I want to export docker container hostname as an environment variable which I can later use in my app. In my docker file I call my script "run" as last command
CMD run
The run file is executable and works fine with rest of commands I perform but before them I want to export container hostname to an env. variable as follows
"run" File Try 1
#!/bin/bash
export DOCKER_MACHINE_IP=`hostname -i`
my_other_commands
exec tail -f /dev/null
But when I enter docker container and check, the variable is not set. If I use
echo $DOCKER_MACHINE_IP
in run file after exporting, it shows ip on console when I try
docker logs
I also tried sourcing another script from "run" file as follows
"run" File Try 2
#!/bin/bash
source ./bin/script
my_other_commands
exec tail -f /dev/null
and the script again contains the export command. But this also does not set the environment variable. What I am doing wrong?
When you execute a script, any environment variable set by that script will be lost when the script exits.
But for both the cases you've posted above the environment variable should be accessible for the commands in your scripts, but when you enter the docker container via docker run you will get a new shell, which does not contain your variable.
tl;dr Your exported environment variable will only be available to sub shells of the shell which set the variable. And if you need it when logging in you should source the ./bin/script file.

Resources