해당 작업은 Mac M프로세스에서 작업
EC2내 프로젝트 배포의 경우 3000:80포트 사용

docker 설치 및 젠킨스 컨테이너 설치 및 접속
아래 사이트에서 도커 설치
https://docs.docker.com/desktop/install/mac-install/
Install Docker Desktop on Mac
Install Docker for Mac to get started. This guide covers system requirements, where to download, and instructions on how to install and update.
docs.docker.com
이미지 다운로드
docker pull jenkins/jenkins docker pull jenkins/jenkins:lts-jdk17
Jenkins container 설치 및 실행
컨테이너 내에서 docker를 사용할 경우 호스트에 존재하는 docker socket을 사용할려고 하여 에러가 발생함 → VM의 docker.sock 을 사용할 수 있도록 마운트가 필요
docker run -d -p 8080:8080 -p 50000:50000 --name jenkins-server --restart=on-failure -v jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock jenkins/jenkins:lts-jdk17
이후 젠킨스 내 docker 동작 중 docker.not found 발생 시 아래 명령 실행
# 혹시 pipeline 중 docker.not found 발생 시 apt-get update apt-get install docker.io chmod 666 /var/run/docker.sock
Jenkins container 최초 접속
docker exec -it --user root [컨테이너 ID] /bin/bash # jenkins 초기 비밀번호 확인 # 해당 비밀번호로 최초 접속 cat /var/jenkins_home/secrets/initialAdminPassword
최초 비밀번호로 접속 및 suggeted plugin 설치

배포를 위한 Jenkins 설정
Jenkins - git 연동
git → setting → Developer Settings → token 생성

Jenkins 내 git credentials 생성
Username : git 아이디
password: token 키값
ID : Jenkins 내 사용할 별칭

Jenkins - docker 연동
도커 빌드를 위한 Dockerfile 작성
프로젝트의 root 디렉토리에 생성 및 작성
# build stage FROM node:16.13.2 as build-stage MAINTAINER jueon.jang@lotte.net WORKDIR /app COPY package*.json ./ RUN npm install COPY . . RUN npm run build # production stage FROM nginx:latest as production-stage RUN apt-get update && apt-get install -y vim COPY --from=build-stage /app/dist /usr/share/nginx/html EXPOSE 3000 CMD ["nginx", "-g", "daemon off;"]
Jenkins 내 docker pipeline 플러그인 설치

젠킨스 내 docker 사용을 위한 credentials 생성
Username : docker ID
password: docker PW
ID : Jenkins 내 docker 사용 별칭

EC2 배포 및 원격 접속을 위한 설정
SSH Pipeline Steps 플러그인 설치

젠킨스 내 EC2 연동을 위한 credentials 생성
ID : Jenkins 내 사용을 위한 별칭
Username : EC2의 사용자 이름
Private key: SSH 키값(보통 EC2 생성 시 Key값으로 사용가능)

파이프 라인 스크립트
pipeline { agent any environment { PROJECT_NAME = 'ybaf_front' // 프로젝트 명 GIT_URL = '<https://github.com/lglis/ybaf-front.git>' // git URL REMOTE_USER = 'ec2-user' // EC2 인스턴스의 사용자 이름 REMOTE_HOST = '43.202.32.99' // EC2 인스턴스의 퍼블릭 IP 주소 DOCKER_CREDENTIAL = 'docker_user' // docker Credntial ID DOCKER_IMAGE = '' DOCKER_USERNAME = '' DOCKER_PASSWORD = '' } tools { nodejs 'NodeJS' } stages { stage('Pre build') { steps { sh "node --version" sh "git --version" git branch: 'dev', credentialsId: 'github_token', url: "${GIT_URL}" } } stage('Build image') { steps { echo 'Build Docker' sh "docker -v" script { withCredentials([usernamePassword(credentialsId: "${DOCKER_CREDENTIAL}", usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) { DOCKER_USERNAME = "${DOCKER_USERNAME}" DOCKER_PASSWORD = "${DOCKER_PASSWORD}" DOCKER_IMAGE = "${DOCKER_USERNAME}/${PROJECT_NAME}:lates" } sh "docker build --platform linux/amd64 -t ${DOCKER_IMAGE} ." sh "docker inspect ${DOCKER_IMAGE}" } } } stage('Post build') { steps { echo 'Push Docker' script { try { sh "docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}" sh "docker push ${DOCKER_IMAGE}" } catch (Exception e) { currentBuild.result = 'FAILURE' // 파이프라인을 실패로 표시 error "Failed to push Docker image: ${e.message}" // 에러 메시지 출력 후 파이프라인 중단 } } } } stage('Deploy to EC2') { steps { script { def remote = [:] withCredentials([sshUserPrivateKey(credentialsId: "EC2_SSH", keyFileVariable: 'key')]) { remote.name = "Remote_EC2" remote.host = "${REMOTE_HOST}" remote.user = "${REMOTE_USER}" remote.allowAnyHosts = true remote.identityFile = key // Jenkins Credential에 등록된 SSH 키 try { echo 'Log in to docker in ec2' sshCommand remote: remote, command: "docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}" echo 'stop and remove container' sshCommand remote: remote, command: "docker stop ${PROJECT_NAME}" sshCommand remote: remote, command: "docker rm ${PROJECT_NAME}" echo 'remove docker image' sshCommand remote: remote, command: "docker rmi ${DOCKER_IMAGE}" } catch (e) { sh 'echo "fail to stop and remove container"' } sshCommand remote: remote, command: "docker pull ${DOCKER_IMAGE}" sshCommand remote: remote, command: "docker run -d --name ${PROJECT_NAME} -p 3000:80 ${DOCKER_IMAGE}" } } } } } }


'CICD' 카테고리의 다른 글
jenkins container - Spring 프로젝트 SonarQube 연동 (0) | 2024.03.10 |
---|---|
docker-compose (0) | 2023.04.08 |
Docker와 DockerFile (2) | 2023.04.08 |
[Kubernetes] Autoscaler(HPA) (0) | 2022.02.17 |
[Kubernetes] Ingress(Controller) (0) | 2022.02.08 |
댓글