Now we will build an automate flow from code compiling to product delivery.
The essential tools using here are:

  1. GitHub (or other VCS)
  2. Jenkins
  3. Docker
  4. A web app for demo (use Ruby here)

1. Project

这个项目基本只有三个文件,分别是 Dockerfile 用于打包镜像,Jenkinsfile 用于Jenkins 执行自动测试、编译等构建工作,还有 app.rb 是一个 web 项目,用来模拟真实环境中的 web 开发项目,下面我将介绍具体步骤。

2. Create GitHub webhooks

我们首先需要创建这个 Git 项目,并 push 到 GitHub 上,在项目的设置界面添加 Webhooks

通过 Webhook 可以在你向 github 仓库推送代码时,通过 HTTP 请求向任意的 web server 发送通知信息,在这里我们用它来通知 Jenkins。

这里的 Payload URL 就是接受请求的 Jenkins 地址,但这个地址稍微有些复杂,你会在下一节中看到这个 URL 的具体格式。

3. Config the jenkins

我们使用 Docker 容器技术来搭建一个 Jenkins 服务,通过 docker 安装 Jenkins 非常容易,如果你使用 mac 或者 linux,只需要一条命令:

docker run \
-u root \
--rm \
-d \
-p 8080:8080 \
-p 50000:50000 \
-v jenkins-data:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
jenkinsci/blueocean

如果你在安装 Jenkins 遇到了问题,你可以查看 Jenkins 官方文档 https://jenkins.io/doc/book/installing/

在浏览器中打开 http://localhost:8080/  ,在完成初始化安装后可以看到主界面如下图:

通过左边的 New Item 按钮创建你的第一个 Jenkins 项目吧!
在配置页面有很多不同类型可供选择,在这里我们选择 Pipline 按下面的 OK 进入下一步。

在项目的配置页面,我们首先设定通过外部脚本(trigger builds remotely)触发 Jenkins 的 pipline 脚本。
Authentication token 可以随便填入一串随机字符串,输入框下面的注释就是上面 GitHub webhook 需要用到的 URL。

具体来说, GitHub webhook 中的 payload URL 应该是这样的格式:
http://<jenkins_username>:<jenkins_api_token>@<jenkins_server:port>/job/<jenkins_item_name>/build?token=<authentication_token>

<jenkins_api_token> 需要你在 user 的 configure 界面自行创建,如下图所示:

Jenkins pipline 是一种领域特定语言, 我们用它来自动创建 docker 镜像,pipline 也可以被放进项目中,通过 Git 进行版本控制,所以在这里我们从 GitHub 上拉取 pipline 脚本。这里的 Script Path 就是 pipline 脚本的名字,如果路径错误 Jenkins 是无法找到正确的脚本的。

我使用的 pipline 脚本比较简单,只是执行一些 shell 语句,
其中区分成 build 和 post 两个阶段:

pipeline {
agent any
stages {
stage('build') {
steps {
sh 'rm -rf just_test/'
sh 'git clone --depth=1 https://github.com/xxx/just_test'
sh 'cd just_test'
sh "docker build -t just_test/v${env.BUILD_ID} . "
}
}
stage('post') {
steps {
sh 'docker stop $(docker ps -a -q --filter="name=justtest")'
sh "docker run -p 4567:4567 --rm --name=justtest -d just_test/v${env.BUILD_ID}"
}
}
}
}

简单解释一下这个 pipline 脚本,在 Jenkins 创建的工作目录中,克隆 github 项目,进入项目目录,调用 docker build 打包新的 docker 镜像,docker build 会调用项目中的 Dockerfile 来创建 docker 镜像。然后是停止旧的 App 容器,并将刚才打包好的镜像启动。

4. Dockerfile

使用 Dockerfile 来打包镜像,代码如下:

# Use an official Ruby runtime as a parent image
FROM ruby # Set the working directory to /app
WORKDIR /usr/src/app # 将当前目录拷贝至 docker container 中的当前目录
COPY . . # 运行 sh 命令(安装 ruby 软件包)
RUN gem install sinatra EXPOSE 4567 # 启动 ruby 服务器
CMD ["ruby", "app.rb"]

Jenkins automate workflow的更多相关文章

  1. Using git-flow to automate your git branching workflow

    Using git-flow to automate your git branching workflow Vincent Driessen’s branching model is a git b ...

  2. [tmux] Automate your workflow using tmux scripts

    Do you have a standard workflow that involves setting up a specific tmux layout, or running certain ...

  3. 构建Docker+Jenkins持续集成环境

    docker和Jenkins不是什么新东西了,两者结合也不是什么稀奇的事情,也已经有很多Jenkins和docker相结合的文章,此文仅为自己的一点心得实践,如有不对的地方,欢迎大家纠正. 先贴上大致 ...

  4. Jenkins iOS – Git, xcodebuild, TestFlight

    Introduction with Jenkins iOS If you are new to continuous integration for mobile platforms then you ...

  5. Front end workflow

    标签:请叫我红领巾 记一哈记一哈 ^_^ 推荐个不错的群 自己刚开始折腾的时候也是无处下手,渺茫啊.然而我是有一个很好的前端群(真的很棒,欢迎加入:[375042952]),关注群里的每一次讨论,每一 ...

  6. 解决jenkins构建job报错“NoClassDefFoundError” in jenkins/scm/RunWithSCM问题

    现象 使用Jenkins 2.8,当我运行一个简单的Jenkins工作时,构建一个job获取源代码,出现下面的错误 FATAL: jenkins/scm/RunWithSCM java.lang.No ...

  7. Jenkins Pipeline高级用法-ShareLibrary

    1.Github配置 1.1 上传jenkinsfile到github https://github.com/zeyangli/ShareLibrary-jenkins.git 2.Jenkins配置 ...

  8. [EXP]Jenkins 2.150.2 - Remote Command Execution (Metasploit)

    ## # This module requires Metasploit: https://metasploit.com/download # Current source: https://gith ...

  9. Jenkins之使用Pyinstaller构建Python应用程序

    目录 1. 极简概述 2. Jenkins配置 2.1 安装JDK 2.2 安装Jenkins 3. 安装Docker 4. 使用PyInstaller构建Python应用程序 4.1 Fork 一个 ...

随机推荐

  1. TZOJ3114: {A}∩{B}

    #include<stdio.h> int main() { ],b[],m,i,j,c; scanf("%d",&t); while(t--) { c=; s ...

  2. ArcGIS JS之 applyEdits之后要素符号更新

    ArcGIS JS版本 ArcGIS JS 4.11 最近做一个地图服务,通过FeatureLayer.applyEdits()方法,更新唯一值的渲染字段,实现地图渲染根据用户的配置实时更新. 由于A ...

  3. 游记-NOI2019

    Day -18 被各路julao们轮番吊打-- Day -12 鸽子F发布了笔试题库,然而并没有 "MLE全场记零分" 的操作 Day -8 广二体育馆机器装配完毕,误闯开幕式表演 ...

  4. 并查集问题hdu 1232

    Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道 ...

  5. VS.NET(C#)--1.4项目与解决方案

    项目与解决方案 项目 除创建网站,VS2005可创建项目.然后把项目放入解决方案中.VS2005可编译很多类型项目,分别是: 1.Windows应用程序 --在用戶计算机上运行的客户端应用程序,可显示 ...

  6. C#从零单排上王者系列---数据类型

    从零单排系列简介 突然发现自己的基础不是很牢固,就买了一个<C#7.0的本质论>.本系列博客就是以此书为本,记录自己的学习心得,如果你的基础也不牢固,不如跟上博主一起学习成长呀! 本篇博客 ...

  7. Django配置websocket请求接口

    1.settings.py INSTALLED_APPS = [ '...', 'channels', '...', ] ASGI_APPLICATION = 'server.routing.appl ...

  8. 大专生自学web前端到找到工作的经验

    先做个自我介绍,我13年考上一所很烂专科民办的学校,学的是生物专业,具体的学校名称我就不说出来献丑了.13年我就辍学了,我在那样的学校,一年学费要1万多,但是根本没有人学习,我实在看不到希望,我就退学 ...

  9. SAP Leonardo图片处理相关的机器学习服务在SAP智能服务场景中的应用

    本文作为Jerry最近正在做的一个项目的工作思路的梳理. 我们假设这样一个服务场景,技师上门维修某设备,发现设备上某零件损坏了,假设这位技师由于种种原因,没能根据自己的经验识别出这个零件的型号.此时技 ...

  10. 用python代码编写象棋界面,棋盘覆盖问题

    编写象棋界面 import turtle t=turtle.Pen() t.speed(100) def angle(x,y): t.penup() t.goto(x+3,y+3) t.pendown ...