JaCoCo 代码覆盖率实践分享

本文适用的是 Gradle 来构建和适用 JaCoCo。

分别介绍了 build.gradle 的文件配置,执行测试和生成报告,报告参数说明,以及如何忽略指定的包或类从而影响测试覆盖率的结果。

build.gradle 文件配置

比如使用 gradle 来管理的项目可以在 build.gradle 里添加如下代码

plugins {
id 'jacoco'
}

jacoco {
toolVersion = "0.8.5"
}

test {
useJUnitPlatform()
exclude '**/**IgnoreTest.class' // 如果有 test case 不通过,如有必要可以通过这样忽略掉
finalizedBy jacocoTestReport // report is always generated after tests run
}

jacocoTestReport {
dependsOn test // tests are required to run before generating the report
reports {
xml.enabled true
csv.enabled false
html.destination file("${buildDir}/reports/jacoco")
}
}

执行测试,生成代码覆盖率报告

然后执行 gradle test 就可以了。之后可以可以在 build\reports\jacoco 目录下找到报告了。

JaCoCo报告

重点是如何分析报告。打开 index.html,报告显示如下:

JaCoCo报告首页

报告参数说明

Read More

你的 Python 代码够不够 Pythonic?

Python 不必多说,它是众多编程语言中最容易学习的动态类型语言。它的跨平台、易读、易写、丰富的 Packages 等众多特性,也是众多DevOps/测试/开发工程师是最常用的语言之一。

相信不少人用它完成了很多工作,但你是不是仅仅止步于功能的实现而忽略了去写出更加简洁,优美的 Pythonic 代码呢?

在我最开始用 Python 时,我还不知道 Pythonic 这个词,直到多年前一位资深的程序员在给我培训的时候提到了项目中有一些代码不够 Pythonic,需要重构。根据语境,我理解他的意思:就是 Python 的代码没有按照 Python 的方式来写。

Read More

Different branches have different default parameters in Jenkins

Problem

When you use Jenkins multibranch pipeline, you may want to have different default parameters settings for defferent branches build.

For example:

For develop/hotfix/release branches, except regular build, you also want to do some code analyzes, like code scanning, etc.
For other branches, like feature/bugfix or Pull Request that you just want to do a regular build.

So you need to have dynamic parameter settings for your multibranch pipeline job.

Read More

基于谷歌代码审查(Code Review)法则的思考与实践

背景

代码审查(Code Review),就是让别人来审查你的代码,其目的就是确保代码库的整体代码运行状况随着时间推移而不断改善。

Read More

Jenkins upgrade issue "Windows agents won't start" workaround

Today, when I tried to upgrade my team’s Jenkins server from Jenkins 2.235.1 to Jenkins 2.263.3, I met a problem that can not launch the Windows agent.

[2021-01-29 23:50:40] [windows-agents] Connecting to xxx.xxx.xxx.xxx
<!-- more -->
Checking if Java exists
java -version returned 11.0.2.
[2021-01-29 23:50:40] [windows-agents] Installing the Jenkins agent service
[2021-01-29 23:50:40] [windows-agents] Copying jenkins-agent.exe
ERROR: Unexpected error in launching an agent. This is probably a bug in Jenkins
Also: java.lang.Throwable: launched here
at hudson.slaves.SlaveComputer._connect(SlaveComputer.java:286)
at hudson.model.Computer.connect(Computer.java:435)
at hudson.slaves.SlaveComputer.doLaunchSlaveAgent(SlaveComputer.java:790)
...
...
at java.lang.Thread.run(Thread.java:748)
java.lang.NullPointerException
at hudson.os.windows.ManagedWindowsServiceLauncher.launch(ManagedWindowsServiceLauncher.java:298)

This issue had been raised in the Jenkins Jira project: JENKINS-63198 and JENKINS-63198

There is also a Windows Support Updates guide here that mentioned this problem.

Finally, I fixed this problem by the following steps:

  1. Update windows-slaves-plugin to the lastest version 1.7 (fixes for Jenkins 2.248+)

Then the error should be like this

[2021-01-30 23:53:40] [windows-agents] Connecting to xxx.xxx.xxx.xxx
Checking if Java exists
java -version returned 11.0.2.
[2021-01-30 23:53:47] [windows-agents] Copying jenkins-agent.xml
[2021-01-30 23:53:48] [windows-agents] Copying agent.jar
[2021-01-30 23:53:48] [windows-agents] Starting the service
ERROR: Unexpected error in launching an agent. This is probably a bug in Jenkins
org.jinterop.dcom.common.JIException: Unknown Failure
at org.jvnet.hudson.wmi.Win32Service$Implementation.start(Win32Service.java:149)
Caused: java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedMethodAccessor219.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.kohsuke.jinterop.JInteropInvocationHandler.invoke(JInteropInvocationHandler.java:140)
Also: java.lang.Throwable: launched here
  1. Then change jenkins-agent.exe.config file. remove or comment out this line <supportedRuntime version="v2.0.50727" /> as below

also do this for jenkins-slave.exe.config in case it also exists.

<configuration>
<runtime>
<!-- see http://support.microsoft.com/kb/936707 -->
<generatePublisherEvidence enabled="false"/>
</runtime>
<startup>
<!-- this can be hosted either on .NET 2.0 or 4.0 -->
<!-- <supportedRuntime version="v2.0.50727" /> -->
<supportedRuntime version="v4.0" />
</startup>
</configuration>
  1. Then try to Launch agent.

If it still does not work and has this error message “.NET Framework 2.0 or later is required on this computer to run a Jenkins agent as a Windows service”, you need to upgrade your .NET Framework.

Here is a link for update .NET Framework.

Hopefully, this could help you to fix connect the issue of the Windows agent. Let me know in case of any questions.

2021年DevOps工程师的学习路线

DevOps 实际上是什么意思?🤔

DevOps 是一种软件开发方法,涉及持续开发,持续测试,持续集成,部署和监视。这一系列过程跨越了传统上孤立的开发和运营团队,DevOps 试图消除它们之间的障碍。

因此,DevOps 工程师基本上与 Development 和 Operations 团队合作。它是这两个主要部分之间的链接。

Read More

预测 2021 年的 DevOps 趋势

DevOps 已经走了很长一段路,毫无疑问,它将在今年继续发光。由于许多公司都在寻求有关数字化转型的最佳实践,因此重要的是要了解领导者认为行业发展的方向。从这个意义上讲,以下文章是 DevOps 领导者对 DevOps 趋势的回应的集合,需要在 2021 年关注。

让我们看看他们每个人对来年的 DevOps 有何评价。

Read More

What's the difference between result and currentResult in Jenkins?

This is just a note to myself about the difference between Jenkins result and currentResult.

Declarative Pipeline

Read More

These settings in Bitbucket/GitHub recommends enable

As I have management our team’s git repositories for more than two years, and as my daily work using Bitbucket, so I’ll take it as an example.

Here are some settings recommend you to enable.

  1. Set Reject Force Push
  2. Set Branch Prevent deletion
  3. Set tags for each hotfix/GA releases
  4. Merge Check -> Minimum approvals (1)
  5. Yet Another Commit Checker

Read More

How to open port 22 and make it listening on Windows

Recently our Bamboo server has an error when connecting to the Windows build system.

Some errors like: Can not connect to the host XXXX 22 port.

I log into the build machine find port 22 with the below command

netstat -aon|findstr "22"

but not found port 22 is inbound.

Read More

git sparse-checkout enable and disable

This post just a note to myself and it works on my environment. it has not been widely tested.

Enable git sparse-checkout

Read More

解决 Code Sign 默认时间戳服务器 http://timestamp.verisign.com/scripts/timstamp.dll 不可用

相信许多程序员在新年开始在做 code sign (数字签名)的时候可能遇到 Verisign Timestamp 服务器不好用了 http://timestamp.verisign.com/scripts/timstamp.dll 的情况。出现了如下错误:

Code Sign 失败了

Read More

2021 年国外 IT 公司对于 DevOps 工程师的要求有哪些?

前言

自 2020 年因疫情开始,越来越多的 IT 公司都因不得不在家办公从而彻底转为 WFH(Work From Home) 公司,因此对于 IT 从业者来说,工作机会今后将会是全球性的。

如果你有意想进入一个跨国公司工作,想与世界各地的人在一起工作,那么就不能仅仅的关注国内的这些大厂,要将眼光放眼到全世界,看看这些耳熟能详的公司对于工程师的职位要求有哪些。

今天就先来看看 DevOps 岗位的需求是什么样的,了解这些,一来可以帮助我们在2021 年树立学习方向,而来如果你有意向去这些公司,了解并提早做准备才能有机会获取你想要的岗位。

由于这些职位的介绍和要求会很长,因此我就先说结论。

主要技能

  • 国外很多公司他们使用的云服务商主要是 AWS,因此熟悉和使用 AWS
  • 熟练使用 DevOps 工具,如 Jenkins, Ansible(Chef), Git 等
  • Docker 和 Kubernetes 是每个想从事 DevOps 需要掌握的
  • 熟悉操作系统,至少要 Linux
  • 大多数都是要求 Python 很熟练,高级一些的岗位会要求熟悉 Go, Java 语言
  • 最后,乐于学习,积极主动,具有创造性思维是每个 DevOps 最重要的特质,因此新的技术和工具层出不穷,我们需要保持和新进同行

具体的职位要求细节,请看后面的职位介绍吧 …

Read More

2020 年终总结

时间飞快,马上就迎来了 2021 年,又到了总结自己这一年来的所有的经历和收获的时候了。

2020 是非比寻常的一年,对于每个人不论是生活还是工作,都因为新冠病毒被影响着。

但生活总归要继续。刷微博、抖音各种社交媒体是一天;工作学习也是一天。我是那种一段时间感觉没进步就特别恐慌的人,做完后者总会让我感觉更安心一些。

Read More

How to backup Jenkins

I believe most of you have used the Jenkins configuration as code principle, so you build/test/release process will be described in code.

It looks like all is good, but not all of the configuration is in code, same of the Jenkins system configuration is store in the Jenkins service, so we also need to backup Jenkins in case of any disaster.

There are two ways to backup Jenkins, one used Jenkins plugin, the other is create shell scripts.

Using plug-in backup

I used the ThinBackup plugin, here is my thinBackup configuration.

ThinBackup Configuration

Read More