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

Defining environment variables is not clear #617

Open
stefan6419846 opened this issue Aug 3, 2023 · 1 comment
Open

Defining environment variables is not clear #617

stefan6419846 opened this issue Aug 3, 2023 · 1 comment

Comments

@stefan6419846
Copy link

Describe your use-case which is not covered by existing documentation.

In the documentation inside the README we have:

You can define both environment variables and job execution parameters.
```groovy
import com.lesfurets.jenkins.unit.BasePipelineTest
class TestExampleJob extends BasePipelineTest {
@Override
@BeforeEach
void setUp() {
super.setUp()
// Assigns false to a job parameter ENABLE_TEST_STAGE
addParam('ENABLE_TEST_STAGE', 'false')
// Defines the previous execution status
binding.getVariable('currentBuild').previousBuild = [result: 'UNSTABLE']
}
@Test
void verifyParam() {
assertEquals('false', binding.getVariable('params')['ENABLE_TEST_STAGE'])
}
}
```

How does this cover setting environment variables? For me, it seems like this will only set a parameter/params value, but no env one. Playing around with it seems to be indicate that this might be achieved by using binding.getVariable('env').newValue = 'test', but it might make sense to actually document this as well.

Reference any relevant documentation, other materials or issues/pull requests that can be used for inspiration.

No response

@ryeleo
Copy link
Contributor

ryeleo commented Aug 9, 2023

Great note, I also ran into this. I dug into BasePipelineTest.groovy and found there is an addEnvVar method defined! Perfect!

I've updated the example to use addEnvVar:

ℹ Also, I was actually looking to add a 'Jenkins global variable' to mock the RUN_DISPLAY_URL Blue Ocean variable, so I added that to the example also.

import com.lesfurets.jenkins.unit.BasePipelineTest 
 
class TestExampleJob extends BasePipelineTest { 
    @Override 
    @BeforeEach 
    void setUp() { 
        super.setUp() 
        // Define environment variable 
        addEnvVar('MY_ENV_VAR', '0')
        // Assigns false to a job parameter ENABLE_TEST_STAGE 
        addParam('ENABLE_TEST_STAGE', 'false')
        // Defines the previous execution status 
        binding.getVariable('currentBuild').previousBuild = [result: 'UNSTABLE'] 
        // Define Jenkins global variable
        binding.setVariable('RUN_DISPLAY_URL', 'https://jenkins.io')
    } 
 
    @Test 
    void verifyParam() { 
        assertEquals('false', binding.getVariable('params')['ENABLE_TEST_STAGE']) 
    } 
} 

I can make a pull request with some docs improvements when I have time. I'm loving this project! 😄🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants