1. 程式人生 > 其它 >docker部署jenkins

docker部署jenkins

技術標籤:jenkinsdevops

前言

docker-compose方式部署jenkins

]# groudadd jenkins
]# useradd jenkins -g jenkins
]# mkdir /opt/jenkins
]# chown -R jenkins:jenkins /opt/jenkins
]# cat docker-compose.yml <<-EOF
version: "3.6"
services:  
  jenkins:
    image: jenkinsci/blueocean
    container_name: jenkinsci
    restart: always
    hostname: jenkinsci
    ports:
      - 8080:8080
      - 50000:50000
    volumes:
      - /opt/jenkins:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock
EOF
]# docker-compose up -d

pipeline流水線輸出"Hello World"

安裝pipeline外掛操作,這裡不再贅述
建立流水線的操作,這裡不再贅述

pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

pipeline流水線克隆程式碼倉庫,並清空工作區

安裝git、Workspace Clean外掛操作,這裡不再贅述
建立流水線的操作,這裡不再贅述

node {
    
    def gitRepoUrl = "http://gitmylab.better.me/CJFC/BetterMe.git"
    def gitBranch = "master"
    def gitCredentialsId = "gitlab-jenkins"	# 建立憑證,這裡不再贅述
    
    stage("Git-Checkout") {	# 克隆專案程式碼
        checkout([
            $class: "GitSCM",
            branches: [[name: gitBranch]],
            userRemoteConfigs: [[
                url: gitRepoUrl,
                credentialsId: gitCredentialsId
            ]]
        ])
        env.COMMIT_ID = sh(returnStdout: true, script: "git rev-parse " + gitBranch).trim()
        env.COMMIT_ID_SHORT = sh(returnStdout: true, script: "git rev-parse --short " + gitBranch).trim()
    }
    
    stage('Cleanup') {	# 清空工作目錄,釋放空間
        cleanWs notFailBuild: true
    }
    
}
  • git知識點
    git.exe rev-parse gitBranch # 獲取整個專案的commit id的字串
    git.exe rev-parse --short gitBranch # 獲取整個專案的commit id的字串的前七位

結語

安裝Jenkins
dockerhub-jenkins-image