Spring Batch 프로젝트 GCP Artifact Registry 배포(젠킨스에서 배포)
* 배치 잡을 GKE 파드 생성해서 돌리기 위한 여정 중🏃♀️
TODO
https://juyeonee826.tistory.com/185
Spring Batch 프로젝트 GCP Artifact Registry 배포(로컬에서 배포)
* 배치 잡을 GKE 파드 생성해서 돌리기 위한 여정 중🏃♀️ TODO Spring Batch 프로젝트를 Jenkins를 이용해서 GCP Artifact Registry에 배포할 것이다. 로컬에 gcloud CLI 설치 공식 문서에서는 SDK 다운로드 해
juyeonee826.tistory.com
이제 Spring batch GCP Artifact Registry 배포를 젠킨스에서 해내야한다.
- gcloud CLI 사용 가능한 jenkins로 컨테이너 다시 띄울 것이고,
- jenkins에 GCP 인증 키를 등록해야 하고,
- Jenkinsfile 생성하고,
- Jenkins에 파이프라인 프로젝트 생성
Jenkins 다시 띄우기
jenkins 컨테이너에서 gcloud CLI를 사용할 수 있도록 Google cloud SDK, python3를 설치한 Dockerfile을 다시 작성하고 Jenkins를 다시 띄웠다.
(python3는 gcloud cli 사용을 위해 필요하다고 한다.)
- Dockerfile
FROM jenkins/jenkins:lts
USER root
RUN apt-get update && \
apt-get install -y python3 python3-pip && \
ln -s /usr/bin/python3 /usr/bin/python
RUN echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && \
apt-get install -y apt-transport-https ca-certificates && \
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key --keyring /usr/share/keyrings/cloud.google.gpg add - && \
apt-get update -y && \
apt-get install google-cloud-sdk -y
USER jenkins
- 빌드 -> 실행
docker build -t jenkins-gcp .
docker run -d -p 8080:8080 -p 50000:50000 -v /var/lib/jenkins:/var/jenkins_home --name jenkins jenkins-gcp
Jenkins에 GCP 인증 키 등록
gcp registry에 이미지를 배포할 수 있는 권한을 가진 계정의 인증 키가 필요하다.
(이미지 업로드를 위해서는 artifactregistry.repositories.uploadArtifacts 권한이 필요)
- json 키 발급
GCP IAM & Admin -> 서비스 계정 -> 계정 선택 -> 키 -> 키 추가
그러면 json 파일이 다운로드 된다.
- Jenkins Credential 추가
Jenkins 관리 -> Manage Credentials -> 도메인 선택 -> Add Credentials
Secret Text 유형으로 추가
secret에 json 파일 내용을 넣어주면 된다.
Jenkinsfile 파일 작성
개발, 운영 repository를 분리해놨기 때문에 젠킨스에서 ACTIVE_PROFILE 변수를 받아 Artifact regitry 경로를 다르게 배포되도록 작성했다.
def LOCATION = "asia-northeast3"
def PROJECT_ID = "##########"
def ACTIVE_PROFILE = "${ACTIVE_PROFILE}"
def REPOSITORY = "######-${ACTIVE_PROFILE}"
def ARTIFACT_REGISTRY = "${LOCATION}-docker.pkg.dev/${PROJECT_ID}/${REPOSITORY}/image"
pipeline {
agent any
tools {
jdk 'java17'
}
stages {
stage('Pull Codes from Github') {
steps {
checkout scm
}
}
stage('Build Docker Image by Jib & Push to GCP Artifact Registry') {
steps {
withCredentials([string(credentialsId: 'gcp-key', variable: 'GOOGLE_CLOUD_KEY')]) {
writeFile file: 'key.json', text: "${GOOGLE_CLOUD_KEY}"
sh """
gcloud auth activate-service-account --key-file=key.json
gcloud auth configure-docker ${LOCATION}-docker.pkg.dev --quiet
./gradlew jib -Djib.to.image=${ARTIFACT_REGISTRY}
"""
}
}
}
}
}
./gradlew jib -Djib.to.image=${ARTIFACT_REGISTRY}
여기서 to image를 넘기기 때문에 jib 코드에 to image는 제거했다.
jib {
extraDirectories {
paths = ['build/libs']
}
from {
image = 'openjdk:17-alpine'
}
container {
entrypoint = ['java', '-jar', '#######-0.0.1-SNAPSHOT.jar']
}
}
Jenkins 파이프라인 프로젝트 생성
메모
- gcloud cli를 위해 젠킨스에 google cloud sdk plugin 설치해서 Jenkinsfile tool 적용 해봤지만 소용 없었다.
- google cloud sdk를 jenkins 서버에 직접 설치해서 glcoud path를 넘겨서 gcloud auth 인증까지 성공했지만 jib gcp 배포 단계에서 실패했다.