Skip to content

Commit

Permalink
fix: 合并环境变量时发生 NPE 错误
Browse files Browse the repository at this point in the history
close #198,#199
  • Loading branch information
liuweiGL committed Feb 16, 2023
1 parent bfa42fe commit 3e91374
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
22 changes: 16 additions & 6 deletions src/main/java/io/jenkins/plugins/DingTalkRunListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import io.jenkins.plugins.tools.DingTalkUtils;
import io.jenkins.plugins.tools.Logger;
import io.jenkins.plugins.tools.Utils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -65,6 +64,9 @@ public void onCompleted(Run<?, ?> run, @NonNull TaskListener listener) {
} catch (Exception e) {
e.printStackTrace();
DingTalkUtils.log(listener, "发送消息时报错: %s", e);
}finally {
// 重置环境变量
PipelineEnvContext.reset();
}
}

Expand Down Expand Up @@ -162,14 +164,22 @@ private EnvVars getEnvVars(Run<?, ?> run, TaskListener listener) {
EnvVars jobEnvVars;
try {
jobEnvVars = run.getEnvironment(listener);
} catch (InterruptedException | IOException e) {
} catch (Exception e) {
jobEnvVars = new EnvVars();
log.error(e);
DingTalkUtils.log(listener, "获取环境变量时发生异常,将只使用 jenkins 默认的环境变量。");
DingTalkUtils.log(listener, "获取 job 任务的环境变量时发生异常");
DingTalkUtils.log(listener, ExceptionUtils.getStackTrace(e));
Thread.currentThread().interrupt();
}
try {
EnvVars pipelineEnvVars = PipelineEnvContext.get();
jobEnvVars.overrideAll(pipelineEnvVars);
}catch (Exception e){
log.error(e);
DingTalkUtils.log(listener, "获取 pipeline 环境变量时发生异常");
DingTalkUtils.log(listener, ExceptionUtils.getStackTrace(e));
}
EnvVars pipelineEnvVars = PipelineEnvContext.get();
return jobEnvVars.overrideAll(pipelineEnvVars);
return jobEnvVars;
}

private boolean skip(TaskListener listener, NoticeOccasionEnum noticeOccasion,
Expand Down Expand Up @@ -238,7 +248,7 @@ private void send(Run<?, ?> run, TaskListener listener, NoticeOccasionEnum notic
.executorName(executorName)
.executorMobile(executorMobile)
.content(
envVars.expand(content).replaceAll("\\\\n", "\n")
envVars.expand(content).replace("\\\\n", "\n")
)
.build()
.toMarkdown();
Expand Down
32 changes: 19 additions & 13 deletions src/main/java/io/jenkins/plugins/context/PipelineEnvContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@

public class PipelineEnvContext {

private final static ThreadLocal<EnvVars> store = new ThreadLocal<>();
private static final ThreadLocal<EnvVars> store = new ThreadLocal<>();

public static void merge(EnvVars value) {
EnvVars current = store.get();
public static void merge(EnvVars value) {
if (value == null) {
return;
}
EnvVars current = store.get();
if (current == null) {
store.set(value);
} else {
current.overrideAll(value);
}
}

if (current == null) {
store.set(value);
} else {
current.overrideAll(value);
}
}

public static EnvVars get() {
return store.get();
}
public static EnvVars get() {
EnvVars current = store.get();
return current == null ? new EnvVars() : current;
}

public static void reset() {
store.remove();
}
}

0 comments on commit 3e91374

Please sign in to comment.