// ububtu 20.04
sudo apt update
sudo apt install openjdk-11-jdk
// ubuntu 18.04
sudo add-apt-repository ppa:linuxuprising/java
sudo apt update
sudo apt install oracle-java11-set-default
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt update
sudo apt install jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins
젠킨스는 기본적으로 8080포트에서 돌아감
sudo ufw allow 8080
sudo ufw status
sudo ufw allow OpenSSH
sudo ufw enable
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
젠킨스설치시 락이 걸려있는데 위의 경로에 초기 비밀번호가 저장되어있다.
ps aux | grep jenkins
sudo -u jenkins /bin/bash
mkdir /var/lib/jenkins/.ssh
cd /var/lib/jenkins/.ssh
ssh-keygen -t rsa -f /var/lib/jenkins/.ssh/id_rsa
id_rsa의 개인키과 id_rsa.pub공개키 2개가 생성된다
setting=>deploy keys에 공개키를 등록한다.
cat /var/lib/jenkins/.ssh/id_rsa.pub
cat /var/lib/jenkins/.ssh/id_rsa
키를 등록하고 비밀번호(Passphrase)도 등록한다.
대시보드=>new item=>파이프라인을 생성을 선택한다.
트리거:github hook
definition:Pipeline script from SCM
SCM:Git
Repository:깃헙레포
credentials:등록한 private key선택
Bracnhes to build:빌드할 브랜치 선택
Script Path:젠킨스 스크립트경로
// JenkinsFile
pipeline {
agent any
stages {
stage("Build") {
steps {
sh "sudo npm install"
sh "sudo npm run build"
}
}
stage("Deploy") {
steps {
script {
sh 'sudo chmod +x ./script/deploy.sh'
sh 'sudo ./script/deploy.sh'
}
}
}
}
}
// ./script/deploy.sh
pid=$(sudo lsof -ti:3400)
if [ -n "$pid" ]; then
sudo kill -9 $pid
echo "kill ${pid} process"
npm start
else
npm start
fi
// /etc/sudoers
jenkins ALL=(ALL) NOPASSWD: /usr/bin/npm install
jenkins ALL=(ALL) NOPASSWD: /usr/bin/npm run build
jenkins ALL=(ALL) NOPASSWD: /bin/rm -rf /var/www/jenkins-react-app
jenkins ALL=(ALL) NOPASSWD: /bin/cp -r
jenkins ALL=(ALL) NOPASSWD: ALL
젠킨스유저가 sudo 명령어 사용지 제약이 있기에 특정 커맨드를 허용해주거나
ALL로 모든 커맨드를 허용해준다.
http://<배포서버IP>:8080/를 Payload URL에 추가해준다.
Build now를 클릭한다.처음빌드이후 브랜치에 푸시될때마다 웹훅으로 자동 빌드가 된다.
참고자료
https://medium.com/swlh/setup-a-ci-cd-pipeline-to-automate-react-app-deployment-on-aws-ec2-82bd0c194f77
https://www.digitalocean.com/community/tutorials/how-to-install-jenkins-on-ubuntu-18-04
https://jasonvan.ca/posts/deploy-next-js-app-with-nginx-let-s-encrypt-and-pm2
https://atenea.marfeel.com/atn/marfeel-press/systems-requirements/set-up-a-reverse-proxy-configuration/create-a-reverse-proxy-and-cache-on-apache-web-server
https://taylor.callsen.me/creating-a-caching-proxy-server-with-apache/
https://steveholgado.com/nginx-for-nextjs/
https://stackoverflow.com/questions/64386737/how-to-deploy-nextjs-with-nginx
https://www.digitalocean.com/community/tutorials/how-to-configure-jenkins-with-ssl-using-an-nginx-reverse-proxy-on-ubuntu-18-04
https://medium.com/containerum/how-to-setup-ci-cd-workflow-for-node-js-apps-with-jenkins-and-kubernetes-360fd0499556