Environment
standard
example 1: sample
pipeline{
agent{
label "k8s-permanent"
}
environment{
name1 = "nim1"
name2 = "nim2"
}
stages{
stage("Build"){
environment{
name3 = "nim3"
}
steps{
echo "name1 ${name1}"
echo "name2 ${name2}"
echo "name3 ${name3}"
}
}
}
}
example 2: environment didn’t declared in stage Pipeline
pipeline{
agent{
label "k8s-permanent"
}
environment{
name1 = "nim1"
name2 = "nim2"
}
stages{
stage("Build"){
environment{
name3 = "nim3"
}
steps{
echo "name1 ${name1}"
echo "name2 ${name2}"
echo "name3 ${name3}"
}
}
stage("Test"){
steps{
echo "name1 ${name1}"
echo "name2 ${name2}"
echo "name3 ${name3}"
}
}
}
}
Example 3: environment declared at both global and stage Pipeline
pipeline{
agent{
label "k8s-permanent"
}
environment{
name1 = "nim1"
name2 = "nim2"
}
stages{
stage("Build"){
environment{
name2 = "change_nim2"
name3 = "nim3"
}
steps{
echo "name1 ${name1}"
echo "name2 ${name2}"
echo "name3 ${name3}"
}
}
}
}
example 4: print env of system
pipeline{
agent{
label "k8s-permanent"
}
environment{
name1 = "nim1"
name2 = "nim2"
}
stages{
stage("Build"){
environment{
name2 = "change_nim2"
name3 = "nim3"
}
steps{
echo "name1 ${name1}"
echo "name2 ${name2}"
echo "name3 ${name3}"
sh "printenv"
}
}
}
}
credential
pipeline{
agent{
label "k8s-permanent"
}
environment{
user_pass = credentials("e375d6c5-54b2-4881-996e-58430673a8f4")
text_secret = credentials("ad2def08-0d61-4503-a6d5-a98666d730fd")
}
stages{
stage("Build"){
steps{
echo "user_pass ${user_pass}"
echo "user_pass ${user_pass_USR}" // prefix _USR lấy username
echo "user_pass ${user_pass_PSW}" // prefix _PSW lấy passwork
echo "user_pass ${text_secret}"
}
}
}
}