Use environment variable directly in CircleCI steps - circleci

Given the below step in a 2.0 CircleCI workflow:
- persist_to_workspace:
root: .
paths:
- foo/bar
Is it possible to use an environment variable instead of foo/bar?

This is called environment variable interpolation, and it's not supported for workspace paths.

Related

Gitlab CI/CD variables: how to access environment scoped variables within jobs?

I have defined the following CI/CD variable (VAULT_PATH) in my gitlab project.
As you can see in the image, the variable is environment scoped so, in order to access its value within my jobs ($VAULT_PATH), I have added the environment property to each job.
job_build_preprod:
environment: preprod
script:
- echo $VAULT_PATH
job_deploy_preprod:
environment: preprod
script:
- echo $VAULT_PATH
job_build_production:
environment: production
script:
- echo $VAULT_PATH
job_deploy_production:
environment: production
script:
- echo $VAULT_PATH
The problem I am facing following this approach is that my "build" jobs are being tagged as deployment jobs (due to the fact that I am adding the environment property) when they are not.
But if I do not add the environment property, I cannot access the environment scoped variable that I need.
So, is there another way to access environment scoped variables within jobs without using the environment property?
I need to use them within build jobs, but I do not want gitlab to tag those build jobs as deployment jobs to the environment.
Check out actions inside environment https://docs.gitlab.com/ee/ci/yaml/#environmentaction.
There are a few actions which you can use which won't trigger deployment.
eg: for build u can use prepare
job_build_preprod
script:
- echo $VAULT_PATH
environment:
name: preprod
action: prepare
url: https://test.com

Adding version number to Jenkins build artifact

I have been ordered to migrate a dotnet build from Bamboo to Jenkins. I used a Freestyle job to run a powershell script, using the PowerShell plugin and successfully built it. However I need to add version number to the built artifact. The Bamboo job uses:
~\.dotnet\tools\dotnet-lambda.exe package -pl $fullDir -f "netcoreapp3.1" -o Payment.${bamboo.majorVersion}.${bamboo.minorVersion}.${bamboo.revisionVersion}.${bamboo.buildNumber}.zip
I went into Jenkins Configuration and in Global Properties, created Environment variables named - buildNumber, majorVersion, minorVersion and revisionVersion, giving it values and in the Build part of the Freestyle job, I used:
~\.dotnet\tools\dotnet-lambda.exe package -pl $fullDir -f "netcoreapp3.1" -o Payment.${env.majorVersion}.${env.minorVersion}.${env.revisionVersion}.${env.buildNumber}.zip
However the name of the built artifact is: Payment.....zip
How can I pass the variable values?
Is there a way to auto increment the revisionNumber and buildNumber, instead of hard coding it?
I'm very new to both Bamboo and Jenkins. Any help would be extremely helpful.
Regards
Ramesh
Personally, I'd not configure this things globally as they seem job specific. Nevertheless,
Install the Environment Injector plugin. You now have two options:
General tab
[ X ] Prepare an environment for the run
Build Environment tab
[ X ] Inject environment variables to the build process
Set the "Properties Content" (that's an environment variable).
In your shell step( no need to preface with ${env....} ):
Execute Shell step:
#!sh -
echo ${FOO}.${BUILD_NUMBER}
echo ${LABEL}
Output:
[EnvInject] - Loading node environment variables.
[EnvInject] - Preparing an environment for the build.
[EnvInject] - Keeping Jenkins system variables.
[EnvInject] - Keeping Jenkins build variables.
[EnvInject] - Injecting contributions.
Building in workspace C:\Users\jenkins\.jenkins\workspace\Foo
[EnvInject] - Executing scripts and injecting environment variables after the SCM step.
[EnvInject] - Injecting as environment variables the properties content
FOO=bar
[EnvInject] - Variables injected successfully.
[Foo] $ sh - C:\Users\jenkins\AppData\Local\Temp\jenkins281351632631450693.sh
bar.8
Finished: SUCCESS
You'll also see at the bottom of the Execute Shell step a link to ${JENKINS_URL}/env-vars.html which lists variables available to shell scripts, which includes BUILD_NUMBER; use that in lieu of buildNumber.
The plugin also supports configuration same at both the Global and the Node level.
You can also have individual build steps to inject / change variables between job steps (we use this to set specific JAVA_HOME for SonarQube step).
You will also see an [Environment variables] button on the LH side of each build log to inspect what you ran with (see below).
If you add Build With Parameters plugin then you can be prompted to supply values for variables when triggering the job which can be consumed in the same fashion without re-configuring the job (it won't remember them, but you will see a [Parameters] button on the LH side of each build log to inspect what you ran with.
The Version Number plugin can provides greater flexibility, say you want to AutoIncrement and the "BUILD_NUMBER" option is too limiting, it offers a variable BUILDS_ALL_TIME, which can use the above defined variables or hard-coded constants to aggregate a version label and optionally control which it is incremented (eg: only increment on successful builds). eg:
[ X ] Prepare an environment for the run
Properties Content
FOO=bar
[ X ] Create a formatted version number
Environment Variable Name [ BUILD-${FOO}.${BUILDS_ALL_TIME} ]
Skip Builds worse than [ SUCCESS ]
Execute Shell step:
#!sh -
echo ${FOO}.${BUILD_NUMBER}
echo ${LABEL}
Output:
[Foo] $ sh - C:\Users\jenkins\AppData\Local\Temp\jenkins4160554383847615506.sh
bar.10
BUILD-bar.2

Permanently set environment variable in Azure Pipeline

While all sites about environment variables on Azure Pipelines seem to talk about setting variables before pipeline start, I want to set (change) the variable PATH in one step and use it in a later step.
But using
steps:
- script: source ./export_variables.sh
displayName: "export variables"
- script: $PATH
displayName: "verify"
condition: succeeded()
where ./export_variables.sh contains something like
#!/bin/bash
export PATH=abc/def/bin:$PATH
does not fulfill the task: In the verify step PATH does not contain abc/def/bin.
What has to be changed so that upates of $PATH become permanent on the machine?
I have had the same issue. It was solved with the following lines in the YAML
steps:
- bash: |
export PATH="${PATH}:${HOME}/.local/bin"
echo "##vso[task.setvariable variable=PATH;]${PATH}"
Which gives me the variable set over the entire stage. Note that this will overwrite the PATH so use with care :)

Access variables from Variable Groups inside Python script task in Azure DevOps Yaml pipeline

I'm using a Python script task of type 'file' in my Azure DevOps Yaml pipeline. I need to use the variables that I defined in my Variable Group in the Python file.
The following is my task on Azure devops yaml pipeline.
- task: PythonScript#0
displayName: 'Run a Python script'
inputs:
scriptPath: 'pythonTest.py'
Any advise on how I can achieve this?
Thanks!
You need to pass the variables to the script, using arguments, and you of course need to reference the variable group:
variables:
- group: variableGroup
steps:
- task: PythonScript#0
displayName: 'Run a Python script'
inputs:
scriptPath: 'pythonTest.py'
arguments: --variableInScript $(variableInVariableGroup)
And then use 'argparse' in the script.
https://learn.microsoft.com/en-us/azure/devops/pipelines/ecosystems/python?view=azure-devops#run-python-scripts
If you were using an inline script you could have done it like this:
- task: PythonScript#0
inputs:
scriptSource: 'inline'
script: |
print('variableInVariableGroup: $(variableInVariableGroup)')
According to the Azure DevOps official documentation:
System and user-defined variables also get injected as environment
variables for your platform. When variables are turned into
environment variables, variable names become uppercase, and periods
turn into underscores. For example, the variable name any.variable
becomes the variable name $ANY_VARIABLE.
So you just have to access the corresponding environment variable from your script. In Python, this can be done using the os.environ dictionary:
import os
my_var = os.environ["MY_VAR"]

Travis CI encrypted secret environment variables

Having a problem with Travis on Github. I encrypted two environment variables using:
travis encrypt MY_SECRET_ENV=super_secret --add env.matrix
I see two encrypted values under env:matrix:-secure:
env:
matrix:
- secure:
kQeMLwvGVBl...
- secure:
h7SXfIif5Y...
If I look at the commit info, I can see the first ENV variable on the first commit, and the second ENV variable on the next commit. The tests indicate the the second ENV variable clobbered the first, and so I only have one ENV variable set: only one of two test passes, depending on which ENV variable "wins".
Is there a way to set two encrypted ENV vars in .travis.yml?
Answer is here.
The Travis CI docs use env:matrix in their example, but that sets up TWO test runs, one for each ENV variable.
Instead of env:matrix, use env:global, which will cause one test to run with multiple ENV variables.

Resources