uWSGI set configuration depending on environment variable value - uwsgi

I have an environment variable DEBUG. Its value either could be true or false. I want to set few configurations based on its value.
[uwsgi]
if-opt = $(DEBUG)=true
print = Inside if condition
endif =
Print statement is not executing. DEBUG is setup in the kubernetes deployment
- name: DEBUG
value: "true"

Related

Environment directive with secret file content not passed to shell stage

I'm trying to use the content of a Secret File as an environment variable as follows:
stage("Terraform_plan"){
environment {
TF_VAR__private_key = credentials('SFTP_DEV_KEY')
}
sh '''
## printenv --> Can't see the TF_VAR__private_key env var.
terraform -chdir=terraform/${COMPONENT} init -var-file=./tfvars/${ENV}.${GROUP}.tfvars -upgrade -input=false
terraform -chdir=terraform/${COMPONENT} plan -var- file=./tfvars/${ENV}.${GROUP}.tfvars -out=./plan
'''
}
Getting this error from Terraform:
Error: No value for required variable
on variables.tf line 77:
77: variable "private_key" {
The root module input variable "private_key" is not set, and has no
default value. Use a -var or -var-file command line argument to provide a
value for this variable.
Also tried:
stage("Terraform_plan"){
environment {
TF_VAR_private_key = credentials('SFTP_DEV_KEY')
}
sh '''
export TF_VAR_private_key = '${env.TF_VAR_private_key}'
## printenv --> Can't see the TF_VAR__private_key env var.
terraform -chdir=terraform/${COMPONENT} init -var-file=./tfvars/${ENV}.${GROUP}.tfvars -upgrade -input=false
terraform -chdir=terraform/${COMPONENT} plan -var- file=./tfvars/${ENV}.${GROUP}.tfvars -out=./plan
'''
}
This works as Terraform goes to the Apply stage but still when applying I'm still getting an error that there's no value for the env var.
printenv shows TF_VAR_private_key=${env.TF_VAR_private_key} and not the value of TF_VAR_private_key.
However, if I try to export the env var with export TF_VAR_private_key it works inside shell.
Shouldn't the env var already be set inside the shell?
Thanks in advance,

docker-compose.yml problem with directives in file

What does this mean in docker-compose.yml directive?
${{ variable }}
And how is it used?
In a pipeline, template expression variables (${{ variables.var }}) get processed at compile time, before runtime starts. Macro syntax variables ($(var)) get processed during runtime before a task runs. Runtime expressions ($[variables.var]) also get processed during runtime but were designed for use with conditions and expressions. When you use a runtime expression, it must take up the entire right side of a definition.
In the following example, you can see that the template expression still has the initial value of the variable after the variable is updated. The value of the macro syntax variable updates. The template expression value does not change because all template expression variables get processed at compile time before tasks run. In contrast, macro syntax variables are evaluated before each task runs.
variables:
- name: one
value: initialValue
steps:
- script: |
echo ${{ variables.one }} # outputs initialValue
echo $(one)
displayName: First variable pass
- bash: echo "##vso[task.setvariable variable=one]secondValue"
displayName: Set new variable value
- script: |
echo ${{ variables.one }} # outputs initialValue
echo $(one) # outputs secondValue
displayName: Second variable pass

Jenkins set value for environment variable from one stage to be used in another

I have an environment var with default value
RUN_TESTS= true
My Jenkins job contains two stages, build and test stage.
The test stage modified to run 2 kind of tests sets depended on RUN_TESTS value.
RUN_TESTS value is set on build stage.
Is there a way to update value for environment variable for future use ?
Here what I tried without success
env.RUN_TESTS= true
node("node1"){
sh '''
printenv
RUN_TESTS="false"
'''
}
print RUN_TESTS
I am getting
+ RUN_TESTS=false
[Pipeline] }
[Pipeline] // node
[Pipeline] echo
true
[Pipeline] End of Pipeline
You can define your variable at the top of the jenkinsfile
environment {
RUN_TESTS=false
}
Then change it like this:
RUN_TESTS = true
with no "" IF you are assigning this directly from the jenkinsfile. However, in your case, you are using it within and sh command, so you might have to use something like
${RUN_TESTS} = true
Although I haven't been able to test it so I can't give a guarantee that it will work.
When you issue a sh directive, a new instance of shell (most likely bash) is created. As usual Unix processes, it inherits the environment variables of the parent. Notably, this includes RUN_TESTS variable and its value. So a copy of Jenkins environment variable RUN_TESTS is created for sh directive.
Your bash instance is then running your script. When your script sets or updates an environment variable, the environment of bash is updated. Once your script ends, the bash process that ran the script is destroyed, and all its environment is destroyed with it. Notably, this includes your updated RUN_TESTS variable and its value. This has no influence on the Jenkins environment variable RUN_TESTS that had its value copied before.
Here's a Python example demonstrating the same concept:
>>> def foo(i):
... print(i)
... i += 1
... print(i)
...
>>> i = 6
>>> i
6
>>> foo(i)
6
7
>>> i
6
The variable i internal to function foo was first initialized to 6, then incremented to 7, then destroyed. The outer variable bearing the same name i was copied for the invocation of foo but wasn't changed as a result.
Use it like this
String RUN_TESTS = "true"
node("node1"){
sh '''
printenv
RUN_TESTS = "false"
'''
}
print RUN_TESTS

How to use groovy to populate an env variable in a declarative jenkins pipeline

I am struggling to populate an environment variable in a Jenkinsfile using groovy
The code below fails:
pipeline {
environment {
PACKAGE_NAME = JOB_NAME.tokenize('/')[1]
}
{
with the following error:
Environment variable values can only be joined together with ‘+’s
What am I doing wrong? Sorry if the question is basic, I am just starting with both groovy and pipelines.
Declarative pipeline is pretty strict about the values you can assign to environment variables. For instance, if you try to set PACKAGE_NAME to JOB_NAME you will get following error:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 5: Environment variable values must either be single quoted, double quoted, or function calls. # line 5, column 24.
PACKAGE_NAME = JOB_NAME
To avoid getting errors and set PACKAGE_NAME env variable as expected you can put it into double quotes and evaluate expression inside the GString:
environment {
PACKAGE_NAME = "${JOB_NAME.tokenize('/')[1]}"
}

Jenkins Global environment variables in Jenkinsfile

How do I invoke Global environment variables in Jenkinsfile?
For example, if I have a variable -
name:credentialsId
value:xxxx-xxxx-xxxxx-xxxxxxxxx
How do I use it in the groovy script?
I tried ${credentialsId}, but it didn't work. It will just give error:
java.lang.NoSuchMethodError: No such DSL method '$' found among steps [ArtifactoryGradleBuild, ........
In a Jenkinsfile, you have the "Working with the Environment" which mentions:
The full list of environment variables accessible from within Jenkins Pipeline is documented at localhost:8080/pipeline-syntax/globals#env,
The syntax is ${env.xxx} as in:
node {
echo "Running ${env.BUILD_ID} on ${env.JENKINS_URL}"
}
See also "Managing the Environment".
How can I pass the Global variables to the Jenkinsfile?
When I say Global variables - I mean in
Jenkins -> Manage Jenkins -> Configure System -> Global properties -> Environment variables
See "Setting environment variables"
Setting an environment variable within a Jenkins Pipeline can be done with the withEnv step, which allows overriding specified environment variables for a given block of Pipeline Script, for example:
Jenkinsfile (Pipeline Script)
node {
/* .. snip .. */
withEnv(["NAME=value"]) {
... your job
}
}
When referring to env in Groovy scope, simply use env.VARIABLE_NAME, for example to pass on BUILD_NUMBER of upstream job to a triggered job:
stage ('Starting job') {
build job: 'TriggerTest', parameters: [
[$class: 'StringParameterValue', name: 'upstream_build_number', value: env.BUILD_NUMBER]
]
}
Scripted pipeline
To read an environment variable whose name you know, use env.NAME
To read an environment variable whose name is not known until runtime use env.getProperty(name).
For example, a value from a YAML config file represents an environment variable name:
config.yaml (in workspace)
myconfig:
key: JOB_DISPLAY_URL
Jenkinsfile
node {
println("Running job ${env.JOB_NAME}")
def config = readYaml(file:'config.yaml')
def value = env.getProperty(config.myconfig.key)
println("Value of property ${config.myconfig.key} is ${value}")
}
For getting values all env.VAR, env['VAR'], env.getProperty('VAR') are fine.
For setting values the only safe way at the moment is withEnv. If you try to assign values to env.VAR it may not work in some cases like for parallel pipelines (like in JENKINS-59871).
Another syntax is $ENV:xxxx
node {
echo "Running $ENV.BUILD_ID on $ENV.JENKINS_URL" }
This worked for me

Resources