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

Here is a test code from this ticket JENKINS-46325

pipeline {
agent any
stages {
stage ('Init') {
steps {
echo "Init result: ${currentBuild.result}"
echo "Init currentResult: ${currentBuild.currentResult}"
}
post {
always {
echo "Post-Init result: ${currentBuild.result}"
echo "Post-Init currentResult: ${currentBuild.currentResult}"
}
}
}
stage ('Build') {
steps {
echo "During Build result: ${currentBuild.result}"
echo "During Build currentResult: ${currentBuild.currentResult}"
sh 'exit 1'
}
post {
always {
echo "Post-Build result: ${currentBuild.result}"
echo "Post-Build currentResult: ${currentBuild.currentResult}"
}
}
}
}
post {
always {
echo "Pipeline result: ${currentBuild.result}"
echo "Pipeline currentResult: ${currentBuild.currentResult}"
}
}
}

Output

Init result: null
Init currentResult: SUCCESS
Post-Init result: null
Post-Init currentResult: SUCCESS
During Build result: null
During Build currentResult: SUCCESS
[test-pipeline] Running shell script
+ exit 1
Post-Build result: FAILURE
Post-Build currentResult: FAILURE
Pipeline result: FAILURE
Pipeline currentResult: FAILURE
ERROR: script returned exit code 1
Finished: FAILURE

Scripted Pipeline

Here is a test code from cloudbees support case

Example that forces a failure then prints result:

node {
try {
// do something that fails
sh "exit 1"
currentBuild.result = 'SUCCESS'
} catch (Exception err) {
currentBuild.result = 'FAILURE'
}
echo "RESULT: ${currentBuild.result}"
}

Output:

Started by user anonymous
[Pipeline] Allocate node : Start
Running on master in /tmp/example/workspace/test
[Pipeline] node {
[Pipeline] sh
[test] Running shell script
+ exit 1
[Pipeline] echo
RESULT: FAILURE
[Pipeline] } //node
[Pipeline] Allocate node : End
[Pipeline] End of Pipeline
Finished: FAILURE

Example that doesn’t fail then prints result:

node {
try {
// do something that does not fail
echo "Im not going to fail"
currentBuild.result = 'SUCCESS'
} catch (Exception err) {
currentBuild.result = 'FAILURE'
}
echo "RESULT: ${currentBuild.result}"
}

Output:

Started by user anonymous
[Pipeline] Allocate node : Start
Running on master in /tmp/example/workspace/test
[Pipeline] node {
[Pipeline] echo
Im not going to fail
[Pipeline] echo
RESULT: SUCCESS
[Pipeline] } //node
[Pipeline] Allocate node : End
[Pipeline] End of Pipeline
Finished: SUCCESS

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

Reject Force Push

This is the first setting I highly recommend you/your team to open it. if not, when someone using the git push -f command to the git repository, you may lost commits if his local code is old then remote repository.

you have to recover the lost commits manually, I have heard 3 times around me. so enable the hook: Reject Force Push ASAP!

Set Branch Prevent deletion

If some branch is very important, you don’t want to lost it, set Branch Prevent deletion ASAP!

Set tags for each hotfix/GA releases

For each hotfix/GA release, highly recommend create tags after release.

Merge Check

Pull Request is a very good workflow process for each team. in case of commits were merged directly without review, we enable Minimum approvals (1).

So, every branch wants to be merged to the main branch MUST add a reviewer (not allow yourself) and the review must click the Approve button otherwise the merge button is disabled.

Yet Another Commit Checker

This is a very powerful feature. it helps to standardize commit messages and create a branch.

More details about this tool you can refer to this introduction

I have a Chinese article to describe how to use Yet Another Commit Checker implement. if you interest it, you can see the post here

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.

Inbound port 22

There’s a lot of articles will tell you how to open ports on Windows. see this one

https://www.windowscentral.com/how-open-port-windows-firewall

But still not works for me

My problem is when I inbound port 22, execute the above command still can’t see the port 22 is listening.

So I install the Win32-OpenSSH, this will lanch two services, then port 22 is listening.

Here are the wiki page about download and installation https://github.com/PowerShell/Win32-OpenSSH/wiki/Install-Win32-OpenSSH

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

Just in my case, I cloned a git repo exists problems on the Windows platform with some folder, in order to work on the Windows platform we get a work around solution as following:

Case 1: when you not already cloned a repository

mkdir git-src
cd git-src
git init
git config core.sparseCheckout true
echo "/assets/" >> .git/info/sparse-checkout
git remote add origin git@github.com:shenxianpeng/shenxianpeng.git
git fetch
git checkout master

Case 2: when you already cloned a repository

cd git-src
git config core.sparseCheckout true
echo "/assets/" >> .git/info/sparse-checkout
rm -rf <other-file/folder-you-dont-need>
git checkout

Disable git sparse-checkout

git config core.sparseCheckout false
git read-tree --empty
git reset --hard

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

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

原因是 code sign 默认的时间戳服务器无法访问了。

在 Stack overflow 这个 post 里上面有人给出了答案,是来自于 Verisign Support 的回复:

他们的身份验证服务已出售给赛门铁克(Symante),现在的服务商是 Digicert。该服务器已弃用了。

他们建议联系 Digicert 或在网络上找免费的 timestamp servers

以上是别人的回复,我在网络上没有找到一个官方回复,因此打算发邮件正式确认一下,发完不一会就得到了回复:

和上面的回复类似:几年前,Verisign 的身份验证和证书业务被出售给赛门铁克(Symantec),目前已过渡到Digicert。您将需要与当前供应商合作以获得支持或更新的时间戳URL。请访问 http://www.digicert.com 了解更多信息。

好了,这下实锤了,放心大胆的开始动手修改到新的时间戳了。

我找到了 Digicert 的时间戳服务器是 http://timestamp.digicert.com。更换到新的时间戳服务器后,数字签名恢复正常。

除了上面 Digicert 那个网址,还有如下网址可以作为替换:

但我都没有选用,我还是选择了官方的时间戳服务,留作备用吧。一旦又抽风 “官方” 哪天又被卖了呢?

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 是非比寻常的一年,对于每个人不论是生活还是工作,都因为新冠病毒被影响着。

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

回顾 2020

关于工作

对于 2020 年,我一直努力想要去做到最好,年尾回头看看,还算满意吧。

在年底写下工作工作总结的时候,发现自己这一年确实做了很多工作。除了做好构建和发布的本职工作之外,做了很多改进流程和提高效率工作,捡一些重要的来说:

作为 DevOps/软件工程师还需要有良好的表达能力,否则你做的东西再好,但无法很清晰的跟同事及领导分享出来也是事倍功半。尤其像我在外企,还需要用英文去做分享。

另外,我想通过坚持阅读和实践,让自己在技术上有比较大的提高。有一段时间我坚持的比较好,完成了好几本技术书籍的阅读,但好的习惯就怕被打破,一旦被打破就很难坚持了。之前一般都是中午午休的时间会阅读半个小时以上的技术书,后来常常因为中午要去新房盯装修,加上每天要做的事情很多(工作、学英语、阅读、装修)的时候,很多时候往往是事情做了,但效果并没有达到预期。

分享上

2020 年博客上一共更新了 41 篇文章,在公众号『DevOps攻城狮』上面一共更新了 26 篇文章。

这个数量和质量跟一些其他的技术公众号真的没法比,但对于我个人:一个不以写作为生、只写原创文章的攻城狮,只要能不间断的输出,达到这个数量我已经比较满意了。

而且在年初有一个清华大学的编辑老师找到我,想让我出一本书,这真是让我受宠若惊。我知道自己几斤几两,但能够受到邀请,并寄给我合同,就已经是对我写作的最大鼓励了。

当时打算写一本《Jenkins 2实践指南》,如果签了合同,我就需要投入至少一年的业余时间死磕自己才有可能完成。

最后考虑到自己当时的状况:优先做好工作;其次还有比Jenkins更重要的技术我需要去学习;另外周末要装修等一系列的事情…我最终放弃了这个机会。

我知道自己还处在输入比输出更加迫切的阶段,我想只要心中的小火苗还在,但行好事,莫问前程。

工作 flag

对于个人年终总结,不仅可以回顾自己过去的一年做了哪些,哪些没做好,有什么可以改进的,还能通过回顾和总结为新年立一个 flag。

2021 我希望自己能更加专注,利用好时间,更加有效的工作和学习。

除了做好工作,最重要的是提高英语口语深入学习技术,其次才是输出。

光有 flag 不行,还要落到具体的行动上。

提高英语口语

🚩 2021 继续坚持至少每天半小时口语打卡练习;另外每周一次 50 分钟的与外教的口语交流。希望能够脱稿进行英文分享,同时能通过英语托业考试。

深入学习技术

我现在学习技术的方式有这么几种:直接去官网阅读英文文档;如果读完还是没搞明白,就去 Linkedin Learning 或是 Udemy 上去搜相应的技术,不但学习了技术,同时也练习了英文听力;如果需要深入系统的去学习一门编程语言以及一些底层技术,会去读一些经典技术书籍。

🚩 2021 年坚持每天也至少半个小时来阅读技术,一个月至少读完一本技术书籍并且有输出。

生活 flag

生活上希望家人和朋友都能身体健康,生活幸福。

🚩 财务稳定,最好能增长;自己能够健康生活,减重 10 斤。

最后,不时的来回顾自己 2021 年的 flag,时不时的看看自己是否偏离了最初的方向。2021 我来了!

过去的年终总结

2019 年终总结
2018 从测试到开发的五个月

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

JaCoCo 实现原理 (JaCoCo Implementation Design)

想要对 Java 项目进行代码覆盖率的测试,很容易就找到 JaCoCo 这个开源代码覆盖率分析工具是众多工具中最后欢迎的哪一个。

本篇仅仅是在学习 JaCoCo 时对其实现设计文档 https://www.jacoco.org/jacoco/trunk/doc/implementation.html 的粗略翻译。

实现设计(Implementation Design)

这是实现设计决策的一个无序列表,每个主题都试图遵循这样的结构:

  • 问题陈述
  • 建议的解决方案
  • 选择和讨论

覆盖率分析机制(Coverage Analysis Mechanism)

覆盖率信息必须在运行时收集。为此,JaCoCo 创建原始类定义的插装版本,插装过程发生在加载类期间使用一个叫做 Java agents 动态地完成。

有几种收集覆盖率信息的不同方法。每种方法都有不同的实现技术。下面的图表给出了 JaCoCo 使用的技术的概述:

实现

字节码插装非常快,可以用纯 Java 实现,并且可以与每个 Java VM 一起工作。可以将带有 Java 代理钩子的动态插装添加到 JVM 中,而无需对目标应用程序进行任何修改。

Java 代理钩子至少需要 1.5 个 JVMs。用调试信息(行号)编译的类文件允许突出显示源代码。不幸的是,一些 Java 语言结构被编译成字节代码,从而产生意外的突出显示结果,特别是在使用隐式生成的代码时(如缺省构造函数或 finally 语句的控制结构)。

Read More

Ansible 实践

最近在思考如何将团队里的所有的虚拟机都很好的管理并监控起来,但是由于我们的虚拟机的操作系统繁多,包括 Windows, Linux, AIX, HP-UX, Solaris SPARC 和 Solaris x86. 到底选择哪种方式来管理比较好呢?这需要结合具体场景来考虑。

Ansible 和其他工具的对比

这里有一个关于 Chef,Puppet,Ansible 和 Saltstack 的对比文章

https://www.edureka.co/blog/chef-vs-puppet-vs-ansible-vs-saltstack/

选择合适的工具

仅管理 Windows 和 Linux

如果你的虚拟机没有这么多平台,只是 Windows 和 Linux,假如你已经有了 VMware vSphere 来管理了,那么可以通过 VMware vSphere API 来查看这些机器的状态。

这里是 VMware 官方的 API Library 供使用:

管理多个操作系统

如果你和我的情况一下,想监控很多个操作操作系统,那么就只能通过 ssh 来登录到每一台机器上去查看,比如执行 uptime 等命令。可以写 shell 脚本来完成这些登录、检测等操作。

另外就是使用 Ansible 的 Playbook。Playbook 里描述了你要做的操作,这是一个权衡,学习 Ansible 的 Playbook 需要花些时间的。

如果想了解下 Ansible 那么可以试试 Ansible Playbook。以下是我使用 Ansible 做了一些练习。

Playbook结构

+- vars
| +- vars.yml
| +- ...
+- hosts # save all hosts you want to monitor
+- run.yml # ansible executable file

Playbook具体代码

Read More