compress-build-log-plugin 使用

Jenkins plugin to compress the log file after build completion

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
这个插件是用来压缩构建log的.
会在builds目录下面把 原来的 log 文本文件压缩为 log.gz 文件. 例如下面的, 其中第1个构建 文本文件名称 还是叫 log, 但是第2个构建就是用这个插件压缩了,名称为log.gz了.
$ tree work/jobs
work/jobs
└── test_pipeline
├── builds
│   ├── 1
│   │   ├── build.xml
│   │   ├── log
│   │   ├── log-index
│   │   └── workflow
│   │   ├── 2.xml
│   │   └── 3.xml
│   ├── 2
│   │   ├── build.xml
│   │   ├── log.gz
│   │   ├── log-index
│   │   └── workflow
│   │   ├── 2.xml
│   │   └── 3.xml
│   ├── legacyIds
│   └── permalinks
├── config.xml
└── nextBuildNumber

6 directories, 14 files


$ tree work/jobs
work/jobs
├── test_freestyle
│   ├── builds
│   │   ├── 1
│   │   │   ├── build.xml
│   │   │   ├── changelog.xml
│   │   │   └── log.gz
│   │   ├── legacyIds
│   │   └── permalinks
│   ├── config.xml
│   └── nextBuildNumber
└── test_Pipeline
├── builds
│   ├── 1
│   │   ├── build.xml
│   │   └── log.gz
│   ├── 2
│   │   ├── build.xml
│   │   ├── log.gz
│   │   └── workflow
│   │   ├── 2.xml
│   │   └── 3.xml
│   ├── 3
│   │   ├── build.xml
│   │   ├── log.gz
│   │   └── workflow
│   │   ├── 2.xml
│   │   └── 3.xml
│   ├── 4
│   │   ├── build.xml
│   │   ├── log.gz
│   │   ├── log-index
│   │   └── workflow
│   │   ├── 2.xml
│   │   └── 3.xml
│   ├── lastFailedBuild -> 1
│   ├── lastStableBuild -> -1
│   ├── lastSuccessfulBuild -> -1
│   ├── lastUnstableBuild -> -1
│   ├── lastUnsuccessfulBuild -> 1
│   ├── legacyIds
│   └── permalinks
├── config.xml
├── lastStable -> builds/lastStableBuild
├── lastSuccessful -> builds/lastSuccessfulBuild
└── nextBuildNumber

14 directories, 31 files



压缩的log.gz文件文件在 freestyle 类型的job中是可以正常打开的,查看log都是没问题的, 但是在pipleline的job中打开就报错了,报这个
警告: Caught exception evaluating: it.writeLogTo(offset,output) in /jenkins/job/test_pipeline/2/console. Reason: java.io.FileNotFoundException: /jobs/test_pipeline/builds/2/log (没有那个文件或目录)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
追踪发现 bug 不是在 compress-build-log-plugin 插件上的, 是在 workflow-jo 插件 引用的 workflow-api-plugin 插件中的.


在页面点击 consoloe output 会调用到 workflow-job中的 WorkflowRun 类的下面这个方法中.

@SuppressWarnings("rawtypes")
@Override
public AnnotatedLargeText getLogText() {
return LogStorage.of(asFlowExecutionOwner()).overallLog(this, !isLogUpdated());
}



static @Nonnull LogStorage of(@Nonnull FlowExecutionOwner b) {
try {
for (LogStorageFactory factory : ExtensionList.lookup(LogStorageFactory.class)) {
LogStorage storage = factory.forBuild(b);
if (storage != null) {
// Pending integration with JEP-207 / JEP-212, this choice is not persisted.
return storage;
}
}
// Similar to Run.getLogFile, but not supporting gzip:
return FileLogStorage.forFile(new File(b.getRootDir(), "log"));
} catch (Exception x) {
return new BrokenLogStorage(x);
}
}


其中的这个方法也是过期的不推荐使用的.
@Deprecated
@Override public File getLogFile() {
LOGGER.log(Level.WARNING, "Avoid calling getLogFile on " + this, new UnsupportedOperationException());
return LogStorage.of(asFlowExecutionOwner()).getLogFile(this, !isLogUpdated());
}
/**
* Provide a file containing the log text.
* The default implementation creates a temporary file based on the current contents of {@link #overallLog}.
* @param build as in {@link #overallLog}
* @param complete as in {@link #overallLog}
* @return a possibly temporary file
* @deprecated Only used for compatibility with {@link Run#getLogFile}.
*/
@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "silly rule")
@Deprecated
default @Nonnull File getLogFile(@Nonnull FlowExecutionOwner.Executable build, boolean complete) {
try {
AnnotatedLargeText<FlowExecutionOwner.Executable> logText = overallLog(build, complete);
FlowExecutionOwner owner = build.asFlowExecutionOwner();
File f = File.createTempFile("deprecated", ".log", owner != null ? owner.getRootDir() : null);
f.deleteOnExit();
try (OutputStream os = new FileOutputStream(f)) {
// Similar to Run#writeWholeLogTo but terminates even if !complete:
long pos = 0;
while (true) {
long pos2 = logText.writeRawLogTo(pos, os);
if (pos2 <= pos) {
break;
}
pos = pos2;
}
}
return f;
} catch (Exception x) {
Logger.getLogger(LogStorage.class.getName()).log(Level.WARNING, null, x);
if (build instanceof Run) {
return new File(((Run) build).getRootDir(), "log");
} else {
return new File("broken.log"); // not much we can do
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

其中的这个getLogFile方法中也没用去判断log文件是否是log.gz文件.所以就导致了文件找不到报错了.


在freestyle风格的job中点击 consoloe output 会调用到下面这个方法中
public @Nonnull AnnotatedLargeText getLogText() {
return new AnnotatedLargeText(getLogFile(),getCharset(),!isLogUpdated(),this);
}

/**
* Returns the log file.
* @return The file may reference both uncompressed or compressed logs
* @deprecated Assumes file-based storage of the log, which is not necessarily the case for Pipelines after JEP-210. Use other methods giving various kinds of streams such as {@link Run#getLogReader()}, {@link Run#getLogInputStream()}, or {@link Run#getLogText()}.
*/
@Deprecated
public @Nonnull File getLogFile() {
File rawF = new File(getRootDir(), "log");
if (rawF.isFile()) {
return rawF;
}
File gzF = new File(getRootDir(), "log.gz");
if (gzF.isFile()) {
return gzF;
}
//If both fail, return the standard, uncompressed log file
return rawF;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
综上来看,要么修改  workflow-job 插件, 要么修改 workflow-api 插件. 通过查找发现 有人已经提交了一个PR 在 workflow-api 插件中,
https://github.com/jenkinsci/workflow-api-plugin/pull/95
其中有个bug描述
https://issues.jenkins-ci.org/browse/JENKINS-54678
其他重复的bug描述还有:
JENKINS-55465 Empty console when properties compressBuildLog() is enabled
JENKINS-54680 Compressed build logs not rendering with update to workflow-job plugin >= 2.26
JENKINS-55024 Pipeline Job Plugin (workflow-job-plugin) gives empty console output when an error occurs



然后通过查找发现还有一个修改是 修改 compress-buildlog 插件的也就是在pipeline job禁用掉这个插件 不过我觉得这不是个好方法.
https://github.com/jenkinsci/compress-buildlog-plugin/pull/10