Next.js Discord

Discord Forum

github action Redeploy GUIDE

Answered
Munchkin posted this in #help-forum
Open in Discord
MunchkinOP
Hello everyone.

I noticed that there is no easy way of self deploying your project to your self hosted server using Github actions.
I have finally written a runner script that will automatically redeploy the project and also makes sure the projects gets builded on your server while also making sure that any .env variables are being used.

For those who run into this issue. Please take a look at this

name: Node.js CI

on:
  push:
    branches: [ "master" ]

jobs:
  build:

    runs-on: [self-hosted]

    strategy:
      matrix:
        node-version: [20.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
    - name: Change permissions
      run: echo ${{ secrets.PASSWORD }} | sudo -S chmod -R 777 #<Put your directory here>
    - uses: actions/checkout@v3
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - name: Set up environment variables
      run: |
        if [ ! -f .env ]; then
          touch .env
        fi
 #<Any secrets here using: ${{secrets.SECRET}}>
        echo "" >> .env 
     #use the password for your running account. Or SSH key should also work i think???
    - name: Run npm commands with sudo
      run: |
        echo ${{ secrets.PASSWORD }} | sudo -S npm ci
        echo ${{ secrets.PASSWORD }} | sudo -S npm run build
        echo ${{ secrets.PASSWORD }} | sudo -S pm2 restart <ProjectName> --update-env


this uses:

- Node v20
- PM2
- NPM

It is recommended to create a specific user for this.
Im not sure if this only works for self hosted runners or not.
Hopefully this helps!
Answered by Munchkin
^^^
View full answer

1 Reply

@Munchkin Hello everyone. I noticed that there is no easy way of self deploying your project to your self hosted server using Github actions. I have finally written a runner script that will automatically redeploy the project and also makes sure the projects gets builded on your server while also making sure that any .env variables are being used. For those who run into this issue. Please take a look at this yml name: Node.js CI on: push: branches: [ "master" ] jobs: build: runs-on: [self-hosted] strategy: matrix: node-version: [20.x] # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ steps: - name: Change permissions run: echo ${{ secrets.PASSWORD }} | sudo -S chmod -R 777 #<Put your directory here> - uses: actions/checkout@v3 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} cache: 'npm' - name: Set up environment variables run: | if [ ! -f .env ]; then touch .env fi #<Any secrets here using: ${{secrets.SECRET}}> echo "" >> .env #use the password for your running account. Or SSH key should also work i think??? - name: Run npm commands with sudo run: | echo ${{ secrets.PASSWORD }} | sudo -S npm ci echo ${{ secrets.PASSWORD }} | sudo -S npm run build echo ${{ secrets.PASSWORD }} | sudo -S pm2 restart <ProjectName> --update-env this uses: - Node v20 - PM2 - NPM It is recommended to create a specific user for this. Im not sure if this only works for self hosted runners or not. Hopefully this helps!
MunchkinOP
^^^
Answer