Cloud Solution Architect/CI CD

Jenkins, Gitlab

YunHyeong 2023. 6. 13. 20:15

0. 전체 실습

 

1. git / git-hub

- window pc에 git 설치

 

- CI/CD 파이프라인 단계 설명

- Git / Git hub 설명

- Jenkins 설명

 

- CD의 구분

- Continuous Delevery와 Continuous Deployment의 구분

- Continuous Delevery : Manual(수동적), 소프트웨어 개발 및 배포 프로세스를 자동화하고, 빌드, 테스트, 배포 등의 작업을 자동으로 실행하여 개발자와 운영팀이 소프트웨어를 더욱 신속하고 안정적으로 전달
- Continuous Deployment: Automated(자동적), 확장 개념으로, 빌드, 테스트, 배포 과정을 자동화하여 개발자가 신속하게 코드를 배포할 수 있는 환경을 구축

- CI / CD의 CD는  Continuous Deployment

 

--- Git을 활용한 GitHub에 소스코드 Push

$ mkdir git-test && cd $_
$ echo "Hello World" >  README.txt
$ git init # Git 로컬 저장소를 생성, .git 디렉토리가 생성됨.
Initialized empty Git repository in /root/git-test/.git/ # 로컬 저장소

$ ls -al
$ git config --global user.email "test@example.com" # 버전 관리를 위해 내 정보 설정
$ git config --global user.name "johnlee"

$ git add README.txt # 파일을 스테이지에 올리는 명령어, 스테이징 영역은 커밋할 준비가 된 변경 내용이 로컬 저장소에 기록되기 전에 대기하는 장소
$ git status
$ git commit -m "add site" # 변경 사항을 로컬 저장소에 저장하는 명령어
[master (root-commit) ee0b2d0] add site
 1 file changed, 1 insertion(+)
 create mode 100644 README.txt

- 커밋이 된 모습

- Git 설치 후 git-bash에서 진행

$ git log
commit ee0b2d0c2c7f874e7e7ed63d42a10ecc87cbac73
Author: johnlee <test@example.com>
Date:   Mon Jan 31 10:45:36 2022 +0900

    add site
$ echo "Aloha" >> README.txt
$ git add README.txt
$ git status
$ git commit -m "add update"
[master 62b4357] add update
 1 file changed, 1 insertion(+)

$ git log
commit 62b435702d0f51da2c85ac805ca9b10923ca8854 # 커밋 해시(commit hash) : 변경 사항을 검토하거나 이전 상태로 되돌릴 때 사용
Author: johnlee <test@example.com>
Date:   Mon Jan 31 10:46:06 2022 +0900

    add update

commit ee0b2d0c2c7f874e7e7ed63d42a10ecc87cbac73
Author: johnlee <test@example.com>
Date:   Mon Jan 31 10:45:36 2022 +0900

    add site

$ cat README.txt
Hello World
Aloha

- git log로 현재 상태 확인 후 README.txt 파일을 수정하여 다시 커밋

- git log로 확이하면 커밋 내역이 하나 더 생긴 것을 확인할 수 있다.

 

$ git checkout 776e5fded06e850941fd79732aa7fc005612ec4d # 커밋 복원
Note: checking out '776e5fded06e850941fd79732aa7fc005612ec4d'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 776e5fd... add site

$ cat README.txt
Hello World

- checkout으로 이전 버전으로 돌아가기

 

$ git checkout -
Previous HEAD position was 776e5fd... add site
Switched to branch 'master'

$ cat README.txt
Hello World
Aloha

- git checkout -로 바로 이전 단계로 돌아간다.

 

 

- github 자격증명 확인 및 삭제 경로

- gitlab VM 생성

 

* gitlab VM에서 순서대로 진행(github 계정에 맞게 수정 필요)

 

---GitHub 원격저장소 커밋

$ git remote add origin https://github.com/hali-linux/test-dev.git
$ git push origin master
Username for 'github_id':
Password for 'token':

 

---GitHub 원격저장소의 커밋을 로컬저장소에 내려받기

$ git clone https://github.com/Hyeong-Yun/test-dev.git
$ cd test-dev/
$ git config --global user.email "test@centos.com" # 버전 관리를 위해 내 정보 설정
$ git config --global user.name "centos"
$ cat README.txt
$ echo "NIHAO" >> README.txt
$ git add README.txt
$ git commit -m "add list"
$ git push origin master
Username for 'https://github.com':
Password for 'https://hali-linux@github.com':

---원격저장소의 새로운 커밋을 로컬저장소에 갱신하기

$ cat README.txt
$ git pull origin master
$ cat README.txt

--- Git Lab 설치 (CentOS7) - 프라이빗 git 원격 저장소 구축

$ curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
$ EXTERNAL_URL="http://192.168.2.212" yum install -y gitlab-ce
$ cat /etc/gitlab/initial_root_password # 패스워드 수정

- VM의 외부 아이피로 접근하여 root 계정으로 접근한다.

- cat /etc/gitlab/initial_root_password

- preference -> password에서 비밀 번호 수정

- 재설정한 비밀번호로 로그인한다.

 

 

- 프로젝트 생성

$ mkdir gitlab-test && cd $_
$ echo "gitlab-Hello World" >  README.txt
$ git init
$ ls -al
$ git config --global user.email "gitlab-test@example.com" # 버전 관리를 위해 내 정보 설정
$ git config --global user.name "hyeongyun"
$ git add README.txt

$ ls -al
$ git add .
$ git status
$ git commit -m "gitlab new site"
$ git remote add origin http://192.168.2.212/root/my-dev.git
$ git push origin master

- aws.tar 파일을 gitlab-test 경로에 압축을 푼다.

- gitlab 원격 저장소에 푸시

- centos에서 gitlab의 master 브랜치를 clone한다

- index.html을 변경후 add -> commit -> push 과정 진행

- 해당 브랜치에 index.html 파일을 들어가면 수정되어 있는 걸 볼 수 있다.

 

- 다시 git-bash로 돌아와서 master 브랜치 클론

- index.html 파일이 변경된걸 확인할 수 있다.

 

--- 원격 저장소 마이그레이션(Github에서 Gitlab으로 마이그레이션)

 

- my-gitlab 프로젝트 생성

$ mkdir git-migration
$ git clone --mirror https://github.com/Hyeong-Yun/test-dev.git git-migration
$ cd git-migration/
$ git push -uf http://192.168.2.212/root/my-gitlab.git --all

- git-bash에서 실행

- 저장소를 통째로 폴더하나에 담아서 옮기는 느낌

- " [remote rejected] master -> master (pre-receive hook declined)" 에러시 프로젝트 셋팅에서 protected branches를 unproteced로 변경해준다.

2. Jenkins

--- 젠킨스 설치

$ sudo su -
# timedatectl set-timezone Asia/Seoul
# yum update -y
# wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
# rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
# amazon-linux-extras install epel -y
# amazon-linux-extras install -y java-openjdk11
# yum install -y jenkins
# systemctl enable --now jenkins
# cat /var/lib/jenkins/secrets/initialAdminPassword # 패스워드 수정

 

---AWS VM 생성

#!/bin/bash
timedatectl set-timezone Asia/Seoul
yum install -y httpd git
systemctl enable --now httpd
cd /var/www/html
git clone https://github.com/Hyeong-Yun/test-dev.git
git checkout master
mv test-dev/* .

- 사용자 데이터를 정의하면서 VM을 만든다.

- 그리고 Route53 서비스에서 VM 주소로 레코드를 생성한다.

- jenkins 접속을 위해 보안그룹에서 8080 포트를 열어준다. (젠킨스의 기본 포트가 8080이기 때문에)

 

cat /var/lib/jenkins/secrets/initialAdminPassword
3fba3fee73514cd18e3e153a976f057d

- 위의 명령어를 통하여 비밀번호를 확인한다.

 

- Route53 서비스의 레코드 주소의 8080포트로 접근하여 비밀번호를 입력한다.

- 오른쪽 x버튼을 클릭하여 탭을 바로 넘긴다.

- start using jenkins 버튼 클릭

- 다음 항목에서 패스워드를 수정한다.

- 수정한 패스워드로 로그인

- Create a job 버튼 누르고 아이템 이름 정하기

- Freestyl project 버튼을 선택하고 ok 버튼을 누른다.

 

- shell 명령어를 입력한다.

- 지금 빌드 버튼을 누르고 빌드된 항목을 확인한다.

'Cloud Solution Architect > CI CD' 카테고리의 다른 글

jenkins, tomcat  (0) 2023.06.14