美文网首页
jenkins与gitlab联通配置

jenkins与gitlab联通配置

作者: 郑码 | 来源:发表于2018-09-01 22:13 被阅读0次

操作

创建一个nodejs-web任务,gitlab push提交后,自动触发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实现node.js持续部署流程

相关文章

网友评论

      本文标题:jenkins与gitlab联通配置

      本文链接:https://www.haomeiwen.com/subject/deiuwftx.html