Obsidian/Recognition/Programing/Jenkins/Jenkins 설치.md

6.5 KiB

#jenkins

Jenkins 설치

1.Docker Image 준비

(외부인터넷 연결 + 도커 설치된 환경에서 작업)


# 폴더 생성
mkdir /opt/jenkins && 
cd jenkins &&
mkdir jenkins_home

# docker-compose 파일 생성
--------------------------------------------------------------------------------
version: '3.8'
services:
  jenkins: 
    image: jenkins/jenkins:lts
    container_name: jenkins
    user: root
#    environment:
#      - TZ=Asia/Seoul
    privileged: true
    ports:
      - 9093:8080
#      - 9000:50000
    volumes:
      - ./jenkins_home:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock
	restart: unless-stopped
--------------------------------------------------------------------------------

# Docker image 빌드 및 실행
docker-compose build && docker-compose up -d

# Jenkins 실행 확인
## Jenkins Unlock Password 확인 (Unlock 이후 파일 삭제됨)
cat ./jenkins_home/secrets/initialAdminPassword
## jenkins 페이지 접속 및 초기 설정
# 1. http://{hostname}:9093 접속
# 2. 최초1회 Unlock Password 입력하여 Unlock
# 3. 관리자 계정 생성
# 4. Jenkins URL 정보 확인 및 수정
# 5. "Start using Jenkins" 버튼클릭하여 초기설정 완료
# 6. 기본 Plugin 설치진행

# Docker image Export
ocker save -o /home/gmt/docker_data/docimg_jenkins.tar jenkins/jenkins


2.Plugin 설치

  • Dashboard > Jenkins 관리 > Plugins > Available plugins (/manage/pluginManager/available)
  • GitLab Plugin
  • Maven Integration plugin
  • Publish Over SSH
  • Publish Over FTP

3.Jenkins에 Maven 설치

  • Dashboard > Jenkins 관리 > Tools > Maven installations (/manage/configureTools/)
  • Name : 임의입력
  • Install automatically 선택
  • Version 선택후 저장 !Pasted image 20240403173424.png

4.폐쇄망 서버 이관 준비


# Docker image Export
ocker save -o /home/gmt/docker_data/docimg_jenkins.tar jenkins/jenkins

# Jenkins Plubin Export
cd /opt/jenkins/ &&
mkdir jenkins_plugins &&
cp -r jenkins_home/plugins/*.jpi jenkins_plugins/

cd jenkins_plugins/ &&
ls | grep '.jpi' | cut -d . -f 1 | while read line; do mv $line.jpi $line.hpi; done &&
tar -cvf ../plugins.tar ./

# Jenkins Tools Export (maven... )
cd /opt/jenkins/jenkins_home/
tar -cvf tools.tar tools

# 최종 이관 파일 List
- docimg_jenkins.tar
- plugins.tar
- tools.tar
- + Docker, Docker-compose 설치파일


Maven Repository 준비

Jenkins에 설치한 동일한 버전의 Maven 설치
setting.xml 작성
  • 라이브러리 저장될 폴더 상위에 setting.xml 작성

<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">

   <localRepository>D:\Workspace\Jenkins\tet_git\maven_repository</localRepository>

 
</settings>

Offline용 라이브러리 다운로드
  • 프로젝트 폴더로 이동하여 CMD 실행

# Offline에서 참조할 dependency 목록과 함께 다운로드
mvn dependency:go-offline -gs D:\Workspace\Jenkins\maven_repositorys\settings.xml

# 기본옵션으로 빌드
mvn clean install -gs D:\Workspace\Jenkins\maven_repositorys\settings.xml

# Offline옵션으로 빌드(테스트)
mvn -o clean install -gs D:\Workspace\Jenkins\maven_repositorys\settings.xml

# repository 압축
cd D:\Workspace\Jenkins
tar -cvf maven_repository.tar  .\maven_repositorys

# Jenkins서버로 압축파일 전송
scp -P 11443 maven_repository.tar root@10.200.31.42:/home/gmt/jenkins_home/

폐쇄망 Jenkins서버 구성

Jenkins CI/CD 환경 구성

1.SSH 설정

# jenkins 서버에서 key발급
docker exec -it jenkins bash
ssh-keygen

# ssh key 확인
## 경로: /root/.ssh/
## id_rsa : 비공개키
## id_rsa.pub : 공개키
cd 

# 접속 대상서버로 Key 전송
ssh-copy-id -i id_rsa.pub -p 11443 gmt@10.200.31.48

  • 비공개키 붙여넣기 Dashboard > Jenkins 관리 > System > Publish over SSH (/manage/configure) !Pasted image 20240404092425.png
  • SSH Server 추가 (key사용하여 비밀번호 없이 로그인) !Pasted image 20240404092657.png
2. Maven 설정
  • Dashboard > Jenkins 관리 > Tools > Maven Configuration (/manage/configureTools) !Pasted image 20240404092924.png
  • Dashboard > Jenkins 관리 > Tools > Maven installations !Pasted image 20240404093006.png
3.GitLab 설정
  • 3-1. GitLab에서 유저 생성( 소스 Pull 용)

    • Admin -> Users -> New user
    • Nane, Username, Email, Password만 입력 하고 나머지 기본. !Pasted image 20240404093714.png
  • 3-2. 프로젝트 Group Member추가

    • Groups -> SACP(프로젝트 Group 클릭) -> 오른쪽하단 group members에서 Manage access 버튼 클릭 !Pasted image 20240404094102.png
    • Invite members 버튼 -> 팝업창에 새로추가한 유저 선택하고 Invite 버튼 (role은 Guest여도 상관없음)
  • 3-3. Access Token 발급

    • Preferences(User Settings) -> Access Tokens
      • Token name : 임의입력
      • Select scopes : 모두 체크
      • 생성하고 화면 상단의 키 복사(이후에 다시 확인 할 수 없음)
  • 3-4. Jenkins Credential 등록

    • Dashboard > Jenkins 관리 > Credentials > Stores scoped to Jenkins (/manage/credentials)
    • Domains -> Global -> Add Credential
    • Kind, API token 입력
    • 선택1(API Token) !Pasted image 20240404095641.png
    • 선택1(Username, password) !Pasted image 20240404100125.png

Jenkins Pipline 설정

  • 소스 코드 관리 -> Git 선택
    • Repository URL, Credentials, Branch Specifier 입력
  • 빌드 유발 -> Build when a change is pushed to GitLab. GitLab webhook URL:...
    • 고급 클릭하면 맨밑에 Secret token -> Generate 버튼
    • URL과, Secret token 복사하여 GitLab에서 Webhook 설정
  • Build -> Goals and options : -o clean install -DskipTests=true
  • 빌드 후 조치 -> 추가 -> "Send build artifacts over SSH" 선택 !Pasted image 20240404100942.png

※ GitLab Webhook 설정

  1. 사전 준비
  • Admin Area -> Settings -> Network -> Outbound requests
  • Allow requests to the local network from web hooks and services 체크 ※ Webhook 설정시 (Url is blocked: Requests to the local network are not allowed) 오류 방지
  1. Webhook 설정
  • 프로젝트별로 설정해야함
  • GitLab -> 프로젝트 -> Settings -> Webhooks
  • URL, Secret token 입력
  • Add webhook 버튼