node {
# Jenkins의 WorkDirectory에 git Clone (JenkinsFile이 있는 git)
stage('Clone') {
echo '>>> Git repository Clone'
git branch: 'main', credentialsId: 'qawesdxc@naver.com' , url: 'https://github.com/Oh-JunYoung/private_CowAPI.git'
}
# 나의 WorkDirectory로 이동 (ex. /home/ec2-user)
stage('Move to Path') {
echo '>>> Move to Deploy Path'
sh 'sudo ./jenkins/MovePath.sh'
}
# Git pull
stage('Pull') {
echo '>>> Git repository Clone'
sh 'sudo ./jenkins/GitPull.sh'
}
# Build
stage('Build') {
echo '>>> Gradle project Build'
sh 'sudo ./jenkins/Build.sh'
}
# Test
stage('Test') {
echo '>>> Gradle project Test'
sh 'sudo ./jenkins/Test.sh'
}
# 기존의 실행 중이던 server kill
stage('Kill') {
echo '>>> Gradle project Deploy'
sh 'sudo ./jenkins/KillProcess.sh'
}
# 서버 재실행
stage('Deploy') {
echo '>>> Gradle project Deploy'
sh 'sudo ./jenkins/Deploy.sh'
}
}
매번 비밀번호를 입력하지 않고 SSH로 git clone을 하고 pull, push 한다.
1. /home/ec2-user/.ssh에 rsa key 생성 (ex. id_rsa, id_rsa.pub)
2. Github -> Setting -> SSH 등록
3. vi /home/ec2-user/.ssh/config
Host github.com
AddKeysToAgent yes
User git
IdentityFile ~/.ssh/id_rsa
키 등록
eval "$(ssh-agent -s)" # agent 활성화
ssh-add -K ~/.ssh/id_rsa # 키 등록
ssh -T git@github.com # 테스트
Jenkins 권한
sudo visudo # jenkins ALL=(ALL) NOPASSWD: ALL
sudo usermod -a -G ec2-user jenkins
sudo usermod -aG jenkins $USER
cp /home/ec2-user/.ssh/id_rsa* /root/.ssh