Search code examples
jenkinsjenkins-pipelineallureautotest

Set environment variables to Allure report from pipeline script


I'm running Jenkins parameterized jobs with the following pipeline configuration of allure step:

stage('Generate reports') {
    allure([
        includeProperties: false,
        jdk              : '',
        properties       : [],
        reportBuildPolicy: 'ALWAYS'
        results          : [[path: webdriverTestResultsPath], [path: unitTestResultsPath]]])
}

Build's parameters, which are set before starting the job, become available within getEnv() in my tests. I'd like to show some of them in the Environment section of Allure report's dashboard. For instance, there is a HOST build parameter which specifies the base application url.

Is there a way to do this?


Solution

  • Disclosure: I've created Java library which deals with this issue: https://github.com/AutomatedOwl/allure-environment-writer

    It uses TransformerFactory to write the environment.xml to the allure-results path in any stage of the test. It also checks for the directory existence in case running from cleaned build.

    Usage example:

    import static com.github.automatedowl.tools.AllureEnvironmentWriter.allureEnvironmentWriter;
    
    public class SomeTests {
    
        @BeforeSuite
        void setAllureEnvironment() {
            allureEnvironmentWriter(
                    ImmutableMap.<String, String>builder()
                            .put("Browser", "Chrome")
                            .put("Browser.Version", "70.0.3538.77")
                            .put("URL", "http://testjs.site88.net")
                            .build(), System.getProperty("user.dir")
                            + "/allure-results/");
        }
    
        @Test
        void someTest() {
            Assert.assertTrue(true);
        }
    }