๐Ÿ™‚0802[CI/CD-AWS codecommit,Jenkins]

๋ง์ง€ยท2022๋…„ 8์›” 2์ผ
0

๐Ÿ“Œ docker host

์ด์ „์— ์ƒ์„ฑํ•ด๋‘๊ณ  ์ค‘์ง€ํ•œ gitlab์„œ๋ฒ„(GCP), jenkins/tomcat์„œ๋ฒ„(aws) ์‹œ์ž‘ํ•˜๊ณ  ์ง„ํ–‰.

๐Ÿ“™ docker host ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ๋ฐ ๋ ˆ์ฝ”๋“œ ์ƒ์„ฑ (AWS)

โœ”๏ธ ์ด๋ฆ„ : docker-host
โœ”๏ธ ์ด๋ฏธ์ง€ : amazon linux 2
โœ”๏ธ ์œ ํ˜• : t2.micro
โœ”๏ธ ํ‚คํŽ˜์–ด ์„ ํƒ
โœ”๏ธ ๋„คํŠธ์›Œํฌ : MY-VPC, PUB2C
โœ”๏ธ ๋ณด์•ˆ๊ทธ๋ฃน : DEV-SG
โœ”๏ธ ์‚ฌ์šฉ์ž๋ฐ์ดํ„ฐ :

#!/bin/bash
timedatectl set-timezone Asia/Seoul
hostnamectl set-hostname docker-host
amazon-linux-extras install docker -y
systemctl enable --now docker
curl https://raw.githubusercontent.com/docker/docker-ce/master/components/cli/contrib/completion/bash/docker -o /etc/bash_completion.d/docker.sh
usermod -a -G docker ec2-user

โœ”๏ธ IP๋งตํ•‘

๐Ÿ“™ jenkins, docker ํ†ตํ•ฉ

docker host, jenkins server mobaxtermํ†ตํ•ด CLIํ™˜๊ฒฝ ์ง„์ž….

โœ”๏ธ docker ์„ค์น˜ ์ž˜ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ

[ec2-user@docker-host ~]$ docker version

โœ”๏ธ docker host์—์„œ ๋ช…๋ น์–ด๋กœ ํ†ตํ•ฉํ•˜๊ธฐ (1)

sudo passwd ec2-user ##ํ‚ค ๋Œ€์‹  ํŒจ์Šค์›Œ๋“œ๋กœ jenkins์™€ ์—ฐ๊ฒฐ๋  ์ˆ˜ ์žˆ๋„๋ก ํŒจ์Šค์›Œ๋“œ ์„ค์ •
sudo vi /etc/ssh/sshd_config
PasswordAuthentication yes
#PermitEmptyPasswords no
#PasswordAuthentication no
sudo systemctl restart sshd

->์„ธ์…˜ ๋Š์—ˆ๋‹ค๊ฐ€ ์ ‘์†ํ•˜๋ฉด ํ‚ค ์—†์ด ํŒจ์Šค์›Œ๋“œ๋กœ ์ ‘์† ๊ฐ€๋Šฅ.

โœ”๏ธ docker host์—์„œ ๋ช…๋ น์–ด๋กœ ํ†ตํ•ฉํ•˜๊ธฐ (2)

sudo mkdir /opt/docker

sudo vi /opt/docker/Dockerfile
FROM tomcat:9
RUN cp -R /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps ##์ปจํ…Œ์ด๋„ˆ ์•ˆ์ชฝ์—์„œ ๋˜๋‹ค๋ฅธ ์ปจํ…Œ์ด๋„ˆ ๊ฒฝ๋กœ๋กœ ๋ณด๋‚ด๊ธฐ ;
COPY ./*.war /usr/local/tomcat/webapps ##๋„์ปค ํ˜ธ์ŠคํŠธ์— ์žˆ๋Š” ๊ฒƒ์„ ์ปจํ…Œ์ด๋„ˆ ์•ˆ์ชฝ์œผ๋กœ ์นดํ”ผ.

sudo chown -R ec2-user:ec2-user /opt/docker/

โœ”๏ธ RUN cp -R /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps ์ด์œ 

[ec2-user@docker-host ~]$ docker run -d -p 8080:8080 --name test-tomcat tomcat:9
 ## dockerport:tomcatport

[ec2-user@docker-host ~]$ docker ps
CONTAINER ID   IMAGE      COMMAND             CREATED          STATUS          PORTS                                       NAMES
5602886b782b   tomcat:9   "catalina.sh run"   15 seconds ago   Up 14 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   test-tomcat

์•ˆ์— ๋‚ด์šฉ๋ฌผ์ด ์—†์–ด์„œ ํ™”๋ฉด์ด ์•ˆ ๋œธ.

[ec2-user@docker-host ~]$ docker exec -it test-tomcat bash
root@5602886b782b:/usr/local/tomcat# ls
bin           conf             lib      logs            NOTICE     RELEASE-NOTES  temp     webapps.dist
BUILDING.txt  CONTRIBUTING.md  LICENSE  native-jni-lib  README.md  RUNNING.txt    webapps

root@5602886b782b:/usr/local/tomcat# cp -R webapps.dist/* webapps

dist์— ์žˆ๋Š” ๋‚ด์šฉ์„ webappsํด๋”๋กœ ์˜ฎ๊ฒจ์ฃผ๋ฉด ์ฒซ ํŽ˜์ด์ง€๊ฐ€ ๊นจ์ง€๋Š” ๊ฒƒ์„ ๋ง‰์„ ์ˆ˜ ์žˆ์Œ.

[ec2-user@docker-host ~]$ docker rm -f test-tomcat
test-tomcat

ํ…Œ์ŠคํŠธ ๋งˆ๋ฌด๋ฆฌ

โœ”๏ธ jenkins๋„๋ฉ”์ธ:8080์œผ๋กœ ์ง„์ž… - ๋กœ๊ทธ์ธ ํ›„ Jenkins ๊ด€๋ฆฌ - ํ”Œ๋Ÿฌ๊ทธ์ธ ๊ด€๋ฆฌ - ์„ค์น˜ ๊ฐ€๋Šฅ - publish over ssh๊ฒ€์ƒ‰ - ์ฒดํฌํ•˜๊ณ  install without restart

โœ”๏ธ jenkins๊ด€๋ฆฌ - ์‹œ์Šคํ…œ ์„ค์ • - ๋งจ ํ•˜๋‹จ SSH Servers - ์ถ”๊ฐ€ - name: docker-host ; hostname:docker.lovemj.shop ; username:ec-user ๊ณ ๊ธ‰ - Use password authentication, or use a different key ์ฒดํฌ ๋ฐ•์Šค ์ฒดํฌ ; Password์นธ์— password ์ž…๋ ฅ, ๋งจ ํ•˜๋‹จ์— Test configuration ํด๋ฆญ, Success ํ™•์ธ. - apply,์ €์žฅ


โœ”๏ธ jenkins dashboard - ์‚ฌ๋กœ์šด ์•„์ดํ…œ - ์ด๋ฆ„ : BuildAndDeployOnContainer - copyfrom : BuildAndDeployjob - ๋‚˜๋จธ์ง€ ๊ทธ๋Œ€๋กœ (gitlab์˜ฌ๋ผ์™€์žˆ๋‹ค๋ฉด , ์•ˆ์˜ฌ๋ ธ์œผ๋ฉด github๋กœ ๊ฒฝ๋กœ ๋ฐ”๊พธ๊ณ ) ๋งจ ํ•˜๋‹จ ๋นŒ๋“œ ํ›„ ์กฐ์น˜ ๊ธฐ์กด ๊ฒƒ ์‚ญ์ œํ•˜๊ณ  send build artifacts over SSH ์„ ํƒ - SSH server name; docker-host - source files; webapp/target/*.war - remove prefix; webapp/target - exec command ;

cd /opt/docker;
docker build -t mj030kk/mytomcat:v1.0 .;
docker rm -f docker-container;
docker run -d -p 8080:8080 --name docker-container mj030kk/mytomcat:v1.0
  • apply, ์ €์žฅ
    ๐Ÿ“ข docker rm -f docker-container;์˜ ์ด์œ  ; ๋‹ค์‹œ ๋นŒ๋“œ ๋  ๋•Œ ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„๊ณผ ํฌํŠธ๊ฐ€ ์ค‘๋ณต๋˜๋ฏ€๋กœ ์ด์ „์— ์ƒ์„ฑ๋œ ๊ฒƒ์„ ์ง€์›Œ์ค˜์•ผํ•จ.

โœ”๏ธ๋Œ€์‹œ๋ณด๋“œ - ์ง€๊ธˆ ๋นŒ๋“œ ํ›„ docker.lovemj.shop:8080/webapp/์œผ๋กœ ์ง„์ž…

๐Ÿ“™ ํŽ˜์ด์ง€ ์ˆ˜์ • (์†Œ์Šค์ฝ”๋“œ ๋ณ€๊ฒฝ) git-bash

r2com@DESKTOP-DD3FU43 MINGW64 ~
$ rm -rf dev-site/

r2com@DESKTOP-DD3FU43 MINGW64 ~
$ git clone -b master http://gitlab.lovemj.shop/mangji/dev-site.git

r2com@DESKTOP-DD3FU43 MINGW64 ~
$ cd dev-site

r2com@DESKTOP-DD3FU43 MINGW64 ~/dev-site (master)
$ cd webapp/src/main/webapp/

r2com@DESKTOP-DD3FU43 MINGW64 ~/dev-site/webapp/src/main/webapp (master)
$ vi index.jsp
->๋ฌธ๊ตฌ ์ˆ˜์ •

r2com@DESKTOP-DD3FU43 MINGW64 ~/dev-site/webapp/src/main/webapp (master)
$ git add index.jsp

r2com@DESKTOP-DD3FU43 MINGW64 ~/dev-site/webapp/src/main/webapp (master)
$ git commit -m "modify index.jsp"
[master 8164efb] modify index.jsp
 1 file changed, 1 insertion(+), 1 deletion(-)


r2com@DESKTOP-DD3FU43 MINGW64 ~/dev-site/webapp/src/main/webapp (master)
$ git push -uf origin master

๐Ÿ“Œ aws code commit

๐Ÿ“™ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ์ƒ์„ฑ

โœ”๏ธ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ - ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ์ƒ์„ฑ - ์ด๋ฆ„ : git-test - ์ƒ์„ฑ

๐Ÿ“™ IAM

โœ”๏ธ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด๋‘” docker ์‚ฌ์šฉ์ž ํ™•์ธ ๋ฐ ์ง„์ž…
โœ”๏ธ ๋ณด์•ˆ์ž๊ฒฉ์ฆ๋ช… ํด๋ฆญ - AWS CodeCommit์— ๋Œ€ํ•œ HTTPS Git ์ž๊ฒฉ ์ฆ๋ช… - ์ž๊ฒฉ์ฆ๋ช… ์ƒ์„ฑ

์ž๊ฒฉ์ฆ๋ช… ๋‹ค์šด๋กœ๋“œ, ๋น„๋ฐ€๋ฒˆํ˜ธ ํ‘œ์‹œ

๐Ÿ“™ gitbash์—์„œ ๋ฆฌํฌ ๊ฐ€์ ธ์˜ค๊ธฐ

ํด๋ฆฝ๋ณด๋“œ์— ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ์ฃผ์†Œ HTTPS ๋ณต์ œ ํ›„ git bash์—์„œ ํด๋ก  ์ง„ํ–‰.

r2com@DESKTOP-DD3FU43 MINGW64 ~
$ git clone https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/git-test
Cloning into 'git-test'...
warning: You appear to have cloned an empty repository.

iam์—์„œ ๋„์›Œ๋‘” ID์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ ๋„ฃ์–ด์„œ ์ž๊ฒฉ์ฆ๋ช…์™„๋ฃŒ.

๐Ÿ“™ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์— ํŒŒ์ผ ๋„ฃ๊ธฐ

r2com@DESKTOP-DD3FU43 MINGW64 ~
$ cd git-test


r2com@DESKTOP-DD3FU43 MINGW64 ~/git-test (master)
$ ls -al
total 20
drwxr-xr-x 1 r2com 197121 0 Aug  2 11:47 ./
drwxr-xr-x 1 r2com 197121 0 Aug  2 11:47 ../
drwxr-xr-x 1 r2com 197121 0 Aug  2 12:06 .git/

-> .git/ ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์€ init , config ํ•  ํ•„์š” ์—†๋‹ค๋Š” ๊ฒƒ.

r2com@DESKTOP-DD3FU43 MINGW64 ~/git-test (master)
$ echo "Hello World" > README.txt

r2com@DESKTOP-DD3FU43 MINGW64 ~/git-test (master)
$ git add README.txt
warning: in the working copy of 'README.txt', LF will be replaced by CRLF the next time Git touches it

r2com@DESKTOP-DD3FU43 MINGW64 ~/git-test (master)
$ git commit -m "add site"
[master (root-commit) 0d03f92] add site
 1 file changed, 1 insertion(+)
 create mode 100644 README.txt

r2com@DESKTOP-DD3FU43 MINGW64 ~/git-test (master)
$ git remote
origin

r2com@DESKTOP-DD3FU43 MINGW64 ~/git-test (master)
$ git push origin master

๐Ÿ“™ ์›๊ฒฉ์ €์žฅ์†Œ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ gitlab->codecommit

gitbash์—์„œ ์ง„ํ–‰

mkdir git-migration
git clone --mirror http://gitlab.lovemj.shop/mangji/dev-site.git git-migration ##git lab ์ฃผ์†Œ
cd git-migration/
git push -uf https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/git-test --all ## codedommit ๋ฆฌํฌ์ง€ ์ฃผ์†Œ

๐Ÿ“™ ์  ํ‚จ์Šค์—์„œ ๋นŒ๋“œ

โœ”๏ธ ๋Œ€์‹œ๋ณด๋“œ - newitem - ์ด๋ฆ„: BuildAndDeployOnContainerWithCodecommit - copy from : BuildAndDeployOnContainer - OK

โœ”๏ธ ์„ค๋ช… : ์ฝ”๋“œ ์ปค๋ฐ‹์—์„œ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๊ณ  ๋ฉ”์ด๋ธ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๋นŒ๋“œํ•˜๊ณ  ๋„์ปค ํ˜ธ์ŠคํŠธ๋กœ ๋ฐฐํฌํ•˜์ž

โœ”๏ธ์†Œ์Šค์ฝ”๋“œ ๊ด€๋ฆฌ ; ๋ฆฌํฌ์ง€ํ† ๋ฆฌ URL : https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/git-test
credential ;
Add(Jenkins)
kind:username with password
username, password ; IAM์ •๋ณด์—์„œ ๊ฐ€์ ธ์˜ค๊ธฐ - Add

โœ”๏ธ๋งŒ๋“  ํฌ๋ฆฌ๋ด์…œ ์„ ํƒํ•˜๊ณ  apply,์ €์žฅ

โœ”๏ธ๋นŒ๋“œ์™„๋ฃŒ!

๐Ÿ“™ ์†Œ์Šค์ฝ”๋“œ ์—…๋ฐ์ดํŠธ

r2com@DESKTOP-DD3FU43 MINGW64 ~/git-test (master)
$ cd ~

r2com@DESKTOP-DD3FU43 MINGW64 ~
$ rm -rf git-test/

r2com@DESKTOP-DD3FU43 MINGW64 ~
$ git clone https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/git-test

r2com@DESKTOP-DD3FU43 MINGW64 ~
$ cd git-test/

r2com@DESKTOP-DD3FU43 MINGW64 ~/git-test (master)
$ vi webapp/src/main/webapp/index.jsp

r2com@DESKTOP-DD3FU43 MINGW64 ~/git-test (master)
$ git add .

r2com@DESKTOP-DD3FU43 MINGW64 ~/git-test (master)
$ git commit -m "codecommit"
[master ce14734] codecommit
 1 file changed, 1 insertion(+), 1 deletion(-)

r2com@DESKTOP-DD3FU43 MINGW64 ~/git-test (master)
$ git push -uf origin master

โœ”๏ธ ์ง€๊ธˆ ๋นŒ๋“œ ํ›„ ํ™•์ธ

profile
๊พธ์ค€ํžˆ, ์ฐจ๊ทผ์ฐจ๊ทผ

0๊ฐœ์˜ ๋Œ“๊ธ€