- Published on
Automatic Deployment of Node.js Using GitHub Actions
source github
After quite some time without writing, I finally managed to find time to add a new blog post
Okay, for those who frequently manage servers, this tool might be quite familiar because it's indeed very popular among devops.
So, what is GitHub Actions? It's an API or feature provided by GitHub to automate workflows based on events in our repository. Yes, with GitHub Actions, we no longer need to manually perform tasks when changes occur in our repository, like when pushing code or when someone makes a pull request to our repo. These tasks based on events can be automated with GitHub Actions. For more details, you can check it out directly on my github.
Here, I'll demonstrate how to automatically deploy a Node.js application to our own server.
Create the File
Create a folder named .github/workflows and a file named nodejs.yml in the root directory of your project.
mkdir -p .github/workflows
touch .github/workflows/nodejs.yml
Insert the code below into the nodejs.yml file.
In the code, there are several {{ secrets.x }} which contain our server credentials.
The port, key, and passphrase parameters are optional, depending on your SSH server configuration.
The script parameter contains the command-line instructions for your server.
Next, create those secrets in your GitHub project repository by going to Settings > Secrets.
Then, select New Secret and fill in the value according to your server credentials.
Finally, push those changes to your repository, then open the Actions menu, and GitHub Actions will run automatically.
Additional Notes
For the script parameter above, I used the following command:
~/.nvm/versions/node/v12.18.0/bin/npm install
~/.nvm/versions/node/v12.18.0/bin/pm2 reload app
I use nvm for Node.js, so we need to add a symlink because the node command in the script will, by default, look for the executable in the /usr/bin/node directory. If we don't add the symlink, an error "/usr/bin/env: node: No such file or directory" will occur.
Here’s the command to add the symlink:
sudo ln -s ~/.nvm/versions/node/v12.18.0/bin/node /usr/bin/node
The folder version depends on the Node.js version you have installed. Here, I’m using Node.js version 12.18.0.
Versi folder tergantung dari versi nodejs yang kalian install. Disini saya menggunakan Node versi 12.18.0.
That’s all for now, see ya~