Deploying on a custom static server

This post is about deploying a Next build onto remote server via SSH.

Prerequisites

In order to complete this manual you're going to need a remote server with SSH access to it.

1. Create a Repo on GitHub

Firstly, create a new repository to connect Heroku with your codebase. It will allow you to use commits to master as a trigger to deploy.

Create a new GitHub repository

Setup name, description and other repository settings.

Setup repository name, description and other settings

After it's done, make changes in the code, commit and push them into the repo.

git add .
git commit -m "Add app starter files"
git push origin master

2. Setup a GitHub Action

Go to “SSH Deploy” Action's page and scroll to “Usage” section.

There you will find an example of how to use this Action in your workflow.

To activate it create a directory .github in the root of your project and inside create another directory called workflows. There, create a YAML-file called custom.yml.

In this file describe the configuration for build and deploy.

name: Deploy on a custom server

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-node@v1
        with:
          node-version: "12"
      - run: npm install
      - run: npm run build
      - run: npx next export
      - uses: easingthemes/ssh-deploy@v2.1.1
        env:
          SSH_PRIVATE_KEY: ${{ secrets.SERVER_SSH_KEY }}
          ARGS: "-rltgoDzvO --delete"
          SOURCE: "out/"
          REMOTE_HOST: ${{ secrets.REMOTE_HOST }}
          REMOTE_USER: ${{ secrets.REMOTE_USER }}
          TARGET: ${{ secrets.REMOTE_TARGET }}

For this to work you're going to need to specify some secrets that should be stored in “Secrets” section in your GitHub repository.

3. Generate Keys for GitHub Actions on a Server

For “SSH Deploy” to work we need to grant it access to a server.

Connect to your server via SSH. (You may be asked a password if you connect first time.)

ssh your_user@your-server.com

When connected, generate new key pair. Keep password empty.

ssh-keygen -t rsa

You might want to set some unique name for the key, just to make it easier to find it later.

When generated, authorize those keys:

ssh-copy-id -i ~/.ssh/key-name your_user@your-server.com

Now copy private key value and paste it as a value for SERVER_SSH_KEY in “Secrets” section.

pbcopy < ~/.ssh/key-name

4. Setup Project Secrets

Create new secrets in the “Secrets” section in GitHub repository.

Create new secrets in the “Secrets” section

When it's done, update your code, push changes to master branch and GitHub Action will build your project, export everything to out directory and deploy all the assets using rsync utility.

Back to main page