Yuav sau li cas hauv Jenkins yog tias koj xav tau ntau qhov chaw sib txawv

Yuav sau li cas hauv Jenkins yog tias koj xav tau ntau qhov chaw sib txawv

Muaj ntau cov ntawv hauv HabrΓ© txog Jenkins, tab sis ob peb piav piv txwv ntawm Jenkins thiab tus neeg sawv cev docker ua haujlwm li cas. Txhua qhov project nrov tsim cov cuab yeej zoo li Drone.io, Bitbucket Pipeline, GitLab, GitHub ua thiab lwm tus, tuaj yeem sau txhua yam hauv ntim. Tab sis dab tsi txog Jenkins?

Niaj hnub no muaj kev daws teeb meem: Jenkins 2 yog qhov zoo ntawm kev ua haujlwm nrog Docker agents. Hauv tsab xov xwm no kuv xav qhia kuv qhov kev paub thiab qhia tias koj tuaj yeem ua koj tus kheej li cas.

Vim li cas kuv thiaj pib daws qhov teeb meem no?

Txij li thaum peb nyob hauv tuam txhab Citronium Vim tias peb siv ntau yam thev naus laus zis, peb yuav tsum khaws cov qauv sib txawv ntawm Node.JS, Gradle, Ruby, JDK thiab lwm yam ntawm lub tshuab sib dhos. Tab sis feem ntau version tsis sib haum xeeb tsis tuaj yeem zam. Yog lawm, koj yuav ua tau yog tias koj hais tias muaj ntau tus thawj tswj hwm version zoo li nvm, rvm, tab sis tsis yog txhua yam zoo nrog lawv thiab cov kev daws teeb meem no muaj teeb meem:

  • ib tug loj npaum li cas ntawm runtime uas developers tsis nco qab ntxuav;
  • muaj kev tsis sib haum xeeb ntawm cov qauv sib txawv ntawm tib lub sijhawm ua haujlwm;
  • Txhua tus tsim tawm xav tau cov txheej txheem sib txawv.

Muaj lwm yam teeb meem, tab sis cia kuv qhia koj txog kev daws teeb meem.

Jenkins hauv Docker

Txij li thaum Docker yog tam sim no tsim tau zoo hauv lub ntiaj teb kev loj hlob, yuav luag txhua yam tuaj yeem khiav siv Docker. Kuv txoj kev daws teeb meem yog kom muaj Jenkins hauv Docker thiab tuaj yeem khiav lwm lub thawv Docker. Cov lus nug no tau pib nug rov qab rau xyoo 2013 hauv tsab xov xwm "Docker tuaj yeem khiav hauv Docker tam sim no".

Hauv luv luv, koj tsuas yog yuav tsum tau nruab Docker nws tus kheej hauv lub thawv ua haujlwm thiab txuas cov ntaub ntawv /var/run/docker.sock.

Nov yog ib qho piv txwv Dockerfile uas tau tawm rau Jenkins.

FROM jenkins/jenkins:lts

USER root

RUN apt-get update && 

apt-get -y install apt-transport-https 
     ca-certificates 
     curl 
     gnupg2 
     git 
     software-properties-common && 
curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg > /tmp/dkey; apt-key add /tmp/dkey && 
add-apt-repository 
   "deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") 
   $(lsb_release -cs) 
   stable" && 
apt-get update && 
apt-get -y install docker-ce && 
usermod -aG docker jenkins

RUN curl -L https://github.com/docker/compose/releases/download/1.25.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose 

RUN apt-get clean autoclean && apt-get autoremove β€”yes && rm -rf /var/lib/{apt,dpkg,cache,log}/

USER jenkins

Yog li, peb tau txais Docker thawv uas tuaj yeem ua tiav Docker cov lus txib ntawm lub tshuab host.

Tsim kev teeb tsa

Tsis ntev dhau los Jenkins tau txais lub sijhawm los piav txog nws txoj cai siv Pipeline syntax, uas ua rau nws yooj yim heev los hloov cov ntawv tsim thiab khaws cia rau hauv qhov chaw cia khoom.

Yog li cia peb muab ib qho tshwj xeeb Dockerfile rau hauv qhov chaw cia nws tus kheej, uas yuav muaj tag nrho cov tsev qiv ntawv tsim nyog rau kev tsim. Txoj kev no, tus tsim tawm nws tus kheej tuaj yeem npaj qhov chaw rov ua dua thiab yuav tsis tas nug OPS los nruab ib qho tshwj xeeb ntawm Node.JS ntawm tus tswv tsev.

FROM node:12.10.0-alpine

RUN npm install yarn -g

Cov duab tsim no tsim nyog rau feem ntau cov ntawv thov Node.JS. Yuav ua li cas yog tias, piv txwv li, koj xav tau ib daim duab rau JVM qhov project nrog Sonar scanner nyob rau hauv? Koj muaj kev ywj pheej xaiv cov khoom uas koj xav tau rau kev sib dhos.

FROM adoptopenjdk/openjdk12:latest

RUN apt update 
    && apt install -y 
        bash unzip wget

RUN mkdir -p /usr/local/sonarscanner 
    && cd /usr/local/sonarscanner 
    && wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.3.0.1492-linux.zip 
    && unzip sonar-scanner-cli-3.3.0.1492-linux.zip 
    && mv sonar-scanner-3.3.0.1492-linux/* ./ 
    && rm sonar-scanner-cli-3.3.0.1492-linux.zip 
    && rm -rf sonar-scanner-3.3.0.1492-linux 
    && ln -s /usr/local/sonarscanner/bin/sonar-scanner /usr/local/bin/sonar-scanner

ENV PATH $PATH:/usr/local/sonarscanner/bin/
ENV SONAR_RUNNER_HOME /usr/local/sonarscanner/bin/

Peb tau piav qhia txog qhov chaw sib sau ua ke, tab sis Jenkins ua li cas rau nws? Thiab Jenkins cov neeg sawv cev tuaj yeem ua haujlwm nrog cov duab Docker thiab tsim lawv sab hauv.

stage("Build project") {
    agent {
        docker {
            image "project-build:${DOCKER_IMAGE_BRANCH}"
            args "-v ${PWD}:/usr/src/app -w /usr/src/app"
            reuseNode true
            label "build-image"
        }
    }
    steps {
        sh "yarn"
        sh "yarn build"
    }
}

Qhia agent siv cuab yeej dockerqhov twg koj tuaj yeem qhia meej:

  • lub npe ntawm lub thawv sib dhos raws li koj txoj cai naming;
  • kev sib cav xav tau los khiav lub thawv tsim, qhov twg nyob rau hauv peb rooj plaub peb mount lub tam sim no directory raws li ib tug directory nyob rau hauv lub thawv.

Thiab twb nyob rau hauv cov kauj ruam tsim peb qhia cov lus txib twg los ua hauv Docker tsim tus neeg sawv cev. Qhov no tuaj yeem yog txhua yam, yog li kuv kuj tseem tso tawm daim ntawv thov xa tawm siv ansible.

Hauv qab no kuv xav qhia qhov dav dav Jenkinsfile uas yooj yim Node.JS daim ntawv thov tuaj yeem tsim.

def DOCKER_IMAGE_BRANCH = ""
def GIT_COMMIT_HASH = ""

pipeline { 
    options {
        buildDiscarder(
            logRotator(
                artifactDaysToKeepStr: "",
                artifactNumToKeepStr: "",
                daysToKeepStr: "",
                numToKeepStr: "10"
            )
        )
        disableConcurrentBuilds()
    }

    agent any

    stages {

        stage("Prepare build image") {
            steps {
                sh "docker build -f Dockerfile.build . -t project-build:${DOCKER_IMAGE_BRANCH}"
            }
        }

        stage("Build project") {
            agent {
                docker {
                    image "project-build:${DOCKER_IMAGE_BRANCH}"
                    args "-v ${PWD}:/usr/src/app -w /usr/src/app"
                    reuseNode true
                    label "build-image"
                }
            }
            steps {
                sh "yarn"
                sh "yarn build"
            }
        }

    post {
        always {
            step([$class: "WsCleanup"])
            cleanWs()
        }
    }

}

Dab tsi tshwm sim?

Ua tsaug rau txoj kev no, peb daws cov teeb meem hauv qab no:

  • ib puag ncig los ua ke configuration lub sij hawm raug txo mus rau 10 - 15 feeb ib qhov project;
  • ib daim ntawv thov rov ua dua tshiab tsim ib puag ncig, txij li koj tuaj yeem tsim nws li no ntawm koj lub computer hauv zos;
  • tsis muaj teeb meem nrog kev tsis sib haum xeeb ntawm cov qauv sib txawv ntawm cov cuab yeej sib dhos;
  • ib txwm ib lub chaw ua haujlwm huv uas tsis tau txhaws.

Kev daws nws tus kheej yog qhov yooj yim thiab pom tseeb thiab tso cai rau koj kom tau txais qee qhov zoo. Yog lawm, qhov pib nkag tau nce me ntsis piv rau cov lus txib yooj yim rau cov rooj sib txoos, tab sis tam sim no muaj kev lees paub tias nws yuav raug tsim ua ib txwm thiab tus tsim tawm nws tus kheej tuaj yeem xaiv txhua yam tsim nyog rau nws cov txheej txheem tsim.

Koj kuj siv tau cov duab kuv sau Jenkins + Docker. Txhua qhov chaw qhib thiab nyob ntawm rmuhamedgaliev/jenkins_docker ua.

Thaum sau tsab xov xwm no, kev sib tham tau tshwm sim txog kev siv cov neeg sawv cev ntawm cov chaw taws teeb tswj kom tsis txhob thauj cov tswv node siv lub plugin docker-plugin. Tab sis kuv yuav tham txog qhov no yav tom ntej.

Tau qhov twg los: www.hab.com

Ntxiv ib saib