Skip to content

Instantly share code, notes, and snippets.

@zellwk
Created August 2, 2023 03:58
Show Gist options
  • Save zellwk/c621f73a3c75ac964f56f0c811d7ec80 to your computer and use it in GitHub Desktop.
Save zellwk/c621f73a3c75ac964f56f0c811d7ec80 to your computer and use it in GitHub Desktop.
Github Actions
# See https://zellwk.com/blog/github-actions-deploy/ an explanation of this code
name: deploy
on:
push:
branches:
- main
- master
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- run: npm install
- run: npm run build
- name: Install SSH Key
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.SSH_PRIVATE_KEY }}
known_hosts: unnecessary
- name: Adding Known Hosts
run: ssh-keyscan -p ${{ secrets.SSH_PORT }} -H ${{ secrets.SSH_HOST }} >> ~/.ssh/known_hosts
- name: Deploy with rsync
run: rsync -avz -e "ssh -p ${{ secrets.SSH_PORT }}" ./dist/ ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}:/var/www/students.asyncjs.today/dist/
- name: Restart Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SSH_PORT }}
script: |
cd /PATH_TO_FOLDER
git fetch origin master
git reset --hard FETCH_HEAD
git clean -d -f --exclude secrets
npm install --production
npm run restart
@wpplumber
Copy link

I added the SSH under Deploy key! should I move it to account's SSH?

@zellwk
Copy link
Author

zellwk commented Sep 8, 2023

You have to authorize the GitHub Actions server to SSH into your server, just like how you authroizes your computer to SSH into the server.

See the linked article for steps to do this

@joebnb
Copy link

joebnb commented Oct 5, 2023

Thank you for this gist, did you meet this error?

i have same error, im doubting this caused by some environment bug,i'm running github runner self hosted in docker container,did you running github runner also in a docker container?

@Tholkappiar
Copy link

this is mine which i used :

steps:
- name: Checkout code
  uses: actions/checkout@v2

- name: Install SSH Key
  uses: shimataro/ssh-key-action@v2
  with:
    key: ${{ secrets.SSH_PRIVATE_KEY }}
    known_hosts: unnecessary
    
- name: Adding Known Hosts
  run: ssh-keyscan -H ${{ secrets.SSH_HOST }} >> ~/.ssh/known_hosts

- name: Remove .git in htdocs on Server
  run: |
    ssh ${{ secrets.USER }}@${{ secrets.USER_SERVER }} "rm -rf /var/www/html/SnapEase/.git"

- name: Print Deployment Success
  run: echo "Deployment successful!" `

And got this : Run ssh-keyscan -H *** >> ~/.ssh/known_hosts ssh-keyscan -H *** >> ~/.ssh/known_hosts shell: /usr/bin/bash -e {0} Error: Process completed with exit code 1. Any idea Bro ?

@karabomaila
Copy link

@Tholkappiar, I am getting the same error, where you able to solve it?

@joebnb
Copy link

joebnb commented Feb 22, 2024

note:runs-on not a architect platform,that's a name of your hosted runner name

@karabomaila
Copy link

I don’t understand you, what do you mean?

@sainikbiswas
Copy link

Hi @zellwk Thanks for sharing this. Your blog post https://zellwk.com/blog/github-actions-deploy/ was very helpful and it helped me setup the workflow the way I wanted it. I had to tweak my configuration a little bit to get it working but it was worth it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment