๋ชฉ์
์์ค์ฝ๋๋ฅผ github์ ๋ฐ์ & Local ์๋ฒ(Mac OS) ๋ด docker ์ ๋ฐฐํฌํด๋ณด๊ธฐ (+dockerHub์๋ ๋ฐ์)
โฃ jenkins pipeline ์ด์ฉ
์์ ์์
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 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์ด ์๋๋ผ๋ ๋ฐ๋ก ์ค์น๊ฐ ํ์ ์์ (์๊ฐ ๋ฐ ๋ ์ ์ฝ ๊ฐ๋ฅ)
2๏ธโฃ docker ์คํ (์๋ฒ ๋ด ์์ผ๋ฉด ์ค์น ํ์)
...์นฉ์ ํ์ธ (์ธํ ์นฉ,,๋งฅ์นฉ,,)
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 ์ ๋ฑ๋ก
5๏ธโฃ ๋น๋ ๋ฐ ๋ฐฐํฌ ์ฑ๊ณต
์ฐธ๊ณ
https://bcp0109.tistory.com/352
https://chinsun9.github.io/2020/10/22/denied-requested-access-to-the-resource-is-denied/
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
'๊ฐ๋ฐ > infra' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
VM(Virtual machine) & Container (feat. docker) (1) | 2022.09.04 |
---|---|
CI/CD ๋ฐ Jenkins (0) | 2022.09.04 |
[Jenkins] ์์ ๊ฒฐ๊ณผ๋ฌผ(Springboot jar file, gradle ์ด์ฉ)์ AWS EC2์ ๋ฐ์ํด๋ณด๊ธฐ (2) | 2022.05.06 |
[k8s] ์ฟ ๋ฒ๋คํฐ์ค (0) | 2022.02.16 |
[k8s] ์ปจํ ์ด๋ ์ค์ผ์คํธ๋ ์ด์ (container orchestration) (0) | 2022.02.16 |