TIL
[github-actions] 프라이빗 레포지토리 CI/CD 자동배포 설정하기
기주그지마
2024. 11. 13. 21:42
1. PAT(Personal Access Token) 발급
<개인 프로필 - settings - developer settings - Personal access tokens - generate new token>
이때 토큰의 권한은 repo관련 모든 권한과 workflow 권한을 추가해둔다.
토큰은 1번만 보여주므로 다른곳에 복사해둔다.
2. Repository 권한 설정
<레포지토리 - settings - actions - general>
workflow permission 섹션에서 read and write permission 활성화
(organization의 레포지토리인 경우 어드민 권한 필요할 수 있음)
3. deploy.yml 작성
1.Checkout repository
actions/checkout@v3 액션을 사용하여 main 브랜치의 최신 코드를 self-hosted 서버로 가져옵니다. 이 단계에서 GITHUB_TOKEN을 사용해 인증이 이루어지며, 워크플로우가 리포지토리에 접근하여 코드를 복사합니다.
2.PAT 설정
git config를 사용하여 액세스 토큰(PAT)을 통한 인증을 설정합니다. 이 설정을 통해 Git이 git pull과 같은 명령을 실행할 때 토큰 인증을 사용합니다. credential.helper store를 사용하여 Git이 인증 정보를 저장하게 하고, url 설정을 통해 GitHub 주소에 자동으로 PAT가 포함되도록 합니다.
git config -l 명령어로 설정을 확인하여, 문제가 발생할 경우 이를 통해 디버깅할 수 있음
name: 오늘도리뷰 배포스크립트
on:
pull_request:
types:
- closed
branches:
- main
jobs:
deploy:
if: github.event.pull_request.merged == true
runs-on: self-hosted
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
ref: main
token: ${{ secrets.GITHUB_TOKEN }}
- name: PAT 설정
run: |
git config --global credential.helper store
git config --global url."https://${{ secrets.ACTION_TOKEN }}@github.com/".insteadOf "https://github.com/"
git config -l # 현재 설정을 확인
- name: 코드 업데이트, 재실행
run: |
echo "PR was merged"
cd /home/ubuntu/mylittlereviewduck_backend
git pull origin main
npx prisma generate
sudo pm2 restart today-review