使用Jenkins部署 node + docker 应用-王树东

创建一个Dockerfile 文件

FROM node:12.10.0-alpine

LABEL maintainer="starkwang"

RUN mkdir -p /usr/app

WORKDIR /usr/app

COPY . /usr/app

EXPOSE 80

RUN yarn install  --registry=https://registry.npm.taobao.org

RUN npm run build:prod

CMD ["npm","run", "start"]

在项目中创建deploy.sh

#!/bin/bash
#set -x
#author https://shudong.wnag

IMAGE_NAME="qianduanfun"
CONTAINER_NAME="qianduanfun:latest"

DEL_DOCKER(){
  cid=(docker ps | grepCONTAINER_NAME |awk '{print 1}')
  if [ x"cid" != x ]
      then
      docker stop cid
      docker rm -fcid
  fi
}

BUILD(){
  echo 'start build'
  docker build -t CONTAINER_NAME .
  DEL_DOCKER
  docker run --nameIMAGE_NAME -d -p 3800:80 CONTAINER_NAME
  END_TIME=(date +%s)
  ELAPSED_TIME=((END_TIME - START_TIME))
  s_to_minute=60
  RUN_MINUTE=`echo "scale=2;ELAPSED_TIME/s_to_minute" | bc`
  echo endtime=`date +'%Y-%m-%d %H:%M:%S'`
  echo "Runtime:ELAPSED_TIME seconds"
  echo "Runtime: RUN_MINUTE minute"
}

START_TIME=(date +%s)
BUILD

Jenkins上面配置

cd /var/lib/jenkins/workspace/qianduan.fun && sh deploy.sh