$ sudo -i
$ curl -fsSL https://get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh
$ git clone https://github.com/pcmin929/sb_code
$ apt-get update
$ apt-get -y install openjdk-8-jdk
$ apt-get -y install maven
# 레포로 이동해서
$ mvn clean install
$ java -jar target/springbootApp.jar
$ apt-get -y remove openjdk*
$ apt-get -y autoremove --purge
$ apt-get autoclean
$ vi jenkins.sh
*******
#!/bin/bash
apt-get update -y
apt-get install -y openjdk-11-jdk
wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -
echo deb http://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys FCEF32E745F2C3D5
sudo apt-get -y update
sudo apt-get -y install jenkins
sed -i s/HTTP_PORT=8080/HTTP_PORT=7777/g /etc/default/jenkins
sed -i s/JENKINS_PORT=8080/JENKINS_PORT=7777/g /usr/lib/systemd/system/jenkins.service
systemctl daemon-reload
systemctl restart jenkins
systemctl enable jenkins
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
*******
$ sh jenkins.sh
FROM openjdk:8-alpine
ADD target/springbootApp.jar springbootApp.jar
EXPOSE 8085
ENTRYPOINT ["java","-jar","springbootApp.jar"]
$ docker build -t leeeuijoo/sbimage:1.0 .
$ docker run -dp 5656:8085 --name sb leeeuijoo/sbimage:1.0
$ git init
$ git remote remove origin
# git clone을 하면 origin이라는 이름의 원격저장소가 제 저장소로 잡혀있기때문에 제거.
$ git remote add origin https://github.com/Leeeuijooo/sb_code.git
# 저장소를 origin으로 등록
$ git add .
# 변경된 파일을 추가
$ git commit -m "first commit"
$ git push origin main
$ cat /var/lib/jenkins/secrets/initialAdminPassword
pipeline {
agent any
// any, none, label, node, docker, dockerfile, kubernetes
stages {
stage('Example') {
steps {
sh 'mvn clean install'
echo 'Hello World'
}
}
}
}
pipeline {
agent any
// any, none, label, node, docker, dockerfile, kubernetes
tools {
maven 'my_maven'
}
environment {
gitName = 'Leeeuijooo'
gitEmail = 'euijoo3233@gmail.com'
gitWebaddress = 'https://github.com/Leeeuijooo/sb_code.git'
gitSshaddress = 'git@github.com:Leeeuijooo/sb_code.git'
gitCredential = 'git_cre' //github credential 생성시 ID
dockerHubRegistry = 'leeeuijoo/sbimage'
}
stages {
stage('Checkout Github') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[credentialsId: gitCredential, url: gitWebaddress]]])
}
post {
failure {
echo 'Reppository clone failure'
}
success {
echo 'Reppository clone success'
}
}
}
stage('Maven Build') {
steps {
sh 'mvn clean install'
//maven 플러그인이 미리 설치되어 있어야함
}
post {
failure {
echo 'Reppository clone failure'
}
success {
echo 'Reppository clone success'
}
}
}
stage('Docker image Build') {
steps {
sh "docker build -t ${dockerHubRegistry}:${currentBuild.number} ."
sh "docker build -t ${dockerHubRegistry}:latest ."
//jang1023/sbimage:x 이런 식으로 빌드가 될 것이다.
//currentBuild.number 젠킨스에서 제공하는 빌드 수
}
post {
failure {
echo 'Reppository clone failure'
}
success {
echo 'Reppository clone success'
}
}
}
}
}
root@ip-172-31-1-88:~/sb_code# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
leeeuijoo/sbimage 6 1a7f9daea69c 50 seconds ago 126MB
leeeuijoo/sbimage latest 1a7f9daea69c 50 seconds ago 126MB
leeeuijoo/sbimage 5 691607409e2c 50 seconds ago 126MB
leeeuijoo/sbimage 1.0 dcd4bfdf105a 2 hours ago 126MB
root@ip-172-31-1-88:~/sb_code# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c212a1d937e5 leeeuijoo/sbimage:1.0 "java -jar springboo…" 2 hours ago Up 2 hours 0.0.0.0:5656->8085/tcp, :::5656->8085/tcp sb
## 빌드된 이미지로 다시 컨테이너 띄우기
root@ip-172-31-1-88:~/sb_code# docker stop c212a1d937e5
c212a1d937e5
root@ip-172-31-1-88:~/sb_code# docker rm c212a1d937e5
c212a1d937e5
root@ip-172-31-1-88:~/sb_code# docker run -dp 5656:8085 --name sb leeeuijoo/sbimage:latest
a243a016730e822faef3b766e1a02acd7b76ac58a3f6d9c1ecbb3459a47ecdfc
root@ip-172-31-1-88:~/sb_code# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a243a016730e leeeuijoo/sbimage:latest "java -jar springboo…" 3 seconds ago Up 2 seconds 0.0.0.0:5656->8085/tcp, :::5656->8085/tcp sb
$ usermod -aG docker jenkins
pipeline {
agent any
// any, none, label, node, docker, dockerfile, kubernetes
tools {
maven 'my_maven'
}
environment {
gitName = 'Leeeuijooo'
gitEmail = 'euijoo3233@gmail.com'
gitWebaddress = 'https://github.com/Leeeuijooo/sb_code.git'
gitSshaddress = 'git@github.com:Leeeuijooo/sb_code.git'
gitCredential = 'git_cre' //github credential 생성시 ID
dockerHubRegistry = 'leeeuijoo/sbimage'
dockerHubRegistryCredential = 'docker_cre'
}
stages {
stage('Checkout Github') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[credentialsId: gitCredential, url: gitWebaddress]]])
}
post {
failure {
echo 'Reppository clone failure'
}
success {
echo 'Reppository clone success'
}
}
}
stage('Maven Build') {
steps {
sh 'mvn clean install'
//maven 플러그인이 미리 설치되어 있어야함
}
post {
failure {
echo 'Maven build failure'
}
success {
echo 'Maven build success'
}
}
}
stage('Docker image Build') {
steps {
sh "docker build -t ${dockerHubRegistry}:${currentBuild.number} ."
sh "docker build -t ${dockerHubRegistry}:latest ."
//jang1023/sbimage:x 이런 식으로 빌드가 될 것이다.
//currentBuild.number 젠킨스에서 제공하는 빌드 수
}
post {
failure {
echo 'docker image build failure'
}
success {
echo 'docker image build success'
}
}
}
stage('docker image push') {
steps {
withDockerRegistry(credentialsId: dockerHubRegistryCredential, url: '') {
// withDockerRegistry : docker pipeline 플러그인 설치시 사용가능.
// dockerHubRegistryCredential : environment에서 선언한 docker_cre
sh "docker push ${dockerHubRegistry}:${currentBuild.number}"
sh "docker push ${dockerHubRegistry}:latest"
}
}
}
stage('docker container deploy') {
steps {
sh "docker rm -f sb"
sh "docker run -dp 5656:8085 --name sb ${dockerHubRegistry}:${currentBuild.number}"
}
post {
failure {
echo 'docker constainer deploy fail'
}
success {
echo 'docker container deploy success'
}
}
}
}
}
stage('Docker image Build') {
steps {
sh "docker build -t ${dockerHubRegistry}:${currentBuild.number} ."
sh "docker build -t ${dockerHubRegistry}:latest ."
//jang1023/sbimage:x 이런 식으로 빌드가 될 것이다.
//currentBuild.number 젠킨스에서 제공하는 빌드 수
}
post {
failure {
echo 'docker image build failure'
sh "docker image rm -f ${dockerHubRegistry}:${currentBuild.number}"
sh "docker image rm -f ${dockerHubRegistry}:latest "
}
success {
echo 'docker image build success'
sh "docker image rm -f ${dockerHubRegistry}:${currentBuild.number}"
sh "docker image rm -f ${dockerHubRegistry}:latest "
}
}
}
pipeline {
agent any
// any, none, label, node, docker, dockerfile, kubernetes
tools {
maven 'my_maven'
}
environment {
gitName = 'Leeeuijooo'
gitEmail = 'euijoo3233@gmail.com'
gitWebaddress = 'https://github.com/Leeeuijooo/sb_code.git'
gitSshaddress = 'git@github.com:Leeeuijooo/sb_code.git'
gitCredential = 'git_cre' //github credential 생성시 ID
dockerHubRegistry = 'leeeuijoo/sbimage'
dockerHubRegistryCredential = 'docker_cre'
}
stages {
stage('Checkout Github') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[credentialsId: gitCredential, url: gitWebaddress]]])
}
post {
failure {
echo 'Reppository clone failure'
}
success {
echo 'Reppository clone success'
}
}
}
stage('Maven Build') {
steps {
sh 'mvn clean install'
//maven 플러그인이 미리 설치되어 있어야함
}
post {
failure {
echo 'Maven build failure'
}
success {
echo 'Maven build success'
}
}
}
stage('Docker image Build') {
steps {
sh "docker build -t ${dockerHubRegistry}:${currentBuild.number} ."
sh "docker build -t ${dockerHubRegistry}:latest ."
//jang1023/sbimage:x 이런 식으로 빌드가 될 것이다.
//currentBuild.number 젠킨스에서 제공하는 빌드 수
}
post {
failure {
echo 'docker image build failure'
sh "docker image rm -f ${dockerHubRegistry}:${currentBuild.number}"
sh "docker image rm -f ${dockerHubRegistry}:latest "
}
success {
echo 'docker image build success'
}
}
}
stage('docker image push') {
steps {
withDockerRegistry(credentialsId: dockerHubRegistryCredential, url: '') {
// withDockerRegistry : docker pipeline 플러그인 설치시 사용가능.
// dockerHubRegistryCredential : environment에서 선언한 docker_cre
sh "docker push ${dockerHubRegistry}:${currentBuild.number}"
sh "docker push ${dockerHubRegistry}:latest"
}
}
}
stage('docker container deploy') {
steps {
sh "docker rm -f sb"
sh "docker run -dp 5656:8085 --name sb ${dockerHubRegistry}:${currentBuild.number}"
sh "docker image rm -f ${dockerHubRegistry}:${currentBuild.number}"
sh "docker image rm -f ${dockerHubRegistry}:latest "
}
post {
failure {
echo 'docker constainer deploy fail'
slackSend (color: '#FF0000', message: "FAILURE: docker container deployment '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
success {
echo 'docker container deploy success'
slackSend (color: '#0000FF', message: "SUCCESS: docker container deployment '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}