Skip to content

Instantly share code, notes, and snippets.

@ThaddeusJiang
Created January 17, 2019 08:47
Show Gist options
  • Save ThaddeusJiang/c2c4f5c02965c84bf7bae14a1bc7a95e to your computer and use it in GitHub Desktop.
Save ThaddeusJiang/c2c4f5c02965c84bf7bae14a1bc7a95e to your computer and use it in GitHub Desktop.
Jenkins 总结
title categories tags date
使用 Jenkins 持续集成
技术
Jenkins
CI
2018-05-31 11:15:56 -0700

背景

私有项目(INSUITE CMS),使用 GitHub enterprise。

CI 选择

首先持续集成工具(CI),不得不选择 JenkinsTravis CI 集成私有项目需要付费。

但是 Jenkins 画面又太丑了,实在无法忍受。折中方法,使用 Blue Ocean Plugin,这是一个稍微现代化一点的 UI 界面。

我使用 jenkinsci/blueocean 的 docker 镜像运行 jenkinsci 优点,省去了安装 JRE 的麻烦。

大概流程

  1. aws 建一个 centos7
  2. 安装 docker
  3. 启动 Jenkins
  4. Jenkins 安装 GitHub plugin(blueocean 默认安装)
  5. 为你的项目添加 Jenkinsfile
  6. Jenkins 界面中设置 GitHub enterprise repository(这里需要 Personal access tokens)
  7. GitHub enterprise repository 设置 webhook,测试一下 webhook
  8. git push 一条 commit,查看 Jenkins 是否工作

安装 docker

aws 创建一个 centos7,并安装 docker。 centos7 安装 docker 方法

为 ec2 设置公网 IP(为了与 GitHub enterprise 互联)。 方法

启动 jenkinsci

方法:

docker run \
  --rm \
  -u root \
  -p 8001:8080 \
  -v jenkins-data:/var/jenkins_home \
  -v /var/run/docker.sock:/var/run/docker.sock \
  --add-host=stainless.dreamarts.co.jp:10.0.128.144 \
  -v "$HOME":/home \
  -d \
  jenkinsci/blueocean

记录: 这里有两个配置是专为 CMS 项目添加的。

  -p 8001:8080 
  // 将 8080 端口指定到 8001 端口,
  // 因为公司 AWS ec2 只开放 8001 端口。  
  
  --add-host=stainless.dreamarts.co.jp:10.0.128.144 
  // 为了让 jenkinsci 能访问 Github,需要添加 GitHub enterprise 的 IP 地址。

Jenkinsfile

写一个最简单的

pipeline {
    agent {
        docker {
            image 'node:8'            
        }
    }
    environment {
        CI = 'true'
    }
    stages {
        stage('Install') {
            steps {
                sh 'npm install'
            }
        }
        stage('Test') {
            steps {
                sh 'npm test'
            }
        }        
    }
}

实际使用的: https://github.com/ThaddeusJiang/parcel-react-starter

遇到的问题

  1. jenkinsci 和 GitHub enterprise 不能互联,因为不在同一个内网。
  2. jenkinsci 后台运行,使用 docker run -d 。
  3. docker 启动时设置 GitHub enterprise 的 IP,使用 docker run --add-host

jenkinsci 需要安装的 Plugins

  1. Notification,用来发送测试结果。
# Jenkins Notification 设计
不使用 plugin, 不写代码, `Ctrl+C & Ctrl+V` 完成 CI 自动通知功能。
## 概要
使用 Jenkins 自带 httpRequest 功能将 CI 运行结果转发出来,
借助 Zapier 将结果发送到指定邮件(or IM)。
## 操作
### 1. 复制👇代码到你的 Jenkinsfile 中
```yml
failure {
// 关键
httpRequest acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON', httpMode: 'POST', requestBody: """{
"contact": {
"title": "${BUILD_TAG}",
"number": "${currentBuild.displayName}",
"status": "${currentBuild.currentResult}",
"url": "${BUILD_URL}"
}
}""", responseHandle: 'NONE', url: "${zapier_webhook}"
}
```
### 2. Jenkins 添加全局变量
zapier_webhook https://zapier_webhook/url
### 3. 配置 Zapier webhook 和 IM app
测试 webhook 和 IM app
### 4. 结合测试
Jenkins + Zapier + IM

记一次 Jenkins+GitHub enterprise CI 搭建

  • 问题一:Jenkins 和 GitHub enterprise不能互联

原因: 我司的 GitHub 和 Jenkins 不在统一内网。 Jenkins 环境使用 aws,由于公司设置,默认只能内网访问,所以 GitHub 无法接入。 其次 Jenkins 环境中没有配置 GitHub IP,所以 Jenkins 也无法发送 response 给 GitHub。

解决: step 1. 修改 aws,允许外网访问。 step 2. 修改 Jenkins 环境(我自己是用 docker 运行)hosts,方法 echo 'github enterprise url' >> /etc/hosts

  • 问题二:Jenkins 自定义端口

我的 Jenkins 使用 docker 启动,需要做端口映射。 Jenkins 默认端口 8080,所以 -p 8001:8080

  • 问题三:Jenkins 执行结果,需要主动发送之 GitHub。 todo

  • 问题四:docker stop 后,使用 docker start 正常运行 Jenkins。 todo docker run 运行Jenkins,需要重新配置 /etc/hosts。 😡


大概流程

  1. aws 建一个 centos7,并且设置允许与 GitHub enterprise 互联。
  2. 安装 docker。
  3. 启动 Jenkins docker run --rm -u root -p 8080:8080 -p 1234:1234 -v jenkins-data:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -v "$HOME":/home -d jenkinsci/blueocean Jenkins 也要允许与 GitHub enterprise 互联。
  4. Jenkins 安装 GitHub plugin(blueocean,默认安装好了)。
  5. Jenkins 界面中设置 GitHub enterprise repository。
  6. GitHub enterprise repository 设置 webhook 和 service(Jenkins GitHub),测试一下 webhook 。
  7. git push 你的修改,查看 Jenkins 是否工作。

定期执行

  1. 需求:周一到周五,8:15到19:15,每2小时执行一次。

  2. 语法: 时间:

# The Minutes within the hour (0–59)
# The hour of the day (0–23)
# The day of the month (1–31)
# The month (1–12)
# The day of the week (0–7) where 0 and 7 are Sunday.

# once every two hours at 45 minutes past the hour starting at 9:45 AM and finishing at 3:45 PM every weekday.
45 9-16/2 * * 1-5

时区:

TZ=Europe/London
# This job needs to be run in the morning, London time
H 8 * * *
  1. 实际使用
TZ=Asia/Tokyo
# 周一到周五,8:15到19:15,每2小时执行一次。
15 H(8-19)/2 * * 1-5

指定 branch

通过画面设置,写正则表达式。

jenkins branch

通知

借助 Zapier 转发,不需要任何 plugin。

Cache 导致硬盘空间不足

问题

Jenkins CI 忘记清空 workspace,导致 Linux 硬盘被写满。

解决方法

找到 Jenkins 的 workspace 将里面的临时文件全部删除,然后在 Jenkinsfile 中加入删除临时文件的步骤。

  1. Jenkins workspace 我的 Jenkins 通过 docker 启动,所以 workspace 也稍有不同。 👇 /var/lib/docker/volumes/jenkins-data/_data/workspace

  2. Jenkinsfile 删除临时文件语法

pipeline {
    agent any
    stages {
        ...
    }
    post {
        always {
            deleteDir()
            cleanWs()
        }
        ...
    }
}

编写 pipeline

在 Jenkins 中 `Pipeline SyntaxP 可以生成 pipeline 语句,非常方便。 位置:左侧菜单

docker and jenkins

#!/usr/bin/env sh

docker run \
  --rm \
  -u root \
  -p 8001:8080 \
  -v jenkins-data:/var/jenkins_home \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v "$HOME":/home \
  --name jenkins_ci \
  -d \
  --add-host=stainless.dreamarts.co.jp:10.0.128.144 \
  --add-host=rh7-apache2-org.m.diol.jp:10.1.27.234 \
  jenkinsci/blueocean
  • -p 8001:8080 da aws 只开放 8001

  • 管理 container docker exec -it jenkins_ci bash

  • 关闭 container docker stop jenkins_ci

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment