侧边栏壁纸
博主头像
WinJay博主等级

博观而约取 厚积而薄发

  • 累计撰写 317 篇文章
  • 累计创建 169 个标签
  • 累计收到 1 条评论
隐藏侧边栏

Demo-Test-NodeJS-Nginx

WinJay
2022-03-11 / 0 评论 / 0 点赞 / 99 阅读 / 6,850 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-04-20,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
/*
//==========通用变量信息=========
def GitAddr = "https://git.nercoa.com/fz/other/uc/sprout.git"
def Branch = "master"
def ImageTag = "v1.0.0"
def Project = "vue-nodejs"
def App_name = "backstage"
def PortNumber "38081"

//====================以上变量可在新建Job任务时使用字符参数进行插入=====================

*/
 //Job Start!!
 
    //阶段1:拉取代码,node 节点未指定,则在Master执行,因为Git、Maven 环境都在 Master,所以就在主节点执行了
stage(' Git Checkout') {
    node('MSBuild') {
       checkout([$class: 'GitSCM', branches: [[name: '${Branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '973c9a08-4d62-4f8a-8792-7a214b7ffe66', url: '${GitAddr}']]])
    }
}

    //阶段2:Dotnet编译代码
stage('Build'){
    node('MSBuild') {
       echo '编译解决方案'
       bat "cd src/${App_name} && npm install && npm run build"
    }
}

stage('Backup To The Site Directory'){
    node('MSBuild') {
//        dir('dist') {
        bat "cd src/${App_name}  && zip -r ${BUILD_NUMBER}.zip dist/*"
//        }
        echo '拷贝文件至备份目录'
        bat "echo d | xcopy /e /y /r /f src\\${App_name}\\${BUILD_NUMBER}.zip \"${BACKUP_SHARE_DIR}/${JOB_NAME}/\""
        bat "rm -rf src\\${App_name}\\dist && rm -rf src\\${App_name}\\*.zip"        
   }
}

stage('Download Project Code'){
    node('TestEnv') {
        //echo '建立业务目录' #会在 workspace 下自动创建任务目录
        //sh "mkdir -p ${JOB_NAME} && cd ${JOB_NAME}"
        echo '下载程序'
        sh "curl -O http://rls.nercoa.com/${JOB_NAME}/${BUILD_NUMBER}.zip"
        echo '解压源程序'
        sh "unzip -o *.zip && mv -f dist/* ./ && rm -rf *.zip dist"
    }
}    

    
    //阶段3:构建Docker镜像并推送到私有仓库
stage('Build And Push Image') {
    node('TestEnv') { 
        sh '''
        REPOSITORY=harbor.nercoa.com/${Project}/${App_name}:${ImageTag}
        cat > Dockerfile << EOF
        #注意选择镜像镜像版 
        FROM nginx
        MAINTAINER WinJayX <WinJayX@Gmail.com>
        LABEL description="This is NodeJS Project"
        LABEL version="0.1"
        USER root

        COPY . /usr/share/nginx/html

EOF

        docker build -t $REPOSITORY .
        docker login harbor.nercoa.com -u admin -p Nerc.nerc/1
        docker push $REPOSITORY
        rm -rf *
        '''
    }
}

    //阶段4:部署到远程Node Docker主机节点
stage('Deploy To The Test Server') {
    node('TestEnv') { 
        sh '''
        REPOSITORY=harbor.nercoa.com/${Project}/${App_name}:${ImageTag}
        docker rm -f ${App_name} || true
        docker container run -d \
        --volume /etc/localtime:/etc/localtime:ro \
        --restart always \
        --user root \
        --name ${App_name} \
        --hostname ${App_name} \
        -p ${PortNumber}:80 $REPOSITORY
        '''
    }
}


/* 
    //阶段5:  获取站点的状态  
stage('Test the WebSite') {
    node('Node12') {
        sh '''
        curl -I -m 10 -o /dev/null -s -w %{http_code} 202.205.161.114:39096
        '''
    }
}

   
stage('Deploying To The Production Server'){
    timeout(time: 1, unit: 'DAYS') {
    input message: 'Deploying To The Production Server?', ok: 'Deployment'
    }
}

 
    //阶段6:部署到生产环境服务器,||true 短路逻辑运算,只有前面返回假echo $? =1 时,后面才执行

stage('Deploy To The Production Docker Server') {
//      方式1 执行远程服务器上的脚本文件
//    node {
//        sh '''
//        ssh root@202.205.161.131 "cd /home/docker/003.CourseDetectionWeb ; sh docker_CourseDetectionSystem.start.sh"
//        '''
//    }

      //方式2
    node('Node12') { 
        sh '''
        cd /home/Docker/009.FZ.VR.Authorizationsystem
        REPOSITORY=harbor.nercoa.com/fz_vr/authorization:${ImageTag}
        docker rm -f VR_Auth || true
        docker container run -d \
        --volume /etc/localtime:/etc/localtime:ro \
        --volume `pwd`/Config:/app/Config \
        --restart always \
        --user root \
        --name VR_Auth \
        --hostname VR_Auth \
        -p 8009:80 \
        $REPOSITORY
        '''
  }
}
*/

image.png

image.png

image.png

image.png

V2

/*
//==========通用变量信息=========
def GitAddr = "https://git.nercoa.com/fz/other/uc/sprout.git"
def Branch = "master"
def ImageTag = "v1.0.0"
def Project = "vue-nodejs"
def App_name = "backstage"
def PortNumber "38081"

//====================以上变量可在新建Job任务时使用字符参数进行插入=====================

*/
 //Job Start!!
 
    //阶段1:拉取代码,node 节点未指定,则在Master执行,因为Git、Maven 环境都在 Master,所以就在主节点执行了
stage(' Git Checkout') {
    node('MSBuild') {
       checkout([$class: 'GitSCM', branches: [[name: '${Branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '973c9a08-4d62-4f8a-8792-7a214b7ffe66', url: '${GitAddr}']]])
    }
}

    //阶段2:Dotnet编译代码
stage('Build'){
    node('MSBuild') {
       echo '编译解决方案'
       bat "cd src/${App_name} && npm install && npm run build"
    }
}

stage('Backup To The Site Directory'){
    node('MSBuild') {
//        dir('dist') {
        bat "cd src/${App_name}  && zip -r ${BUILD_NUMBER}.zip dist/*"
//        }
        echo '拷贝文件至备份目录'
        bat "echo d | xcopy /e /y /r /f src\\${App_name}\\${BUILD_NUMBER}.zip \"${BACKUP_SHARE_DIR}/${JOB_NAME}/\""
        bat "rm -rf src\\${App_name}\\dist && rm -rf src\\${App_name}\\*.zip"        
   }
}

stage('Download Project Code'){
    node('TestEnv') {
        //echo '建立业务目录' #会在 workspace 下自动创建任务目录
        //sh "mkdir -p ${JOB_NAME} && cd ${JOB_NAME}"
        echo '下载程序'
        sh "curl -O http://rls.nercoa.com/${JOB_NAME}/${BUILD_NUMBER}.zip"
        echo '解压源程序'
        sh "unzip -o *.zip && mv -f dist/* ./ && rm -rf *.zip dist"
    }
}    

    
    //阶段3:构建Docker镜像并推送到私有仓库
stage('Build And Push Image') {
    node('Container') { 
        sh '''
        REPOSITORY=harbor.nercoa.com/${Project}/${App_name}:${ImageTag}
        cat > Dockerfile << EOF
        #注意选择镜像镜像版 
        FROM nginx
        MAINTAINER WinJayX <WinJayX@Gmail.com>
        LABEL description="This is NodeJS Project"
        LABEL version="0.1"
        USER root

        COPY . /usr/share/nginx/html

EOF

        docker build -t $REPOSITORY .
        docker login harbor.nercoa.com -u admin -p Nerc.nerc/1
        docker push $REPOSITORY
        rm -rf *
        '''
    }
}

    //阶段4:部署到远程Node Docker主机节点
stage('Deploy To The Test Server') {
    node('TestEnv') { 
        sh '''
        REPOSITORY=harbor.nercoa.com/${Project}/${App_name}:${ImageTag}
        docker rm -f ${App_name} || true
        docker container run -d \
        --volume /etc/localtime:/etc/localtime:ro \
        --restart always \
        --user root \
        --name ${App_name} \
        --hostname ${App_name} \
        -p ${PortNumber}:80 $REPOSITORY
        '''
    }
}


/* 
    //阶段5:  获取站点的状态  
stage('Test the WebSite') {
    node('Node12') {
        sh '''
        curl -I -m 10 -o /dev/null -s -w %{http_code} 202.205.161.114:39096
        '''
    }
}

   
stage('Deploying To The Production Server'){
    timeout(time: 1, unit: 'DAYS') {
    input message: 'Deploying To The Production Server?', ok: 'Deployment'
    }
}

 
    //阶段6:部署到生产环境服务器,||true 短路逻辑运算,只有前面返回假echo $? =1 时,后面才执行

stage('Deploy To The Production Docker Server') {
//      方式1 执行远程服务器上的脚本文件
//    node {
//        sh '''
//        ssh root@202.205.161.131 "cd /home/docker/003.CourseDetectionWeb ; sh docker_CourseDetectionSystem.start.sh"
//        '''
//    }

      //方式2
    node('Node12') { 
        sh '''
        cd /home/Docker/009.FZ.VR.Authorizationsystem
        REPOSITORY=harbor.nercoa.com/fz_vr/authorization:${ImageTag}
        docker rm -f VR_Auth || true
        docker container run -d \
        --volume /etc/localtime:/etc/localtime:ro \
        --volume `pwd`/Config:/app/Config \
        --restart always \
        --user root \
        --name VR_Auth \
        --hostname VR_Auth \
        -p 8009:80 \
        $REPOSITORY
        '''
  }
}
*/
0

评论区