GitHub

https://github.com/Choidongjun0830

클라우드

Jenkins로 커밋 후 자동 빌드 ~ yaml 수정 후 push까지 pipeline script (윈도우 환경)

gogi masidda 2024. 8. 17. 17:13
pipeline {
    agent any

    environment {
        imagename = "insurance"
        repository = "choidongjun/graduationwork"
        registryCredential = "dockerhub_cred" //Dockerhub 자격 증명명
    }

    stages {
        stage('github clone Project repository') {
            // steps {
            //     sshagent (credentials: ['ssh-rsa-private-key']) {
            //         git branch: 'main', url: 'https://github.com/Choidongjun0830/graduationWork'
            //     }
            // }
            steps {
                echo 'Clone Github Repository'
                git branch: 'main', credentialsId: 'github-username-pw', url: 'https://github.com/Choidongjun0830/graduationWork'
            }
        }
        stage('build') { //Jar 만들기
            steps {
                bat '''
                    echo 'start bootJar'
                    ./gradlew clean bootJar
                '''
            }
        }
        //이미지 만들고
        stage('Build Image') {
            steps {
                echo 'Build Docker'
                script {
                    // Windows 환경에서는 Docker 데몬 접근 권한 설정을 생략
                    dockerImage = docker.build("${repository}")
                }
            }
        }
        //이미지 Dockerhub에 푸시
        stage('Push Dockerhub') {
            steps {
                echo 'Push to Dockerhub'
                script {
                    docker.withRegistry( '', registryCredential) {
                        dockerImage.push("${currentBuild.number}") //이미지명:currentBuild.number로 도커 허브에 push됨.
                    }
                }
            }
        }
        stage('github clone Yaml repository') {
            steps {
                echo 'Clone Github Repository'
                git branch: 'main', credentialsId: 'github-username-pw', url: 'https://github.com/Choidongjun0830/graduationWorkYaml'
            }
        }
        stage('Update deploy.yaml') {
            steps {
                script {
                    IMAGE_TAG = "${currentBuild.number}"  // Jenkins 빌드 번호를 이미지 태그로 사용
                    echo "image tag: ${IMAGE_TAG}"
                    bat "powershell -command \"(Get-Content insurance-deployment.yaml) -replace 'image: .*', 'image: docker.io/choidongjun/graduationwork:${IMAGE_TAG}' | Set-Content insurance-deployment.yaml\""
                }
            }
        }
        stage("Push to Git Repository") {
            steps {
                withCredentials([string(credentialsId: 'github-token-dongjun', variable: 'GITHUB_TOKEN')]) {
                    bat """
                        git config user.email "adong0808@naver.com"
                        git config user.name "Choidongjun0830"
                        git add .
                        git commit -m "Update image tag in insurance-deployment.yaml"
                        git push https://${GITHUB_TOKEN}@github.com/Choidongjun0830/graduationWorkYaml.git HEAD:main
                    """
                }
            }
        }

    }
}
728x90