aws 인스턴스 실행
인스턴스 보안 규칙 포트 설정
접속
도커 다운로드
도커에서 jenkins image pull
컨테이너 생성 및 실행
sudo docker run --name jenkins-docker -d -p 8100:8080 -v /home/ubuntu/jenkins_home:/var/jenkins_home --name jenkins -v /var/run/docker.sock:/var/run/docker.sock -u root jenkins/jenkins
jenkins 관리 --> system -->
secret text로 해야됨
pipeline {
agent any
environment {
// Docker Hub credentials
DOCKER_HUB_CREDENTIALS_ID = 'docker'
// GitHub credentials
GITHUB_CREDENTIALS_ID = 'gihoGithub'
// Docker image details
DOCKER_IMAGE_NAME = 'rlarlgh654/web-test'
DOCKER_IMAGE_TAG = 'latest'
GIT_BRANCH = 'main'
CONTAINER_NAME = 'web-test-container'
}
stages {
stage('Checkout') {
steps {
// Checkout code from GitHub
script {
git credentialsId: env.GITHUB_CREDENTIALS_ID, url: 'https://github.com/GihoKim-DataScientist/jenkin_example.git', branch: env.GIT_BRANCH
}
}
}
stage('Build Docker Image') {
steps {
// Build Docker image
script {
dockerImage = docker.build("${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}", "-f Dockerfile .")
}
}
}
stage('Push to Docker Hub') {
steps {
// Push Docker image to Docker Hub
script {
docker.withRegistry('https://registry.hub.docker.com', env.DOCKER_HUB_CREDENTIALS_ID) {
dockerImage.push()
}
}
}
}
}
post {
success {
// Cleanup Docker image
script {
// Stop the existing container if it's running
sh "docker ps -q --filter name=${CONTAINER_NAME} | xargs -r docker stop"
// Remove the existing container
sh "docker ps -aq --filter name=${CONTAINER_NAME} | xargs -r docker rm"
sh "docker run -d -p 8080:8080 --name ${CONTAINER_NAME} ${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}"
}
}
}
}