简体   繁体   English

如何以编程方式导出Jenkins构建变量

[英]How to export Jenkins build variable programatically

I'm trying to do a slight modification to this plugin https://github.com/bitbar/testdroid-run-in-cloud-plugin . 我正在尝试对该插件https://github.com/bitbar/testdroid-run-in-cloud-plugin进行一些修改。

I want to export the test results URL that are provided by the Testdroid API as a job environment variable something like TEST_CLOUD_LINK. 我想将Testdroid API提供的测试结果URL导出为作业环境变量,例如TEST_CLOUD_LINK。 I found the variable that holds this information in the CloudLink class but I'm not sure on how I could export it as a env variable to use in the build. 我在CloudLink类中找到了保存此信息的变量,但不确定如何将其导出为要在构建中使用的env变量。

Anyone can help with an example? 任何人都可以帮助举一个例子吗?

I managed to get it working. 我设法使它起作用。

Fist I defined an Action class implementing EnvironmentContributingAction interface. 我首先定义了一个实现EnvironmentContributingAction接口的Action类。

public class RunInCloudEnvInject implements EnvironmentContributingAction {

    private String key;

    private String value;

    public RunInCloudEnvInject(String key, String value) {
        this.key = key;
        this.value = value;
    }

    @Override public void buildEnvVars(AbstractBuild<?, ?> abstractBuild, EnvVars envVars) {
        if (envVars != null && key != null && value != null) {
            envVars.put(key, value);
        }
    }

    @Override public String getIconFileName() {
        return null;
    }

    @Override public String getDisplayName() {
        return "RunInCloudBuilderEnvInjectionAction";
    }

    @Override public String getUrlName() {
        return null;
    }
}

After that I modified this snippet bellow inside the @Override perform() method of the class that implements the abstract builder. 之后,我在实现抽象生成器的类的@Override perform()方法中修改了以下代码段。

        String cloudLink = String.format("%s/#service/testrun/%s/%s", cloudLinkPrefix, testRun.getProjectId(),
                testRun.getId());
        build.getActions().add(new CloudLink(build, cloudLink));

        RunInCloudEnvInject variable = new RunInCloudEnvInject("CLOUD_LINK", cloudLink);
        build.addAction(variable);

Now I can use my CLOUD_LINK environment variable inside the Jenkins build to post the results url in a Slack notification for example. 现在,我可以在Jenkins内部使用我的CLOUD_LINK环境变量在例如Slack通知中发布结果url。

Here is the pull request https://github.com/jenkinsci/testdroid-run-in-cloud-plugin/pull/4 这是拉取请求https://github.com/jenkinsci/testdroid-run-in-cloud-plugin/pull/4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM