在Jenkins 中先創(chuàng)建一個任務(wù)名稱
然后進(jìn)行下一步,放一個項(xiàng)目
填寫一些參數(shù)
參數(shù)1:
參數(shù)2:
參數(shù)3:參數(shù)4:
點(diǎn)擊保存就行了
配置腳本
// git
def git_url = 'http://gitlab.xxxx.git'
def git_auth_id = 'GITEE_RIVERBIED'
// harbor
def harbor_host = '10.0.165.17:5000'
def harbor_project = 'test'
def harbor_crt_id = 'HARBOR_CRT_ID'
// k8s shf
def k8s_crt_id = 'KUBE_CONFIG_FILE_ID'
def k8s_namespace = 'test-web'
// common
def api_name = 'test-web'
def docker_file_path= ''
def docker_image = "${harbor_host}/${harbor_project}/${api_name}:${SERVICE_VERSION}-${ENVIRONMENT.toLowerCase()}"
def service_node_port = ''
def current_timespan = System.currentTimeMillis().toString()
pipeline {
agent any
tools {
nodejs 'nodejs14.15.1'
}
stages {
stage('參數(shù)初始化+代碼拉取') {
steps {
script {
api_name = "test-web"
docker_file_path = "Dockerfile"
docker_image = "${harbor_host}/${harbor_project}/${api_name}-${ENVIRONMENT.toLowerCase()}:${SERVICE_VERSION}_${current_timespan}"
service_node_port = "30992"
}
dir("${ENVIRONMENT.toLowerCase()}") {
// 如果是公開倉庫,可以直接使用 git url: "${git_url}" 拉取代碼
git branch: BRANCH, credentialsId: "${git_auth_id}", url: "${git_url}"
}
}
}
stage('依賴安裝') {
steps {
dir("${ENVIRONMENT.toLowerCase()}") {
sh (script: """
npm install --registry=https://registry.npm.taobao.org
""")
}
}
}
stage('代碼編譯') {
steps {
dir("${ENVIRONMENT.toLowerCase()}") {
sh (script: """
npm run build
""")
}
}
}
stage('鏡像構(gòu)建') {
steps {
dir("${ENVIRONMENT.toLowerCase()}") {
sh (script: """
oldImage=\$(docker images ${harbor_host}/${harbor_project}/${api_name}:${SERVICE_VERSION} | grep ${api_name} | awk \'{ print \$1":"\$2 }\')
if [ -z \$oldImage ]; then
echo "正常構(gòu)建鏡像"
else
echo "刪除存在鏡像"
docker rmi \$oldImage
fi
""")
sh 'pwd'
// 生成鏡像
sh "docker build -t ${docker_image} -f ${docker_file_path} ."
// 查看鏡像
sh "docker images ${harbor_host}/${harbor_project}/${api_name}"
}
}
}
stage('鏡像上傳') {
steps {
withCredentials([usernamePassword(credentialsId: "${harbor_crt_id}", passwordVariable: 'harbor_password', usernameVariable: 'harbor_user_name')]) {
sh (script: """
# 登錄鏡像倉庫
HARBOR_PASSWORD=${harbor_password} && echo "\$HARBOR_PASSWORD" | docker login ${harbor_host} -u ${harbor_user_name} --password-stdin
# 推送鏡像
docker push ${docker_image}
# 登出
docker logout ${harbor_host}
# 刪除鏡像
docker rmi ${docker_image}
""")
}
}
}
stage('發(fā)布到K8S') {
steps {
dir("${ENVIRONMENT.toLowerCase()}") {
sh """
api_name=${api_name}
deploy_api_name=\${api_name}
export REGISTRY_HOST_IMAGE=${docker_image}
export SERVICE_NAME=\${deploy_api_name}
export SERVICE_VERSION=\${SERVICE_VERSION}
export SERVICE_DEPLOYNAME_NAME=\${deploy_api_name}-deployment
export ASPNETCORE_ENVIRONMENT=${ENVIRONMENT}
export SERVICE_SERVICE_NAME=\${deploy_api_name}-service
export SERVICE_SERVICE_PORT_NAME=\${deploy_api_name}-port
export SERVICE_SERVICE_SELECT_NAME=\${deploy_api_name}
export SERVICE_SERVICE_NODE_PORT=${service_node_port}
export SERVICE_REPLICAS=${REPLICAS}
export K8S_DEPLOY_NAMESPACE=${k8s_namespace}
envsubst < deploy/k8s-master/template/api-deployment.yaml > deploy/k8s-master/template/api-real-deployment.yaml
echo 'deployment發(fā)布內(nèi)容'
cat deploy/k8s-master/template/api-real-deployment.yaml
envsubst < deploy/k8s-master/template/api-service.yaml > deploy/k8s-master/template/api-real-service.yaml
echo 'service發(fā)布內(nèi)容'
cat deploy/k8s-master/template/api-real-service.yaml
"""
withKubeConfig([credentialsId: "${k8s_crt_id}"]) {
sh 'kubectl apply -f deploy/k8s-master/template/api-real-deployment.yaml'
sh 'kubectl apply -f deploy/k8s-master/template/api-real-service.yaml'
}
}
}
}
}
}
在harbor網(wǎng)站里創(chuàng)建一個項(xiàng)目對應(yīng)harbor_project
與腳本相互對應(yīng),
如果不創(chuàng)建,鏡像創(chuàng)建不成功
在下面這個網(wǎng)站里創(chuàng)建一個k8s_namespace
與上面的腳本相互對應(yīng),
如果不創(chuàng)建,發(fā)布到k8s會報錯
然后還有一個Dockerfile文件
FROM nginx
COPY dist/ /var/test/html/
COPY dockerConf/default.conf /etc/nginx/conf.d/
EXPOSE 80
EXPOSE 443
然后還要在dockerConf文件夾下創(chuàng)建一個default.conf文件
server {
listen 80;
server_name "";
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/javascript application/x-javascript text/css application/css application/xml application/xml+rss text/javascript application/x-httpd-php ima
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
access_log /var/log/nginx/access.log;
location / {
root /var/test/html/;
index index.html index.htm;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.html?s=\$1 last;
break;
}
}
}
各種配置完之后,運(yùn)行一下文章來源:http://www.zghlxwxcb.cn/news/detail-723920.html
接下來,成功會變成這樣,如果失敗了,會在具體的哪一步報錯,根據(jù)錯誤信息去修改,有時候網(wǎng)速慢,會在下載依賴的時候就會報錯
這就成了
文章來源地址http://www.zghlxwxcb.cn/news/detail-723920.html
到了這里,關(guān)于Jenkins+vue發(fā)布項(xiàng)目的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!