jenkins與gitlab聯(lián)通配置

操作

創(chuàng)建一個nodejs-web任務,gitlab push提交后,自動觸發(fā)jenkins構建任務

流程圖

image.png

jenkins配置

jj-1-1.png
jj-2-1.png
jj-2-2.png
jj-2-4.png

gitlab配置

給gitlab管理員身份進入,在每個項目中:Settings -> Integrations 頁面中進行設置

jj-2-g5.png
jj-2-g6.png
jj-2-g7.png
jj-2-g8err.png
jj-2-g8ok.png
jj-2-g9.png

腳本文件

基于Vue的Dockerfile

參考: Dockerize Vue.js App

FROM node:10.9.0-alpine

# fix "could not get uid/gid" error
RUN npm config set unsafe-perm true

# install simple http server for serving static content
RUN npm install -g http-server

# make the 'app' folder the current working directory
WORKDIR /app

# copy both 'package.json' and 'package-lock.json' (if available)
COPY package*.json ./

# install project dependencies
RUN npm install --registry=https://registry.npm.taobao.org

# copy project files and folders to the current working directory (i.e. 'app' folder)
COPY . .

# build app for production with minification
RUN npm run build:prod

EXPOSE 8080
CMD [ "http-server", "dist" ]

Jenkinsfile

#!/usr/bin/env groovy

def url = "http://192.168.31.100:8088/nodejs/web.git"
def credentialsId = "850f1ba0-caf8-416f-9d9e-130403e93730"
def containerName = "nodejs-web"
def sourcePort = 8080
def targetPort = 9002
def imageUrl = "reg.htwy.com/nodejs/${containerName}:${env.BUILD_NUMBER}"

node() {

    stage ('checkout') {
        git url: url, credentialsId: credentialsId
    }

    stage ('build and push image') {
        sh "docker build -t ${imageUrl} ."
        sh "docker push ${imageUrl}"
    }

    stage ('Run container') {
        try {
            // 默認 停止、刪除 容器
            sh "docker ps -f name=${containerName} -q | xargs --no-run-if-empty docker container stop"
            sh "docker container ls -a -f name=${containerName} -q | xargs -r docker container rm"

            //運行新容器
            sh "docker run -it -d --name ${containerName} -p ${targetPort}:${sourcePort} ${imageUrl}"

        } catch (error) {
            error.printStackTrace()
        } finally {
        }
    }
}

基于Eggjs的Dockerfile

參考: eggjs + docker 最佳實踐

FROM node:8.6.0-alpine

RUN apk --update add tzdata \
    && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo "Asia/Shanghai" > /etc/timezone \
    && apk del tzdata

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

# add npm package
COPY package.json /usr/src/app/package.json

RUN npm i --registry=https://registry.npm.taobao.org

# copy code
COPY . /usr/src/app

EXPOSE 7001

CMD npm start

參考

gitlab + jenkins實現(xiàn)node.js持續(xù)部署流程

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容