Pipeline+Docker持续集成Node

如果你已经安装了Jenkins和Docker,cnpm,且有 版本控制系统(Git,公共私有都可以),也在jenkins里配置了凭据。第一步可以通过jenkins语法生成器生成。 

pipeline {
    agent any
    stages {
        stage('pull code') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/dev']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'xxxxxx', url: 'ssh://[email protected]']]])
            }
        } 
        stage('Build') {
            steps {
                sh '''
                cnpm install &&cnpm run build
                tar zcvf webtest.tar.gz  dist/*
                '''
            }
        }
        stage('build image') {
            steps {
                sh '''
                mv  webtest.tar.gz scripts/
                docker build -t web:test -f scripts/dockerfile scripts
                '''
            }
        }
        stage('deploy') {
            steps {
                sh '''
                docker run -d -p 81:80 --name web-test web:test 
                '''
            }
        }
    }
    post {
        always {
            sh '''
            rm -rf dist*
            rm -f scripts/webtest.tar.gz
            echo "deleted dist dir"
            '''
        }
    }
}

Dockerfile(代码根目录下创建scripts):

FROM nginx:1.15-alpine
ADD webtest.tar.gz  /usr/share/nginx/html

后期可以根据需求自行添加shell脚本到scripts目录来实现镜像和容器的删除、构建和运行

以上仅代表个人观点,根据需求采纳

你可能感兴趣的:(Docker,Jenkins)