CI/CD 개념 참고자료
https://www.redhat.com/ko/topics/devops/what-is-ci-cd
https://www.youtube.com/watch?v=0Emq5FypiMM&t=60s
CI/CD 구축 방법 참고자료
https://dramatic-armchair-97f.notion.site/e85a109829c94c9ea5c841ca8e852ac5
sudo docker run --name jenkins -d -p 8081:8080 -p 50000:50000 jenkins/jenkins:lts
http://[IP]:8081
를 입력해서 접속 가능하다.sudo docker logs jenkins
를 입력해서 찾을 수 있다.Name : EC2_Instance
Hostname : [EC2 domain]
Username : ubuntu
Remote Directory : /home/ubuntu/jenkins_build
sudo vim /etc/ssh/sshd_config
PubkeyAuthentication yes
PubkeyAcceptedKeyTypes +ssh-rsa
sudo service sshd restart
를 입력해서 ssh 서비스를 재시작한다.mkdir ~/jenkins_build
apt-get update
apt-get install sudo
apt-get install vim
sudo 설정, 설정 파일 열기
sudo visudo
해당 파일에 아래 내용 추가
jenkins ALL=(ALL) NOPASSWD: ALL
EC2에 접속한 후, jenkins에 접속하는 명령어를 수행한다.
sudo docker exec -it -u root jenkins /bin/bash
Jenkins에 NVM을 설치한다.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
NVM을 활성화한다. . ~/.nvm/nvm.sh
14버전을 설치한다. (프로젝트에 맞는 버전으로 설치)
nvm install 14
모든유저에게 적용한다.
n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; cp -r $n/{bin,lib,share} /usr/local
sudo apt-get install openjdk-11-jdk
https://dramatic-armchair-97f.notion.site/e85a109829c94c9ea5c841ca8e852ac5
참고자료
https://junhyunny.github.io/information/jenkins/github/jenkins-github-webhook/
참고 노션 [8. script 작성] 참고
https://dramatic-armchair-97f.notion.site/e85a109829c94c9ea5c841ca8e852ac5
실제 작성한 스크립트를 기반으로 설명할 예정.
전체 스크립트는 아래 참고
node {
stage('Build') {
checkout scmGit(branches: [[name: '*/dev']], extensions: [], userRemoteConfigs: [[credentialsId: 'comet', url: 'https://lab.ssafy.com/s08-webmobile2-sub2/S08P12C109']])
dir("front") {
sh "pwd"
sh "sudo npm install"
sh "sudo npm run build"
sh "sudo cp -r /var/jenkins_home/workspace/zzalu_ci_cd_test/front/dist/* /var/jenkins_home/workspace/zzalu_ci_cd_test/back/src/main/resources/static"
}
dir("back") {
sh "pwd"
sh "sudo chmod +x gradlew "
sh "sudo ./gradlew clean"
sh "sudo ./gradlew build"
}
}
stage('Deploy') {
dir("back/build/libs") {
sshPublisher(publishers: [sshPublisherDesc(configName: 'zzalu_deploy_server_test', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''cd /home/ubuntu/jenkins_build
kill -9 `cat save_pid.txt`
rm save_pid.txt
sudo nohup java -jar zzalu-0.0.1-SNAPSHOT.jar > logs/zzalu.log 2>&1 & echo $! > save_pid.txt''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '*.jar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true)])
mattermostSend color: '#32a852', message: "Dev Branch Deploy End! (${env.JOB_NAME}) #(${env.BUILD_NUMBER}) (<${env.BUILD_URL}|Open>) \n See the (<${env.BUILD_URL}console|console>)"
}
}
}
checkout scmGit(branches: [[name: '*/dev']], extensions: [], userRemoteConfigs: [[credentialsId: 'comet', url: 'https://lab.ssafy.com/xxxxxxxxxx/xxxxxx']])
dir("front") {
sh "pwd"
sh "sudo npm install"
sh "sudo npm run build"
sh "sudo cp -r /var/jenkins_home/workspace/zzalu_ci_cd_test/front/dist/* /var/jenkins_home/workspace/zzalu_ci_cd_test/back/src/main/resources/static"
}
sshPublisher(publishers: [sshPublisherDesc(configName: 'zzalu_deploy_server_test', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''cd /home/ubuntu/jenkins_build
kill -9 `cat save_pid.txt`
rm save_pid.txt
sudo nohup java -jar zzalu-0.0.1-SNAPSHOT.jar > logs/zzalu.log 2>&1 & echo $! > save_pid.txt''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '*.jar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true)])
mattermostSend color: '#32a852', message: "Dev Branch Deploy End! (${env.JOB_NAME}) #(${env.BUILD_NUMBER}) (<${env.BUILD_URL}|Open>) \n See the (<${env.BUILD_URL}console|console>)"
execCommand:
전송한 후 수행할 명령어들을 작성한다.위의 경우에서 전송후 특정 디렉토리로 이동 (cd),
기존 프로세스를 종료(kill -9)한 후
전송한 jar 파일을 실행(nohup)한다.