docker中安装jenkins请参考文章《Jenkins-在docker中的持续集成》。
一、安装node环境:
官网下载node_v14,建议使用迅雷下载,比较快。
地址:https://nodejs.org/dist/latest/node-v14.8.0-linux-x64.tar.gz
下载后上传到服务器/root/download/下,并解压:
$ tar -zxvf node-v14.8.0-linux-x64.tar.gz -C ./
重命名:
$ mv node-v14.8.0-linux-x64 node_v14
配置环境变量:
$ vim /etc/profile
增加:
export PATH=${PATH}:/root/download/node_v14/bin
使环境变量立即生效:
$ source /etc/profile
使用命令查看版本:
$ node -v
修改jenkins的docker-compose.yml
增加挂载目录,让jenkins中可以使用node命令:
- "/root/download:/var/download"
二、jenkins中node配置
在jenkins插件中搜索NodeJsPlugin并安装:
先点击Available搜索可用的插件,再点击安装,Installed表示已经安装过得插件。
安装后需要重启jenkins。
在Global Tool Configuration中配置node
此路径是jenkins容器中的路径,我们在启动jenkins时,已经将外部/root/download挂载到了容器内部/var/download。
三、运行nginx
拉取镜像:
$ docker pull nginx:1.18.0
编写docker-compose.yml文件,将docker-compose.yml文件放到/root/download/docker-nginx中:
version: '3'
services:
jeeplus_nginx:
container_name: jeeplus_nginx
hostname: 192.168.0.150
image: nginx:1.18.0
restart: always
privileged: true
environment:
TZ: 'Asia/Shanghai'
volumes:
- "/root/download/docker-nginx/nginx.conf:/etc/nginx/nginx.conf"
- "/root/download/docker-nginx/html:/etc/nginx/html"
- "/root/download/docker-nginx/log:/var/log/nginx"
ports:
- "5000:80"
容器启动时会挂载宿主机指定目录到容器内部。
将nginx.conf配置文件放到/root/download/docker-nginx/中。
nginx.conf:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /org/ {
root /etc/nginx/html;
index index.html index.htm;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.html?s=$1 last;
break;
}
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
运行nginx:
$ cd /root/download/docker-nginx
$ docker-compose up -d
四、新建项目
主页点击New Item:
输入项目名称,选择Freestyle project:
选择代码来源,我这里是git,指定分支为development分支:
node环境变量配置:
在build时就可以选择shell命令,就可以使用node来打包你的前端项目然后将dist目录下的内容移动到nginx的html中:
参考shell命令:
此命令功能为删除nginx的html下的旧文件,然后将新构建的html移动进去。
SERVER_NAME=pension_org_ui
JENKINS_HOME=/var/jenkins_home
PENSION_HOME=$JENKINS_HOME/workspace/$SERVER_NAME
HTML_BASE=/var/download/docker-nginx/html
cd $PENSION_HOME
npm install -g cnpm --registry=https://registry.npm.taobao.org
echo "执行install..."
cnpm install
echo "执行build..."
cnpm run build
echo "删除旧文件..."
rm -rf $HTML_BASE/org
echo "创建mzj文件夹..."
mkdir -p $HTML_BASE/org
echo "移动文件..."
mv $PENSION_HOME/dist/* $HTML_BASE/org
echo "部署完成!"
SERVER_NAME即为你创建的jenkins item的名称。
五、部署
项目中点击build now:
点击构建记录:
查看控制台输出:
构建成功: