fix deployment #126
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy | |
on: | |
push: | |
branches: main | |
env: | |
VERSION: ${{ github.run_number }}-${{ github.run_attempt }}-${{ github.run_id }} | |
REPO_NAME: ${{ github.event.repository.name }} | |
jobs: | |
main: | |
runs-on: mbp-13 | |
environment: prod | |
steps: | |
- name: checkout code | |
uses: actions/checkout@v4 | |
- name: install node, update npm | |
run: | | |
\. "$HOME/.nvm/nvm.sh" | |
nvm install --lts | |
npm i -g npm | |
- name: install packages, build | |
run: | | |
\. "$HOME/.nvm/nvm.sh" | |
npm pkg delete scripts.prepare | |
npm ci | |
npm run build | |
- name: create .env | |
run: | | |
echo "ADMIN_ID=${{ secrets.ADMIN_ID }}" >> .env | |
echo "BOT_TOKEN=${{ secrets.BOT_TOKEN }}" >> .env | |
echo "LOGTAIL_TOKEN=${{ secrets.LOGTAIL_TOKEN }}" >> .env | |
echo "LOG_LEVEL=${{ secrets.LOG_LEVEL }}" >> .env | |
echo "MONGO_DB_URL=${{ secrets.MONGO_DB_URL }}" >> .env | |
echo "NODE_ENV=${{ secrets.NODE_ENV }}" >> .env | |
echo "VERSION=${{ env.VERSION }}" >> .env | |
- name: copy files | |
run: | | |
mkdir -p ~/Deployments/${{ env.REPO_NAME }}/${{ env.VERSION }} | |
cp -a . "$_" && cd "$_" | |
cd .. && echo ${{ env.VERSION }} > latest | |
- name: delete old deployments | |
run: | | |
cd ~/Deployments/${{ env.REPO_NAME }} | |
MAX_DEPLOYMENTS=3 | |
ls -td */ | tail -n +$((MAX_DEPLOYMENTS+1)) | xargs rm -rf | |
# set up pm2 startup, log rotate manually | |
- name: install, configure pm2 | |
run: | | |
\. "$HOME/.nvm/nvm.sh" | |
cd ~/Deployments/${{ env.REPO_NAME }}/${{ env.VERSION }} | |
npm i -g pm2@latest | |
pm2 update | |
pm2 stop ${{ env.REPO_NAME }} || true | |
pm2 delete ${{ env.REPO_NAME }} || true | |
pm2 start npm --name ${{ env.REPO_NAME }} --exp-backoff-restart-delay=${{ secrets.RESTART_DELAY }} -- run start | |
pm2 save |