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 like deploying your Node.js code to production servers. In this guide, we will walk through how you can securely and reliably deploy your Node.js application to both staging and production environments using GitHub Actions.
We suggest maintaining two branches: staging and production. When you push code to either of these branches, your changes automatically deploy to the corresponding Stackhero service.
Having a staging instance is optional. You can follow this guide using only a production instance. To make deployments smoother and build confidence before going live, it is highly recommended to maintain both staging and production environments. This approach is widely adopted in the industry and helps prevent common deployment issues.
Before you get started, make sure you have a GitHub account and a repository hosting your Node.js code.
Creating the Node.js services
First, sign in to your Stackhero dashboard and create two Stackhero services: one for staging and one for production. For clarity, you might want to name these services "Staging" and "Production".
If you do not have a Stackhero account yet, you can create one in just two minutes for free and set up your Node.js cloud services in a few clicks.
Example of Node.js services
Configure SSH keys
SSH keys allow GitHub Actions to securely connect to your Node.js service when deploying your code. This is a crucial step for protecting your Stackhero services.
On your computer, you can generate new SSH keys using:
ssh-keygen -C "" -f /tmp/ssh_key -N ""
Set the public key
To view 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 settings
Continue with these steps:
- In SSH public keys, click on Add a public key.
- For Description, enter
GitHub Action. - For Key, paste the public key you copied earlier.
Get service settings
Set the private key
Now, switch to GitHub and open your project repository. Click on Settings, then select Environments. Choose New environment.
Configuring GitHub environments
Enter "production" for the Name and confirm.
Setting the environment
Click the No restriction button, then choose Selected branches and tags.
Setting environment restrictions
Now click on Add deployment branch or tag rule, enter "production" in the Name pattern field, and click Add rule.
Setting environment branch
Setting environment branch
In Environment secrets, click on Add secret.
Add 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 secret
Then, in Environment variables, click on Add variable.
Setting 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 variable
If you have set a custom domain name for your service, use your custom domain instead of <XXXXXX>.stackhero-network.com.
Delete the generated keys
For security, it is a good idea to remove the SSH keys from your computer after you have set them up on Stackhero and GitHub:
rm /tmp/ssh_key /tmp/ssh_key.pub
Configure the GitHub Actions workflow
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 like this:
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 confirm the deployment, open your project on GitHub and click on Actions.
GitHub Actions that deployed to production
That is it. Your code is now set up for automatic deployment to production via GitHub Actions.
Creating the staging environment
Setting up the staging environment is almost the same as for production. Just repeat the steps above, replacing production with staging as needed.
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 designated Node.js service.
Going further
It is a good idea to protect the production and staging branches to prevent direct pushes. Instead, you can create a pull request for the staging branch and merge changes from your development branch. Once you have validated the changes on the staging platform, merge the pull request into the production branch.
This workflow helps ensure only authorized contributors can push to staging and production, and it provides an extra layer of testing before new features go live.