Jenkins可伸缩3-做流水线npm发布项目

参考链接:https://blog.csdn.net/chihong9645/article/details/100722537

一、制作镜像
先启动一个不带jdk和maven的jnlp容器
docker pull jenkins/jnlp-slave:4.9-1
因为默认是jenkins用户进的,这里就直接用root进去

docker run --name=jnlp **-u root** -it jenkins/jnlp-slave:4.9-1 /bin/bash
mkdir -p /usr/local/nodejs
cd /usr/local/nodejs

在宿主机

wget https://nodejs.org/dist/v16.14.2/node-v16.14.2-linux-x64.tar.xz
xz -d node-v16.14.2-linux-x64.tar.xz
docker cp node-v16.14.2-linux-x64.tar jnlp:/usr/local/nodejs/

再到容器里面

tar -xvf node-v16.14.2-linux-x64.tar
echo 'NODEJS_HOME=/usr/local/nodejs/node-v16.14.2-linux-x64' >>/etc/profile
echo 'PATH=$NODEJS_HOME/bin:$PATH' >>/etc/profile
echo 'export PATH' >>/etc/profile
source /etc/profile
ln -s /usr/local/nodejs/node-v16.14.2-linux-x64/bin/npm /usr/bin/npm
ln -s /usr/local/nodejs/node-v16.14.2-linux-x64/bin/node /usr/bin/node
npm -v

在这里插入图片描述

在宿主机执行

docker commit jnlp 192.168.1.169:80/jyyun/jenkins-agent:npm-16.14.2

二、镜像实验

docker run --name=jnlp **-u jenkins**  -it 192.168.1.169:80/jyyun/jenkins-agent:npm-16.14.2 /bin/bash

修改npm的镜像

npm config set registry [http://192.168.1.182:11280/](http://192.168.1.182:11280/)
npm config get registry

git clone -b dev http://xxx/integrated-console.git
cd integrated-console/manage-console
npm install -d
npm run build:dev

Jenkins可伸缩3-做流水线npm发布项目_第1张图片

成功了

三、重新构建一个默认jenkins用户的镜像

docker run --name=jnlp **-u jenkins**  -it 192.168.1.169:80/jyyun/jenkins-agent:npm-16.14.2 /bin/bash

修改npm的镜像

npm config set registry [http://192.168.1.182:11280/](http://192.168.1.182:11280/)
npm config get registry

设置免密登录

cd
ssh-keygen -t rsa
cd .ssh
cat id_rsa.pub

然后把内容拷贝到docker服务器的~/.ssh/authorized_keys中(备注,目标服务器的设置在jenkins配置的时候已经有说明,此处不赘述)

docker commit jnlp 192.168.1.169:80/jyyun/jenkins-agent:npm-16.14.2-20220322

docker push 192.168.1.169:80/jyyun/jenkins-agent:npm-16.14.2-20220322

四、开始设置流水线
Jenkins可伸缩3-做流水线npm发布项目_第2张图片

注意,需要分两步,第一步,就是要把项目clone和install出来;后面再更改为pull和run
建个挂载盘:
Jenkins可伸缩3-做流水线npm发布项目_第3张图片

第一步的脚本:
备注:如果重新设置的话,首先确保NFS服务器中该项目对应的两个目录被删除了(下面应该是rm -rf 项目名)
在这里插入图片描述

podTemplate(containers: [
    containerTemplate(
        name: 'jnlp', 
        image: '192.168.1.169:80/jyyun/jenkins-agent:npm-16.14.2-20220321', 
        alwaysPullImage: false, 
        args: '${computer.jnlpmac} ${computer.name}'),
  ],
  volumes: [
    persistentVolumeClaim(mountPath: '/home/jenkins/agent/workspace/', claimName: 'jenkins-npmproject')
  ]) {
    node(POD_LABEL) {
        stage('git clone') {
           echo "prepare to clone"
           sh 'git clone -b dev http://xxx/integrated-console.git'
        	 echo "clone completely"
        }
        stage('install') {
           sh '''
           cd integrated-console/manage-console
           npm config set registry http://192.168.1.182:11280/
		   npm install -d
           '''
        }
        stage('test') {
            echo "hello test"
        }
        stage('deploy') {
            echo "hello deploy"
        }
    }
}

可以成功构建:
Jenkins可伸缩3-做流水线npm发布项目_第4张图片

Jenkins可伸缩3-做流水线npm发布项目_第5张图片

第二步脚本:

podTemplate(containers: [
    containerTemplate(
        name: 'jnlp', 
        image: '192.168.1.169:80/jyyun/jenkins-agent:npm-16.14.2-20220321', 
        alwaysPullImage: false, 
        args: '${computer.jnlpmac} ${computer.name}'),
  ],
  volumes: [
    persistentVolumeClaim(mountPath: '/home/jenkins/agent/workspace/', claimName: 'jenkins-npmproject')
  ]) {
    node(POD_LABEL) {
        stage('git pull') {
           echo "prepare to pull"
           sh '''
           cd /home/jenkins/agent/workspace/test_npm_pipline/integrated-console
           git pull
           '''
        	 echo "pull completely"
        }
        stage('install') {
           sh '''
           cd /home/jenkins/agent/workspace/test_npm_pipline/integrated-console/manage-console
					 npm run build:dev
           '''
        }
        stage('test') {
            echo "hello test"
        }
        stage('deploy') {
            echo "hello deploy"
        }
    }
}

构建成功了:
Jenkins可伸缩3-做流水线npm发布项目_第6张图片

第三步,增加发布到docker镜像仓库
脚本:

podTemplate(containers: [
    containerTemplate(
        name: 'jnlp', 
        image: '192.168.1.169:80/jyyun/jenkins-agent:npm-16.14.2-20220321', 
        alwaysPullImage: false, 
        args: '${computer.jnlpmac} ${computer.name}'),
  ],
  volumes: [
    persistentVolumeClaim(mountPath: '/home/jenkins/agent/workspace/', claimName: 'jenkins-npmproject')
  ]) {
    node(POD_LABEL) {
        stage('git pull') {
           echo "prepare to pull"
           sh '''
           cd /home/jenkins/agent/workspace/test_npm_pipline/integrated-console
           git pull
           '''
           echo "pull completely"
        }
        stage('install') {
           sh '''
           cd /home/jenkins/agent/workspace/test_npm_pipline/integrated-console/manage-console
		   npm run build:dev
           '''
        }
        stage('test') {
            echo "hello test"
        }
        stage('deploy') {
            echo "remote 1 start"
            sh '''
            ssh -o StrictHostKeyChecking=no [email protected] << remotessh
            cd /root/dockerbuild/npmwebtest
			rm -rf dist
			exit
            remotessh
            '''
            echo "remote 1 end"
            echo "remote 2 start"
            sh '''
            cd
            scp -o StrictHostKeyChecking=no -r agent/workspace/test_npm_pipline/integrated-console/manage-console/dist [email protected]:/root/dockerbuild/npmwebtest/
            '''
            echo "remote 2 end"
            echo "remote 3 start"
            sh '''
			ssh -o StrictHostKeyChecking=no [email protected] << remotessh
			cd /root/dockerbuild/npmwebtest
			docker build --rm --tag 192.168.1.169:80/jyyun/npmwebtest:`date +%Y%m%d` .
			docker push 192.168.1.169:80/jyyun/npmwebtest:`date +%Y%m%d`
            exit
            remotessh
            '''
            echo "remote 3 end"
        }
    }
}

构建成功了:
Jenkins可伸缩3-做流水线npm发布项目_第7张图片

Jenkins可伸缩3-做流水线npm发布项目_第8张图片

访问也正常(具体怎么发布在此不赘述了):
Jenkins可伸缩3-做流水线npm发布项目_第9张图片
欢迎关注公众号一起探讨技术问题:
Jenkins可伸缩3-做流水线npm发布项目_第10张图片

你可能感兴趣的:(运维部署,kubernetes,容器,jenkins,docker,node.js)