Jenkins使用piplien NodeJs编译vue项目
安装
- 在
Dashbord>Manage Jenkins>Plugins
页面点击Available plugins
搜索NodeJS
点击安装并重启。 - 在
Dashbord>Manage Jenkins>Tools
页面最底部的NodeJS一栏点击Add NodeJS
,设置名字nodejs
,然后选择版本。
配置
Dockerfile
1
2
3
4
5FROM nginx:alpine
RUN rm /etc/nginx/conf.d/default.conf
ADD ./nginx.conf /etc/nginx/conf.d/nginx.conf
COPY ./dist /usr/share/nginx/html/
CMD ["nginx","-g","daemon off;"]nginx的配置
nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}Jenkinsfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
pipeline {
agent any
parameters {
choice(name: 'project_choice',
choices: 'demo-pc',
description: '你要编译构建那个项目?')
}
stages {
stage('npm build') {
tools {
nodejs "nodejs" //这里的node要和nodejs工具的配置里的name要一致
}
steps {
dir("${params.project_choice}") { //切换目录
echo "开始构建${params.project_choice}项目"
sh "npm install --registry=https://registry.npmmirror.com"
sh "npm run build:stage" //编译打包
}
}
}
stage('docker build'){
steps{
dir("${params.project_choice}") { //切换目录
sh "docker build --no-cache -t registry.cn-qingdao.aliyuncs.com/xxx/demo-pc:${env.BUILD_NUMBER} ."
echo "构建镜像" + "ag/${params.project_choice}".toLowerCase() + "完成"
}
}
}
stage('docker push'){
steps{
sh "docker login --username=zhangsan registry.cn-qingdao.aliyuncs.com -p mima"
sh "docker push registry.cn-qingdao.aliyuncs.com/xxx/demo-pc:${env.BUILD_NUMBER}"
echo "推送镜像" + "ag/${params.project_choice}".toLowerCase() + "完成"
}
}
stage('docker run'){
steps{
sh "docker stop demo-pc||true"
sh "docker rm demo-pc||true"
sh "docker run -d -p 8911:80 --name demo-pc registry.cn-qingdao.aliyuncs.com/xxx/demo-pc:${env.BUILD_NUMBER}"
echo "运行镜像" + "ag/${params.project_choice}".toLowerCase() + "完成"
}
}
}
}
运行
- 创建流水线
Dashboard>New Item>Pipeline
输入流水线名字,点击ok。 - 在
Pipeline
一栏的Definition选择Pipeline script from SCM
。 - 在
SCM
一栏选择Git
,在Repository URL
中输入git项目地址。 - 设置项目中
Jenkinsfile
文件的路径,点击保存。 - 点击
build
运行流水线即可。
java项目同理
Dockerfile
1
2
3
4FROM exxk/java:8-alpine-cst
ADD ./target/demo-admin.jar /app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]Jenkinsfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
pipeline {
agent any
parameters {
choice(name: 'project_choice',
choices: 'demo-admin' +
'\ndemo-framework' +
'\ndemo-system' +
'\ndemo-common',
description: '你要编译构建那个项目?')
}
stages {
stage('maven build') {
tools {
maven "maven" //这里的maven要和maven工具的配置里的name要一致
}
steps {
echo "开始构建${params.project_choice}项目"
sh "mvn package -pl ${params.project_choice} -am" //构建
}
}
stage('docker build'){
steps{
dir("${params.project_choice}") { //切换目录
sh "docker build --no-cache -t registry.cn-qingdao.aliyuncs.com/xxx/demo-admin:${env.BUILD_NUMBER} ."
echo "构建镜像" + "ag/${params.project_choice}".toLowerCase() + "完成"
}
}
}
stage('docker push'){
steps{
sh "docker login --username=zhangsan registry.cn-qingdao.aliyuncs.com -p mima"
sh "docker push registry.cn-qingdao.aliyuncs.com/xxx/demo-admin:${env.BUILD_NUMBER}"
echo "推送镜像" + "ag/${params.project_choice}".toLowerCase() + "完成"
}
}
stage('docker run'){
steps{
sh "docker stop demo-admin||true"
sh "docker rm demo-admin||true"
sh "docker run -d -p 8910:8080 --name demo-admin registry.cn-qingdao.aliyuncs.com/xxx/demo-admin:${env.BUILD_NUMBER}"
echo "运行镜像" + "ag/${params.project_choice}".toLowerCase() + "完成"
}
}
}
}