Reset #11
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 & Deploy | |
on: | |
push: | |
branches: [ main ] | |
jobs: | |
build: | |
if: github.event_name == 'push' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Install Npm Packages | |
uses: actions/setup-node@v4 | |
with: | |
node-version: 21 | |
- run: npm i | |
- run: npm run build | |
- name: Zip | |
run: | | |
touch build.tar.gz | |
tar -zcf build.tar.gz -C ./dist . | |
- name: Archive production artifacts | |
uses: actions/upload-artifact@v4 | |
with: | |
name: build | |
path: ./build.tar.gz | |
deploy: | |
needs: build | |
runs-on: ubuntu-latest | |
env: | |
SSH_USER: github | |
SSH_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
SSH_HOST: 194.36.146.51 | |
REPO_NAME: ${{ github.event.repository.name }} | |
steps: | |
- name: Download build | |
uses: actions/download-artifact@v4 | |
with: | |
name: build | |
- name: Install ssh key | |
run: | | |
mkdir -p ~/.ssh/ | |
echo "$SSH_KEY" > ~/.ssh/staging.key | |
chmod 600 ~/.ssh/staging.key | |
ssh-keyscan -H $SSH_HOST >> ~/.ssh/known_hosts | |
- name: Configure SSH | |
run: | | |
cat >>~/.ssh/config <<END | |
Host staging | |
HostName $SSH_HOST | |
User $SSH_USER | |
IdentityFile ~/.ssh/staging.key | |
StrictHostKeyChecking no | |
END | |
- name: Check SSH Connection | |
run: ssh staging 'echo "It works!"' | |
- name: Delete previous deploy files | |
run: ssh staging "sudo rm -r /home/$SSH_USER/$REPO_NAME/*" | |
continue-on-error: true | |
- name: Deploy build | |
run: rsync -avz -e 'ssh -i ~/.ssh/staging.key -o StrictHostKeyChecking=no' --progress ./build.tar.gz $SSH_USER@$SSH_HOST:/home/$SSH_USER/$REPO_NAME | |
- name: Unzip | |
run: ssh staging "cd /home/$SSH_USER/$REPO_NAME/ && sudo tar --no-same-owner --no-same-permissions -xf build.tar.gz" | |
- name: Delete build zip | |
run: ssh staging "cd /home/$SSH_USER/$REPO_NAME/ && sudo rm build.tar.gz" | |
- name: Delete current deployed files | |
run: ssh staging 'sudo rm -r -d /var/www/netdb' | |
continue-on-error: true | |
- name: Copy files | |
run: ssh staging "sudo rsync -av --ignore-existing /home/$SSH_USER/$REPO_NAME/ /var/www/netdb" |