首页 > 其他分享 >docker in docker运行pipeline

docker in docker运行pipeline

时间:2023-04-07 23:00:21浏览次数:44  
标签:npm pipeline config && jenkins docker root 运行

一、配置JenkinsMaster挂载Docker

docker run --name jenkins -itd  --privileged=true      -p 8081:8080    -p 50000:50000 -v /etc/localtime:/etc/localtime:ro  -v /var/run/docker.sock:/var/run/docker.sock  -v /bin/docker:/usr/bin/docker -v /mnt/jenkins-data:/var/jenkins_home jenkins/jenkins:2.375.1-lts-jdk11  #挂载宿主机docker



解决权限问题/以root用户运行
grep docker /etc/group #获取docker组ID
docker exec -it -u root jenkins bash
groupadd docker -g 996  #和宿主机保持一致
gpasswd -a jenkins docker
newgrp docker
docker ps

二、docker in docker使用示例

1.pipeline

pipeline {
    agent {
        docker { 
            image 'maven:3.6.3-jdk-8' 
            args '-v $HOME/.m2:/root/.m2'
        }
    }
    stages {
        stage('Build') {
            steps {
                sh 'mvn -v'
            }
        }
    }
}

2.stage

pipeline {
    agent none
    stages {
        stage('ServiceBuild') {
            agent {
                docker { 
                    image 'maven:3.6.3-jdk-8' 
                    args '-v $HOME/.m2:/root/.m2'
                }
            }
            steps {
                sh 'mvn -v  && sleep 15'
            }
        }
      
        stage('WebBuild') {
            agent {
                docker { 
                    image 'node:7-alpine' 
                    args '-v $HOME/.npm:/root/.npm'
                }
            }
            steps {
                sh 'node -v  && sleep 15'
            }
        }
    }
}

三、前端pipeline示例

1.初始化前端项目

ZeyangdeMacBook-Pro:demo zeyang$ vue-init webpack test    #实际应该从git拉取

? Project name test
? Project description A Vue.js project
? Author adminuser <[email protected]>
? Vue build standalone
? Install vue-router? Yes
? Use ESLint to lint your code? No
? Set up unit tests No
? Setup e2e tests with Nightwatch? No
? Should we run `npm install` for you after the project has been created? (recom
mended) npm


ZeyangdeMacBook-Pro:~ zeyang$ cp -r demo/* jenkins/workspace/test/
ZeyangdeMacBook-Pro:~ zeyang$ cd jenkins/workspace/test
ZeyangdeMacBook-Pro:test zeyang$ ls
README.md        index.html        package.json
build            node_modules        src
config            package-lock.json    static

2.编写jenkinsfile(声明式)

pipeline {
    agent none
    stages {
        stage('WebBuild') {
            agent {
                docker { 
                    image 'node:10.19.0-alpine' 
                    args '-u 0:0 -v /var/jenkins_home/.npm:/root/.npm'  #此处/var/jenkins_home/.npm为 jenkins docker自己的目录
                }
            }
            steps {
                sh """
                    id 
                    ls /root/.npm

                    #npm config set unsafe-perm=true
                    npm config list
                    npm config set cache  /root/.npm
                    #npm config set registry https://registry.npm.taobao.org
                    npm config list
                    ls 
                    cd demo && npm install  --unsafe-perm=true && npm run build  && ls -l dist/ && sleep 15 
                """
            }
        }
    }
}

3.script(脚本式)

pipeline {
   agent {node {label "master"}}
    stages {
        stage('WebBuild') {
            steps {
                script {
                    docker.image('node:10.19.0-alpine').inside('-u 0:0 -v /var/jenkins_home/.npm:/root/.npm') {
                
                
                        sh """
                            id 
                            ls /root/.npm

                            ls /root/ -a
                            npm config set unsafe-perm=true
                            npm config list
                            npm config set cache  /root/.npm
                            #npm config set registry https://registry.npm.taobao.org
                            npm config list
                            ls 
                            cd demo && npm install  --unsafe-perm=true && npm run build  && ls -l dist/ && sleep 15 
                        """
                    }
                }
            }
        }
    }
}

四、注意事项

1. npm构建权限问题:使用root用户构建。设置容器运行用户 -u 0:0

2. npm打包慢问题: 
   2.1 挂载缓存卷 -v /var/jenkins_home/.npm:/root/.npm
   2.2 设置淘宝源 npm config set registry https://registry.npm.taobao.org

五、镜像清理(HARBOR)

会全部清理,注意测试环境验证生产谨慎使用

#!groovy
@Library('jenkinslibrary@master') _

def tools = new org.devops.tools()

String registryName = "${env.registryName}"
String serviceName = "${env.serviceName}"
String tagName = "${env.tagName}"
def harborProjects = []

currentBuild.description = "Trigger by ${serviceName} ${tagName}"


pipeline {
    agent { node { label "build"} }

    stages{

        stage("GetHarborTags"){
            steps{
                timeout(time:5, unit:"MINUTES"){
                    script{
                        tools.PrintMes("获取Harbor仓库中的项目信息","green")
                        println(serviceName)
                            
                        try {
                            response = httpRequest authentication: 'harbor-admin,
                                                   url: "https://registry.demo.com/api/repositories/${registryName}/${serviceName}/tags",
                                                   ignoreSslErrors: true
                            //println(response.content)
                            response = readJSON text: """${response.content}"""
                            
                        } catch(e){
                            response = ['name':'']
                            println(e)
                            println("Harbor镜像不存在此标签!")
                            
                        }

                        /*println(tagName)
                        for (tagname in response){
                            //println(response)
                            harborProjects << tagname['name']
                        }
                        
                        println(harborProjects)*/

                    }
                }
            }
        }


        
        stage("DeleteHarborTags"){
            steps{
                timeout(time:20, unit:"MINUTES"){
                    script{
                        tools.PrintMes("总共找到 ${harborProjects.size()} 个标签","green")
                        sumImageNum = harborProjects.size()
              //这里循环删除,注意验证,生产谨慎使用 for (tag in harborProjects){ sumImageNum -= 1 tools.PrintMes(" ${sumImageNum} Delete Tags ---> ${registryName} --> ${serviceName} --> ${tag} ","green") httpRequest httpMode: 'DELETE', authentication: 'c016027e-0573-4246-93cf-f4a55b08a86a', url: "https://registry.demo.com/api/repositories/${registryName}/${serviceName}/tags/${tag}", ignoreSslErrors: true sleep 1 } } } } } } post { always{ script{ cleanWs notFailBuild: true } } } }

 

标签:npm,pipeline,config,&&,jenkins,docker,root,运行
From: https://www.cnblogs.com/panwenbin-logs/p/17294555.html

相关文章

  • docker push 遇到 no basic auth credentials
      当遇到上面问题时,请在本地先登录一下远程仓库: dockerlogin http://101.201.237.xxx:5000......
  • Mac Apple 芯片运行 Vue 项目中 node-sass 转为 sass 遇到的问题记录,node-sass 替换成
    背景:前段时间因为某些原因将window笔记本换成MacM1pro,然后运行项目的时候发现高于node12版本的项目中不支持node-sass。记录下解决相关问题之后的记录......
  • docker-compose的安装
    1.去官网(https://github.com/docker/compose/releases)下载docker-compose-linux-x86_64,自行选择最新版或者所需要的版本即可建议直接通过curl下载到服务器上,若内网服务器就只能通过上传了,下面附上本人下载连接(GitHub太慢了,通过https://ghproxy.com/进行了一次代理)curl-SLhttp......
  • 使用Docker发布普通web项目(非springboot项目)
    公司的项目比较旧,还是普通的web格式,只能打个war包放在tomcat中运行,现在要上docker发布,在此做下记录DockerDesktop开发机是win10,想要打包镜像需要安装DockerDesktop,安装前需要在bios里面开启虚拟化支持。然后按照官方安装教程一步一步安装。Dockerfile然后在项目根目录编写doc......
  • PySide2或PyQt5 运行时提示This application failed to start because no Qt platform
    1.把PySide2或者PyQt5安装在解释器目录下的 \plugins\platforms 目录添加到环境变量Path中。比如,我的环境就是把下面这个路径加到环境变量 Path 中 c:\Python38\Lib\site-packages\PySide2\plugins\platforms 2. 确保环境变量中没有中文......
  • LNN工具链详解:在CSK6上运行你自己的AI算法
    工具链简介LNN(ListenAINeuralNetwork)是聆思科技专为聆思AIOT芯片(目前支持CSK6系芯片)定制开发的AI生态工具链,它包含linger和thinker两个部分,其中linger是量化训练组件,thinker是轻量级推理引擎组件,两个套件相互配合实现了训练推理一体化。LNN工具链已支撑聆思芯片在计算机......
  • docker-compose运行mysql 8.0.2
    docker-compose.yamlversion:'3.9'services:core:depends_on:mysql:condition:service_healthymysql:image:mysql:8.0.32container_name:mysql:8.0.32restart:alwayscommand:--default-authentication-plu......
  • Docker+Rancher + Harbor + GitLab + Jenkins 搭建CI/CD环境
    Docker+Rancher+Harbor+GitLab+Jenkins搭建CI/CD环境一.环境说明机器名称系统版本IP地址k8s版本docker版本k8s-rancher-205-10ubuntu22.04192.168.205.101.25.220.10.18k8s-master-205-11ubuntu22.04192.168.205.111.25.220.10.18k8s-worker01-205-12ubuntu22.04192.168.......
  • 向运行中的docker容器添加挂载磁盘
    需求容器跑了一段时间,空间不足,需要扩容。传统方法需要commit成新的image然后重新run添加-v进行挂载容器使用了很长时间,数据较多打包不方便,希望热添加。实现以下命令在root权限下执行sudo-i#找到当前容器my_container的iddockercontainerinspectmy_container|grep"......
  • rabbitmq docker部署、集成学习笔记
    docker部署rabbitmq:image:rabbitmq:managementcontainer_name:myrabbitrestart:alwayshostname:rabbitmq_hostports:-15672:15672#webUI管理接口-5672:5672#生产者和消费者连接使用的接口volum......