๋ฐ˜์‘ํ˜•
๋ชฉ์ 

์†Œ์Šค์ฝ”๋“œ๋ฅผ 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

 

๋ฐ˜์‘ํ˜•

+ Recent posts