Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for shared lib and params variables being null in environment section #529

Merged
merged 1 commit into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,38 @@ abstract class GenericPipelineDeclaration {
env.env = env
env.currentBuild = delegate.binding.currentBuild

def cl = this.environment.rehydrate(env, delegate, this)
def cl = this.environment.rehydrate(wrapEnv(env as Map<String, Object>), delegate, this)
cl.resolveStrategy = DELEGATE_FIRST
cl.call()
}
}

private def wrapEnv(Map<String, Object> env) {
return new GroovyObject() {
Object invokeMethod(String name, Object args) {
GenericPipelineDeclaration.this.invokeMethod(name, args)
}

Object getProperty(String propertyName) {
if (env.containsKey(propertyName)) {
env.get(propertyName)
} else {
GenericPipelineDeclaration.this.getProperty(propertyName)
}
}

void setProperty(String propertyName, Object newValue) {
env.put(propertyName, newValue)
}

MetaClass getMetaClass() {
GenericPipelineDeclaration.this.metaClass
}

void setMetaClass(MetaClass metaClass) {
throw new UnsupportedOperationException("This is wrapper object. Setting metadata class is not supported here.")
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.lesfurets.jenkins

import com.lesfurets.jenkins.unit.declarative.DeclarativePipelineTest
import org.junit.Assert
import org.junit.Before
import org.junit.Test

import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library
import static com.lesfurets.jenkins.unit.global.lib.ProjectSource.projectSource

class TestSharedLibraryEnvVariable extends DeclarativePipelineTest {

String sharedLibVars = this.class.getResource("/libs/env_var_not_defined").getFile()

@Override
@Before
void setUp() throws Exception {
scriptRoots += 'src/test/jenkins'

super.setUp()

def library = library().name("env_var_not_defined")
.retriever(projectSource(sharedLibVars))
.defaultVersion("master")
.targetPath(sharedLibVars)
.allowOverride(true)
.implicit(false)
.build()
helper.registerSharedLibrary(library)
}

@Test
void "test lib var not defined in env"() {

runScript("job/library/test_lib_var_not_defined_in_env.jenkins")

def prop1 = binding.env["prop1"]
Assert.assertEquals("magic", prop1)
}

@Test
void "test params not defined in env"() {
def version = "1.2.0"
addParam("VERSION", version)

runScript("job/library/test_params_not_defined_in_env.jenkins")

def versionFromEnv = binding.env["VERSION"]
Assert.assertEquals(version, versionFromEnv.toString())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@Library("env_var_not_defined") _

pipeline {

environment {
prop1 = envHelper.property1()
}

stages {
stage('Test stage'){
steps {
echo "prop1=${env.prop1}"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pipeline {
agent none
parameters {
string name: 'VERSION', defaultValue: 'default-version'
}
environment {
VERSION = "${params.VERSION}"
}
stages {
stage('Print VERSION') {
steps {
echo env.VERSION // null when VERSION = "${VERSION}"
echo VERSION // default-version when VERSION = "${VERSION}"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

def property1() {
return "magic"
}