Actualizar flujo de trabajo de despliegue para incluir la clave SSH e… #70
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: Build and Deploy | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Set up Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: 21 | |
- name: Install dependencies | |
run: npm install --legacy-peer-deps | |
- name: Create .env file | |
run: | | |
echo "STRAPI_TOKEN=${{ secrets.STRAPI_TOKEN }}" >> .env | |
echo "STRAPI_HOST=${{ secrets.STRAPI_HOST }}" >> .env | |
- name: Build Next.js app | |
run: npm run build | |
- name: Set up SSH keys | |
run: | | |
mkdir -p ~/.ssh | |
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519 | |
chmod 600 ~/.ssh/id_ed25519 | |
ssh-keyscan -H ${{ secrets.SERVER_HOST }} >> ~/.ssh/known_hosts | |
- name: Transfer files to server | |
env: | |
STRAPI_TOKEN: ${{ secrets.STRAPI_TOKEN }} | |
STRAPI_HOST: ${{ secrets.STRAPI_HOST }} | |
run: | | |
rsync -avz --delete --exclude='.next/cache' -e "ssh -i ~/.ssh/id_ed25519" ./ ${{ secrets.SERVER_HOST }}:/srv/www/html/ | |
- name: Run remote commands | |
run: | | |
ssh -i ~/.ssh/id_ed25519 ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} << 'EOF' | |
export STRAPI_TOKEN=${{ secrets.STRAPI_TOKEN }} | |
export STRAPI_HOST=${{ secrets.STRAPI_HOST }} | |
cd /srv/www/html | |
npm install | |
pm2 restart nextjs-app || pm2 start npm --name "nextjs-app" -- start | |
EOF |