Node.js: Deploy with GitHub Actions

Learn how to deploy your Node.js code using GitHub Actions

👋 Welcome to the Stackhero documentation!

Stackhero offers a ready-to-use Node.js cloud solution that provides a host of benefits, including:

  • Deploy your application in seconds with a simple git push.
  • Use your own domain name and benefit from the automatic configuration of HTTPS certificates for enhanced security.
  • Enjoy peace of mind with automatic backups, one-click updates, and straightforward, transparent, and predictable pricing.
  • Get optimal performance and robust security thanks to a private and dedicated VM.

Save time and simplify your life: it only takes 5 minutes to try Stackhero's Node.js cloud hosting solution!

GitHub Actions makes it easy to automate tasks such as deploying your Node.js code to production servers. In this guide, we will show you how to securely and reliably deploy your Node.js application to both staging and production environments using GitHub Actions.

We recommend maintaining two branches: staging and production. When you push code to either of these branches, your changes are automatically deployed to the corresponding Stackhero service.

Having a staging instance is optional. You can follow this guide using only a production instance. To streamline deployments and validate your changes before going live, it is highly recommended to maintain both staging and production environments. This approach is widely used in the industry and helps avoid many common deployment issues.

Before you begin, make sure you have a GitHub account and a repository hosting your Node.js code.

First, sign in to your Stackhero dashboard and create two Stackhero services: one for staging and one for production. For clarity, you may want to name these services "Staging" and "Production".

If you do not have a Stackhero account yet, you can create one for free in just two minutes and set up your Node.js cloud services in a few clicks.

Example of Node.js servicesExample of Node.js services

SSH keys allow GitHub Actions to securely connect to your Node.js service when deploying your code. This step is essential for protecting your Stackhero services.

On your computer, you can generate new SSH keys with:

ssh-keygen -C "" -f /tmp/ssh_key -N ""

To display the public key you just created, run:

cat /tmp/ssh_key.pub

Next, in your Stackhero dashboard, select your production Node.js service and click the Configure button.

Get service settingsGet service settings

Continue with these steps:

  1. In SSH public keys, click on Add a public key.
  2. For Description, enter GitHub Action.
  3. For Key, paste the public key you copied earlier.

Get service settingsGet service settings

Now, switch to GitHub and open your project repository. Click on Settings, then select Environments. Choose New environment.

Configuring GitHub environmentsConfiguring GitHub environments

Enter "production" for the Name and confirm.

Setting the environmentSetting the environment

Click the No restriction button, then choose Selected branches and tags.

Setting environment restrictionsSetting environment restrictions

Now click on Add deployment branch or tag rule, enter "production" in the Name pattern field, and click Add rule.

Setting environment branchSetting environment branch

Setting environment branchSetting environment branch

In Environment secrets, click on Add secret.

Add secretAdd secret

To retrieve the private key you generated, run:

cat /tmp/ssh_key

In GitHub, use STACKHERO_SSH_PRIVATE_KEY as the Name and paste your private key into the Value field.

Setting the SSH private key secretSetting the SSH private key secret

Then, in Environment variables, click on Add variable.

Setting variablesSetting variables

Enter STACKHERO_ENDPOINT as the Name and paste your Node.js service endpoint into the Value field. You can find this endpoint in your Stackhero dashboard.

Setting the endpoint variableSetting the endpoint variable

If you have set a custom domain name for your service, use your custom domain instead of <XXXXXX>.stackhero-network.com.

For security, it is recommended to remove the SSH keys from your computer once they have been added to Stackhero and GitHub:

rm /tmp/ssh_key /tmp/ssh_key.pub

Inside your Git repository, create a .github/workflows directory if it does not already exist. Then, add a file named deploy-to-stackhero.yml:

# File: .github/workflows/deploy-to-stackhero.yml

name: Deploy to Stackhero
run-name: Deploy branch "${{ github.ref_name }}" to Stackhero

on:
  push:
    # List the branches that trigger the deployment action. Make sure there is an environment in GitHub (under "Settings" > "Environments") for each branch.
    # Then add the corresponding secret STACKHERO_SSH_PRIVATE_KEY and variable STACKHERO_ENDPOINT in that environment.
    branches: [ "production", "staging" ]

jobs:
  Deploy:
    environment: ${{ github.ref_name }}
    runs-on: ubuntu-latest
    steps:
    - uses: stackhero-io/github-actions-deploy-to-stackhero@v1
      with:
        # STACKHERO_SSH_PRIVATE_KEY and STACKHERO_ENDPOINT should be set in the respective GitHub environment.
        ssh_private_key: ${{ secrets.STACKHERO_SSH_PRIVATE_KEY }}
        endpoint: ${{ vars.STACKHERO_ENDPOINT }}

Once you have created the workflow file, you can commit your changes as follows:

git add -A .
git commit -m "Add GitHub Actions to deploy to Stackhero"

To create a production branch, run:

git checkout -b production

Then push your changes to GitHub:

git push --set-upstream origin production

This push sends your code to the production branch and triggers GitHub Actions to deploy your code to the Stackhero service. To check the deployment, open your project on GitHub and click on Actions.

GitHub Actions that deployed to productionGitHub Actions that deployed to production

That's it. Your code is now ready for automatic deployment to production via GitHub Actions.

Setting up the staging environment is almost identical to production. Simply repeat the previous steps, replacing production with staging where necessary.

Start by creating a staging branch:

git checkout -b staging

Then push your staging branch to GitHub:

git push --set-upstream origin staging

GitHub Actions will automatically deploy your staging branch to its dedicated Node.js service.

It is a good idea to protect the production and staging branches to prevent direct pushes. You can create a pull request to the staging branch and merge changes from your development branch. Once the changes have been validated on the staging platform, merge the pull request into the production branch.

This workflow ensures that only authorized contributors can push to staging and production branches, while adding a validation step before new features go live.