说明:
由于项目需要通过环境变量动态更改nginx的配置所以引入default.conf.template,default.conf.template是带有环境变量的nginx配置文件,docker启动后会读取环境变量替换掉default.conf.template中使用的环境变量,生default.conf后再启动docker
default.conf.template
server {
listen 80;
server_name localhost;
#charset koi8-r;
access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#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;
#}
location /api {
root /usr/share/nginx/html;
proxy_pass ${PROXY_URL};
rewrite ^/api/(.*)$ /$1 break;
}
}
Dockerfile
FROM node:10.19.0 as build
WORKDIR /merry-web
COPY package.json ./
RUN npm install
COPY . .
RUN npm install node-sass
RUN npm run build
FROM nginx
COPY --from=build /merry-web/dist /usr/share/nginx/html/
COPY nginx/templates /etc/nginx/templates
// 可以加载环境变量配置不同的代理url
ENV PROXY_URL='http:localhost:8080'
EXPOSE 80
Jenkinsfile
pipeline{
agent any
stages {
stage('Build') {
steps{
withCredentials([usernamePassword(credentialsId: 'QACICD', passwordVariable: 'QACICDPassword', usernameVariable: 'QACICDUser')]) {
# app:0.1.0.123.1qweasde
sh 'docker build -t ${project_name}:${ver_release} .'
}
}
}
stage('push') {
parallel{
stage("push harbor"){
steps{
withCredentials([usernamePassword(credentialsId: 'QACICD', passwordVariable: 'QACICDPassword', usernameVariable: 'QACICDUser')]) {
sh "docker login -u ${QACICDUser} -p ${QACICDPassword} ${imgharb}"
sh "docker tag ${local_image_name} ${harb_img_name}"
sh "docker tag ${local_image_name} ${harb_img_name_latest}"
sh "docker push ${harb_img_name} && docker push ${harb_img_name_latest}"
}
}
}
stage("push register"){
steps{
withCredentials([usernamePassword(credentialsId: 'QACICD', passwordVariable: 'QACICDPassword', usernameVariable: 'QACICDUser')]) {
sh "docker login -u ${QACICDUser} -p ${QACICDPassword} ${imghub}"
sh "docker tag ${local_image_name} ${image_name}"
sh "docker tag ${local_image_name} ${image_name_latest}"
sh "docker push ${image_name} && docker push ${image_name_latest}"
}
}
}
}
}
}
environment {
version = sh(returnStdout: true, script: """cat package.json |grep version|awk -F '"' '{print \$4}'""").trim()
com_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
com_count = sh(returnStdout: true, script: 'git rev-list --count HEAD').trim()
ver_release = "${version}.${com_count}.${com_tag}"
project_name = "app"
# 线上镜像库url(根据需求自己填)
imghub = "url"
imgharb ="url"
GIT_NAME= sh(returnStdout: true, script: "git --no-pager show -s --format='%an' $GIT_COMMIT").trim()
GIT_COMMITER_EMAIL= sh(returnStdout: true, script: "git --no-pager show -s --format='%ae' $GIT_COMMIT").trim()
local_image_name = "${project_name}:${ver_release}"
image_name = "${imghub}/${project_name}:${ver_release}"
image_name_latest = "${imghub}/${project_name}:latest"
image_name_version = "${imghub}/${project_name}:${version}"
harb_img_name = "${imgharb}/${project_name}:${ver_release}"
harb_img_name_latest = "${imgharb}/${project_name}:latest"
harb_img_name_version = "${imgharb}/${project_name}:${version}"
}
}
docker运行
sudo docker run container -d -p 80:80 -e PROXY_URL=url image_name