Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

KJH

Jenkins Chain Job, Artifact 본문

DevOps

Jenkins Chain Job, Artifact

모이스쳐라이징 2024. 9. 8. 23:07

순차적인 작업이나 application 빌드에 사용할 base이미지를 생성해야하는 경우에 사용하면 좋은 기능

 

시나리오

 

 

 

BuildBaseJob

빌드 및 이미지 push가 끝났다는 걸 가정하고, 해당 tag를 txt파일로 생성

        stage('Build and Tag Docker Image') {
            steps {
                script {
                    writeFile file: "${env.baseImageTxt}", text: "${imageName}:${buildDate}"
                }
            }
        }

 

Job이 성공했다면 해당 txt 등록 및 다음 Chain Job 실행

    post {
        success {
            script {
                archiveArtifacts artifacts: "${env.baseImageTxt}"

                env.updatePath = VarFlink.updatePath()
                build job: "${env.updateJob}", wait: false, parameters: [
                    string(name: 'BASE_PATH', value: "${env.updatePath}"),
                ]
            }
        }
    }

 

 

ParallelJob

trigger된 커밋에 변경 이력이 있는 파일 만큼 병렬 job 생성

 

※ -diff-filter=d로 삭제된 파일은 필터에서 제외 가능

    env.changedPythonFiles = sh(
        script: "git diff --name-only --diff-filter=d HEAD~1 HEAD | grep '.py' | cut -d'/' -f2",
        returnStdout: true
    ).trim()

    changedPythonFilesArray = env.changedPythonFiles.split('\n').findAll { it.trim() }
    changedPythonFilesArray.each { changedPythonFile ->
        echo "Changed Python files: ${changedPythonFile}"
        def changedPythonName = changedPythonFile.replaceAll('[._]', '-')

        build job: "${env.deployJob}", wait: false, parameters: [
            string(name: 'BASE_PATH', value: "${env.deployJobPath}"),
            string(name: 'POD_TEMPLATE_PATH', value: 'PodTemplate.yaml'),
            string(name: 'IMAGE_SECRET_NAME', value: 'xxx-regcred'),
            string(name: 'KUBECONFIG_SECRET_NAME', value: 'xxx-kubeconfig'),
            string(name: 'PYTHON_FILE', value: "${changedPythonFile}"),
            string(name: 'PYTHON_NAME', value: "${changedPythonName}"),
        ]
    }

 

간단한 parallel Job 예제

stage 안에서 진행해야함

        stage('Build and Tag Docker Image') {
            parallel{
                stage('Build 1') {
                    steps {
                        script {
                            sh 'echo 1 build...'
                        }
                    }
                }
                stage('Build 2') {
                    steps {
                        script {
                            sh 'echo 2 build...'
                        }
                    }
                }
            }
        }

 

 

BASE_IMAGE를 인자로 받아 빌드 실행

def buildArgs = "--build-arg BASE_IMAGE=${env.BASE_IMAGE} "

--Dockerfile--
ARG BASE_IMAGE
FROM xxx/${BASE_IMAGE}

 

DeployJob

buildDescription을 사용하면 build History에서 어떤 파일로 job이 도는지 확인할 수 있음

        stage("Initialization") {
            steps {
                // buildDescription "Executed @ ${params.PYTHON_FILE}"
                buildDescription("Committer: ${params.PYTHON_FILE}")
            }
        }

 

'DevOps' 카테고리의 다른 글

Jenkins RBAC(w/ helm chart)  (0) 2024.09.08
Jenkins Pipeline간 전역변수 사용(w/ Shared Libray)  (0) 2024.09.08
Jenkins Shared Library (w/ kaniko)  (0) 2024.09.08
Prometheus Client  (0) 2024.07.01
Fortigate  (0) 2024.04.29