๋ชฉ์
์์ค์ฝ๋๋ฅผ github์ ๋ฐ์ & Local ์๋ฒ(Mac OS) ๋ด docker ์ ๋ฐฐํฌํด๋ณด๊ธฐ (+dockerHub์๋ ๋ฐ์)
โฃ jenkins pipeline ์ด์ฉ
โญ๏ธ https://www.youtube.com/watch?v=PKcGy9oPVXg ์ ๋ณด๊ณ ์ค์ตํ๋ฉด์ ์ ๋ฆฌํด๋ด! โญ๏ธ
์์
์์
1๏ธโฃ ํ๋ก์ ํธ ์
ํ
๋ฐ github push
1-1. ํ๋ก์ ํธ ์์ฑ
โฃ ๋์ ๊ฒฝ์ฐ, spring Initializr or https://start.spring.io์ผ๋ก ํ๋ก์ ํธ ์์ฑ
# DockerFile
FROM openjdk:11
EXPOSE 8081
ADD build/libs/cicd-study.jar cicd-study.jar
ENTRYPOINT ["java", "-jar", "/cicd-study.jar"]
...
# application.yml
server:
port: 8081
...
@RestController
@SpringBootApplication
public class MainApplication {
@GetMapping
public String message() {
return "HELLO !!!!!!!!!!";
}
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
1-2. github ํ๋ก์ ํธ ์์ฑ ๋ฐ push
git repository ์์ฑ
terminal ์ [...or create a new repository on the command line] ์คํ
$ git init
$ add .
$ git commit -m "first commit"
$ git branch -M main
$ git remote add origin git@github.com:mumgmangmange/test2.git
$ push -u origin main
๋ง์ฝ, git add . ๊ฐ ์๋๊ฑฐ๋ , ./gradlew clean ์คํ
โป gradle or gradlew ์ฌ์ฉ์ ์ฐจ์ด?
Wrapper๋ ์ ์ธ๋ ๋ฒ์ ์ Gradle์ ํธ์ถํ๊ณ ํ์์ ๋ฐ๋ผ ๋ฏธ๋ฆฌ ๋ค์ด๋ก๋ํ๋ ์คํฌ๋ฆฝํธ์ด๋ค.
์์
ํ๊ฒฝ ๋ด ๋ค๋ฅธ๋ฒ์ ์ gradle์ด ์๋๋ผ๋ ๋ฐ๋ก ์ค์น๊ฐ ํ์ ์์ (์๊ฐ ๋ฐ ๋ ์ ์ฝ ๊ฐ๋ฅ)
http://daplus.net/gradle-gradlew%EC%99%80-gradle-%EC%82%AC%EC%9A%A9%EC%9D%98-%EC%B0%A8%EC%9D%B4%EC%A0%90/
2๏ธโฃ docker ์คํ (์๋ฒ ๋ด ์์ผ๋ฉด ์ค์น ํ์)
Install Docker Desktop on Mac
docs.docker.com
...์นฉ์
ํ์ธ (์ธํ
์นฉ,,๋งฅ์นฉ,,)
์ค์น ์๋ฃํ๋ฉด, docker-compose ๋ฐ docker version ์ค์น๋จ
3๏ธโฃ jenkins ์คํ (์๋ฒ ๋ด ์์ผ๋ฉด ์ค์น ํ์)
โฃ ๋๋ brew ์ผ๋ก ์ค์นํจ
โฃ ๊ทธ๋ฆฌ๊ณ jenkins ๊ณ์ ์์ฑ ๊ด๋ จ โ ํด๋น ๋งํฌ ๋ด 2-5 ํ์ธ
-- jenkins ์ค์น
$ brew install jenkins
-- jenkins ์คํ
$ brew services start jenkins
4๏ธโฃ jenkins ํ๋ก์ ํธ ์
ํ
๋ฐ ๋ฐฐํฌํ๊ฒฝ ์ค์
โโโ ํ์ดํ๋ผ์ธ ์คํฌ๋ฆฝํธ ๋ด์ฉ โโโ
pipeline {
agent any
stages {
stage('Build Gradle') { // github ์ ์์คํ์ผ ๋ถ๋ฌ์ค๊ณ , build clean ๋ฐ build
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[url: 'git@github.com:xxxxx/xxx.git']]])
sh './gradlew clean build'
}
}
stage('Build docker image') { // ๋์ปค ์ด๋ฏธ์ง ๋น๋
steps {
// withEnv ๋ธ๋ก ๋ด ํ๊ฒฝ๋ณ์ ์ค์ ๊ฐ๋ฅํ๋๋ก ํจ
withEnv(['PATH+EXTRA=/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin']) {
sh 'docker rmi -f ๋์ปค์ด๋ฏธ์ง๋ช
[:TAG]'
sh 'docker build -t ๋์ปค์ด๋ฏธ์ง๋ช
[:TAG] .'
}
}
}
stage('Push image to Hub') { // docker hub ์ image ํธ์
steps {
withEnv(['PATH+EXTRA=/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin']) { // /usr/local/bin ์ฌ์ฉ ์ํด withEnv ์ฌ์ฉ
withCredentials([string(credentialsId: 'docker๊ณ์ ๋ช
', variable: 'dockerpwd')]) {
sh 'docker login -u ๋์ปค๊ณ์ ์์ด๋ -p ${dockerpwd}'
}
sh 'docker push [OPTIONS] ๋์ปค์ด๋ฏธ์ง๋ช
[:TAG]'
}
}
}
stage('Image Execute') { // ์ด๋ฏธ์ง ์ปจํ
์ด๋ ์คํ
steps {
withEnv(['PATH+EXTRA=/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin']) {
sh '''docker run --name cicd-study \
-v //var/run/docker.sock:/var/run/docker.sock \
--privileged \
-p 8081:8081 \
-d \
๋์ปค์ด๋ฏธ์ง๋ช
[:TAG]'''
}
}
}
}
}
โ๏ธโ๏ธ dockerHub ์
๋ก๋ ์๋ตํ๋ ค๋ฉด ํ์ดํ๋ผ์ธ ์์ค ๊ด๋ จ ์ฃผ์์ฒ๋ฆฌํด๋๋ ๋ฏ!
๊ทธ๋ ์ง ์๋ค๋ฉด, https://hub.docker.com/settings/security?generateToken=true ์ ์ ๋ฐ accessToken ์์ฑํด๋๊ณ , jenkins credentials ์ ๋ฑ๋ก
https://hub.docker.com/settings/security?generateToken=true
http://jenkins์ฃผ์/credentials/store/system/domain/_/newCredentials ์ ์ ๋ฐ secret ์ accessToken ๋ฑ๋ก
5๏ธโฃ ๋น๋ ๋ฐ ๋ฐฐํฌ ์ฑ๊ณต
jenkins
docker container
docker hub
๋์ปค ์ปจํ
์ด๋ ๋ด ๋ฐฐํฌ ์ ์ฉ ๊ฑด ํธ์ถ (port 8081 ์ผ๋ก)
์ฐธ๊ณ
https://bcp0109.tistory.com/352
https://chinsun9.github.io/2020/10/22/denied-requested-access-to-the-resource-is-denied/
https://i5i5.tistory.com/527
https://www.inflearn.com/questions/229989
https://m.blog.naver.com/wideeyed/221533279838
https://unix.stackexchange.com/questions/4186/what-is-usr-local-bin
https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=special9486&logNo=220274932377