Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    JavaScript

    Node.js Lesson 13: Debugging in Node.js

    Interview

    Interview with Iskander

    JavaScript

    Effective Strategies for Managing Scope Creep in Projects

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Wednesday, September 10
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / JavaScript / Node.js / Deploying Your NodeJS Code to a Server Every Time You Push with Github Actions
    Node.js

    Deploying Your NodeJS Code to a Server Every Time You Push with Github Actions

    bradstarartBy bradstarartJune 29, 2020No Comments7 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    Deploying Your NodeJS Code to a Server Every Time You Push with Github Actions
    Deploying Your NodeJS Code to a Server Every Time You Push with Github Actions
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    Deploying Your NodeJS Code to a Server Every Time You Push with Github Actions
    Deploying Your NodeJS Code to a Server Every Time You Push with Github Actions

    Since they were introduced in late 2019, GitHub Actions have completely changed the ways in which developers handle workflow automation. GitHub Actions allow you to automate tedious repetitive workflows like installing dependencies (npm ci), running tests (npm test), scanning for vulnerabilities (npm audit) and deploying to a server.

    Manual deployment should be a thing of the past. A lot of developers simply SSH into their servers, `git pull` and restart the app. This process is prone to run into a myriad of errors, though, not to mention the fact that it’s slow and repetitive. We can remedy that (for free!) with GitHub Actions.

    You can think of an action as a third party library that you can insert somewhere in your pipeline to perform certain tasks. There’s an action for almost anything you can think of – from setting up NodeJs to sending text messages. Individual actions are used inside workflows.

    Workflows are triggered by events. An event is almost anything that happens in your git repository – a push, commit, merge, etc. Essentially, any GitHub webhook event will trigger a workflow.

    Finally, workflows run on separate Github-owned servers. These servers are referred to as runners. You can host your own runners, but GitHub’s hosted ones will do just fine for our purposes.

    Let’s see this in action (no pun intended).

    Creating a workflow

    A workflow is a custom automated process consisting of jobs that are run in steps. Inside these steps, you can use actions or run bash commands within your package. This way, you can build, test, scan, package and deploy any project on Github.

    A workflow is configured using YAML syntax and saved in the ‘.github/actions’ folder. The directory structure should look like this:

    .github
    └── workflows
        └── hello-world.yml
    

    So, say we wanted to write a simple command that prints “Hello World” every time we push to the server.

    First, we need to specify the name of the workflow

    name: Hello World

    Since we only need our workflow to be triggered when a push is made to our git repository, we need to specify it as such.

    name: Hello World
    
    on:
      push:
        branches: [ master ]
    

    Every workflow must contain at least one job. We only have one job in this case so that shouldn’t be a problem.

    We’ll name our job “echo.”

    We also need to specify the operating system our job will run on. The latest version of Ubuntu will do just fine.

    name: Hello World
    
    on:
      push:
        branches: [ master ]
    
    jobs:
      echo:
        runs-on: ubuntu-latest
    

    Lastly, we need to define a series of steps that will run sequentially inside our job.

    name: Hello World
    
    on:
      push:
        branches: [ master ]
    
    jobs:
      echo:
        runs-on: ubuntu-latest
        steps:
        steps:
          - name: Checkout source code
            uses: actions/checkout@v2
          - name: Echo Hello World
            run: echo "Hello World"
    

    This job has two steps:

    1. Checkout the source code from our git repo using “actions/checkout@v2.” Notice that this is just an action, not a command. You can also leave out the ‘name’ property if you like.
    2. Run “Hello World” on the runner. ‘run’ accepts any valid bash commands.
    Read More:  Mastering N-API and Native Modules in Node.js

    This is the output we get from Github once you push to the repo.

    Output from Github
    Output from Github

    With the fundamentals out of the way, let’s apply the same principles to see how we can deploy a simple express app every time we push to the master branch.

    Deploying to an SSH server

    If you don’t have a project up and running already, you can bootstrap one using the command

    npx express-generator
    

    Before we proceed, let’s lay out the groundwork. Here is how the application is supposed to work:

    1. The developer pushes to a github repo
    2. A deploy workflow is triggered.
    3. The task runner connects to the SSH server.
    4. On the SSH server, the task runner clones the repo.
    5. Dependencies are installed.
    6. Start (or restart) any relevant processes.

    You might be curious as to why there isn’t a step for checking out the repo like in the previous section.

    We don’t need to checkout or clone the repo because we don’t run any direct commands on it from the runner. We simply connect to the SSH server and run all our commands there.

    This is a direct consequence of the lack of potentially resource-intensive tasks such as running tests, creating coverage reports, static code analysis and transpilation. Projects based on frameworks such as React and Typescript (that need a build step) will probably have a different-looking flow.

    This is how the above pseudocode looks when represented as a workflow YAML file.

    name: Deploy application
    
    on:
      push:
        branches: [ master ]
    
    jobs:
      deploy:
        runs-on: ubuntu-latest
        steps:
          - name: Deploy NodeJS app
            uses: appleboy/ssh-action@v0.1.2
            with:
              host: ${{secrets.SSH_HOST}}
              username: ${{ secrets.SSH_USERNAME }}
              password: ${{ secrets.SSH_PASSWORD }}
              script: |
                mkdir -p ~/my-express-app/ # create a new folder
                cd ~/my-express-app/ # navigate into the folder
                git clone https://github.com/Bradleykingz/github-actions-tutorial app # clone the repo into the 'app' folder
                cd github-actions-tutorial # navigate into the repo
                npm install # install dependencies 
                pm2 start app.js # start as a background service.
    

    Some steps are unique to this application because we are running it for the first time. Typically, for example, you won’t need to create new folders or clone your repo.

    Read More:  How to Architect a Node.Js Project from Ground Up?

    A more typical script would look like this:

       script: |
    	cd ~/my-express-app/
            git pull
            npm install
            pm2 restart app.js
    

    In order to log into our server, we have to provide the relevant credentials. These are defined as repository or organization-level secrets.

    Creating secrets

    To create a secret, head over to the “Settings” tab of your Github repo and click on “Secrets.” Here are the above SSH secrets set up in our dummy repository:

    SSH Secrets Set up in Our Dummy Repository
    SSH Secrets Set up in Our Dummy Repository

    To learn more about how secrets are encrypted, how they are passed to your workflow and the limits imposed on them, follow this Github guide.

    Since this is SSH, you’re free to use a private/public key pair if you prefer. Save it as a secret in your repository and supply it to your workflow file in the same manner as above.

    If you’d like to see the code on Github (you can’t see the secrets, though.)

    Github link: https://github.com/Bradleykingz/github-actions-tutorial

    Troubleshooting

    There are a couple of issues you may run into while attempting to run this tutorial.

    npm: command not found – this error is as a result of how commands are run once the task runner connects to your server. If you encounter this add NodeJS to your PATH.

    If Node is already included in your path and you still experience this, you will have to run all node-related commands explicitly. If you use NVM, for instance, you will need to run ` ~/.nvm/versions/node/v12.14.0/bin/npm install` instead of regular old ‘npm install’. Additionally, commands such as ‘pm2’ will need to be re-written.

    /usr/bin/env: ‘node’: No such file or directory – you will either run into this error because of PM2 or installing Node using a package manager like apt.

    For nvm users: `ln -s .nvm/versions/node/v<your node version>/bin/node`
    For apt users: `ln -s /usr/bin/nodejs /usr/bin/node`

    Be careful with this approach. If you ever change your node version, you will also need to change these commands to reflect the change.

    Conclusion

    Hopefully, this article serves as a great introduction to the world of automated development practices. Modifying your pipeline to automate boring repetitive bits gives developers time to concentrate on more important tasks at hand, besides greatly reducing human interaction with sensitive bits of the system such as deployment.

    Lastly, we didn’t get to showcase a lot of impressive things that Github Actions can do in this tutorial, but you’d do well to learn how to integrate a whole CI/CD pipeline just within Github itself.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    bradstarart

      Related Posts

      An Introduction to Clustering in Node.js

      March 26, 2024

      Server-Side Rendering with Vue.js and Nuxt.js

      February 26, 2024

      Mastering N-API and Native Modules in Node.js

      February 13, 2024
      Leave A Reply Cancel Reply

      You must be logged in to post a comment.

      Stay In Touch
      • Facebook
      • Twitter
      • Pinterest
      • Instagram
      • YouTube
      • Vimeo
      Don't Miss
      B2B Leads December 7, 2024

      Navigating B2B Lead Generation: Key Challenges and Solutions

      Navigating B2B lead generation can be challenging, with issues like targeting the right audience and managing complex sales cycles. Implementing data-driven strategies and leveraging automation tools can streamline the process and enhance conversion rates.

      React Lesson 13. Part 1: Asynchronous actions

      July 3, 2020

      Работа с Redmine

      June 25, 2016

      React Lesson 2: Homework Assignment

      November 1, 2019

      Categories

      • AI & Automation
      • Angular
      • ASP.NET
      • AWS
      • B2B Leads
      • Beginners
      • Blogs
      • Business Growth
      • Case Studies
      • Comics
      • Consultation
      • Content & Leadership
      • CSS
      • Development
      • Django
      • E-commerce & Retail
      • Entrepreneurs
      • Entrepreneurship
      • Events
      • Express.js
      • Facebook Ads
      • Finance & Fintech
      • Flask
      • Flutter
      • Franchising
      • Funnel Strategy
      • Git
      • GraphQL
      • Home Services Marketing
      • Influencer & Community
      • Interview
      • Java
      • Java Spring
      • JavaScript
      • Job
      • Laravel
      • Lead Generation
      • Legal & Compliance
      • LinkedIn
      • Machine Learning
      • Marketing Trends
      • Medical Marketing
      • MSP Lead Generation
      • MSP Marketing
      • NestJS
      • Next.js
      • Node.js
      • Node.js Lessons
      • Paid Advertising
      • PHP
      • Podcasts
      • POS Tutorial
      • Programming
      • Programming
      • Python
      • React
      • React Lessons
      • React Native
      • React Native Lessons
      • Recruitment
      • Remote Job
      • SaaS & Tech
      • SEO & Analytics
      • Soshace
      • Startups
      • Swarm Intelligence
      • Tips
      • Trends
      • Vue
      • Wiki
      • WordPress
      Top Posts

      Mastering JavaScript Proxies: Practical Use Cases and Real-World Applications

      Express.js May 7, 2023

      React Advanced: The Tiniest Recap — See What You’ve Missed

      Events October 31, 2019

      Interview with Alexander Troshchenko

      Interview March 28, 2018

      Angular 2, part 2

      Programming August 26, 2016

      Subscribe to Updates

      Get The Latest News, Updates, And Amazing Offers

      About Us
      About Us

      Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

      7901 4th St N, Suite 28690
      Saint Petersburg, FL 33702-4305
      Phone: 1(877)SOSHACE

      Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
      Our Picks
      Franchising

      Essential Skill of Capabilities Assessment

      Remote Job

      7 Statistics About Remote Work to Make Your Company Better

      Python

      How to send multiple forms with Ajax (FormData) in Django

      Most Popular

      Building a Pokedex with Next.js

      JavaScript

      Creating Real-Time API with Beautiful Soup and Django REST Framework

      Python

      NextJS Tutorial: Getting Started with NextJS

      Beginners
      © 2025 Soshace Digital.
      • Home
      • About
      • Services
      • Contact Us
      • Privacy Policy
      • Terms & Conditions

      Type above and press Enter to search. Press Esc to cancel.