Comments (1)
Jenkins 初探
[TOC]
安装
本次通过 docker 安装。
1. 首先安装 docker
2. docker pull jenkinsci/blueocean
运行 Jenkins
docker run -p 8080:8080 -u root -v /var/run/docker.sock:/var/run/docker.sock -v jenkins-data:/var/jenkins_home -v "$HOME":/home jenkinsci/blueocean
这条命令需要注意的是
-p 8080:8080 #代表将 host 的 8080 端口影射到 container 的8080 端口
-v jenkins-data:/var/jenkins_home #
-v /var/run/docker.sock:/var/run/docker.sock #绑定 host 机器的 /var/run/docker.sock 到 container 的 /var/run/docker.sock
用浏览器打开 loacalhost:8080 再将 terminal 中的 password 粘贴进 jenkins 初始界面,我们就能到创建用户和安装插件的基面了。
这里有个小 tips: 如果创建用户名的时候没有输入密码,则会无法登录 jenkins, 这个时候需要我们进入 docker container 禁用用户登录。步骤如下:
docker ps #记下 container id.
docker exec -it container_id /bin/bash #进入 container 的 bash
vi /var/jenkins_home/config.xml
- set userSecurity to false: false
- delete
and - 重启 docker
这个时候 jenkins 默认就不需要用户登录了(生产环境请不要这么玩)
创建任务
将官方提供的 repo clone 下来
git clone https://github.com/gilesgas/simple-node-js-react-npm-app-original.git
- 回到 localhost:8080
- 新建一个 pipeline
- 在下一个页面选择 pipeline tab
- defination 下拉选择 pipeline script from SCM(Souce Control Management)
- 在 SCM field 选择 git
- Repository URL 输入 clone 的 repo 的路径
- 如果 jenkins 是放在 docker 中则需要将 host 的对应目录挂载到 docker 的 volume 上
- 保存
Build
回到 clone 的 repo,在根目录下创建 Jenkinsfile
pipeline {
agent {
docker {
image 'node:6-alpine'
args '-p 3000:3000'
}
}
stages {
stage('Build') {
steps {
sh 'npm install'
}
}
}
}
这个文件的含义是
- docker pull node:6-alpline 的 image,如果有则直接启动。。
- args 代表说 node container 可以通过 3000 端口访问到。
- Jenkins 和 node 是单独的 container 运行在 docker 当中
- 这个 node container 成为了 Jenkins builde 当前 project 的一个 agent。并且持续时间很短,只会持续到当前任务运行结束。
- 定义了一个叫
Build
的 state,该 state 会出现在 Jenkins UI 上。
将提交 push,jenkins 会自动 trigger 一个 task。
Test
等 build 跑完之后我们再修改 Jenkinsfile
为:
pipeline {
agent {
docker {
image 'node:6-alpine'
args '-p 3000:3000'
}
}
+ environment {
+ CI = 'true'
+ }
stages {
stage('Build') {
steps {
sh 'npm install'
}
}
+ stage('Test') {
+ steps {
+ sh './jenkins/scripts/test.sh'
+ }
}
}
}
第一个新增的 section 代表将 CI 环境变量设置为 true,这个环境变量在整个 pipeline 中都可见。
后面的任务就很简单了,我们又新增了一个 Test
任务在 pipeline 中。
这个时候安装 cross-env
的依赖再修改 package.json
yarn add cross-env --dev
- "test": "react-scripts test --env=jsdom",
+ "test": "cross-env CI=true react-scripts test --env=jsdom",
把代码推到远程,这个时候再去 Jenkins 上点击 Build now
我们的 Test 任务就出现在 pipeline 中啦。(请忽略挂掉的那次)
Delivery
终于来到最后部署步骤,仍然修改 Jenkinsfile 为:
pipeline {
agent {
docker {
image 'node:6-alpine'
args '-p 3000:3000'
}
}
environment {
CI = 'true'
}
stages {
stage('Build') {
steps {
sh 'npm install'
}
}
stage('Test') {
steps {
sh './jenkins/scripts/test.sh'
}
}
+ stage('Deliver') {
+ steps {
+ sh './jenkins/scripts/deliver.sh'
+ input message: 'Finished using the web site? (Click "Proceed" to continue)'
+ sh './jenkins/scripts/kill.sh'
+ }
+ }
}
}
重复上面的动作,这里需要注意的是 input 那行代码,此代码会在执行完之前的步骤后出现 Process
和 Abort
的选项,提示信息如 message 所示。
其实此示例最后也没有部署,只是用 npm start
演示下 build
之后的效果,如果点击 Process
则会执行之后的 kill 脚本,否则则直接终止 pipeline 并将当前任务状态设置为 abort
。
知识点
- Jenkinsfile 中 enviroment 作用 #43
- 在所有 job 中都能在 script 中获取到,并且可以通过声明在 stage 中从而隔离环境变量
- #44 set -x
npm start &
sleep 1
echo $! > .pidfile
set +x
参考资料
from mrbone.github.io.
Related Issues (20)
- css flexbox HOT 1
- CSS
- 重装 mac 的基本配置 HOT 1
- git submodules HOT 1
- git lab flow
- koa trial
- yield * 的作用
- diigo get start HOT 1
- ES6 module export directly
- ES6 module export directly
- ES6 module export directly
- Instapaper gettting start HOT 1
- LinkedIn update
- Finacial
- MLF HOT 1
- 逆回购
- 爱尔兰不动产
- proxyquire with es6 module
- release logs
- release logs HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mrbone.github.io.