HomeContact

GitHub action to deploy on linux server

By Shady Nagy
Published in Github
May 01, 2021
3 min read
GitHub action to deploy on linux server

Table Of Contents

01
Introduction
02
Easily Deploy to a Linux Server Using GitHub Actions
03
Enhance Security and Monitoring in Your Deployment Process
04
Monitoring and Logging
05
Conclusion
06
Further Reading
07
Feedback and Questions

Introduction

In this tutorial, we’ll walk you through the process of automating deployment to a Linux server using GitHub Actions. This powerful feature allows you to streamline your workflow and ensure that your application is always up-to-date with the latest changes from your repository. We’ll cover the basic setup and provide some additional enhancements to improve security, monitoring, and management.

Easily Deploy to a Linux Server Using GitHub Actions

Automatically deploy to your Linux server after every push to the main branch with GitHub Actions.

  1. Set the push condition for the main branch:
on:
push:
tags:
- v*.*.*
branches:
- main
  1. Copy files from the repository to the Linux server using SCP:
- name: Copy files via scp
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
port: ${{ secrets.PORT }}
key: ${{ secrets.SSHKEY }}
passphrase: ${{ secrets.PASSPHRASE }}
source: "./public"
target: ${{ secrets.COPY_TO }}
  1. Execute a script to move files to the appropriate directory and restart Nginx or perform other actions using SSH or Bash syntax:
- name: Move files to correct place
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
port: ${{ secrets.PORT }}
key: ${{ secrets.SSHKEY }}
passphrase: ${{ secrets.PASSPHRASE }}
script: rm -rf ${{ secrets.COPY_LAST }};mv ${{ secrets.COPY_FROM }} ${{ secrets.COPY_LAST }};chcon -Rt httpd_sys_content_t ${{ secrets.COPY_LAST }}; nginx -s reload

Create a deploy.yml file in the repository at the path .github/workflows.

Example deploy.yml for deploying a Gatsby.js site:

name: Deploy On Server
on:
push:
tags:
- v*.*.*
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@master
- name: Set up Node.js
uses: actions/setup-node@master
with:
node-version: 14.x
- name: Install Gatsby CLI
run: npm install -g gatsby-cli
- name: Install dependencies
run: npm install
- name: Build
run: gatsby build
- name: Copy files via scp
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
port: ${{ secrets.PORT }}
key: ${{ secrets.SSHKEY }}
passphrase: ${{ secrets.PASSPHRASE }}
source: "./public"
target: ${{ secrets.COPY_TO }}
- name: Move files to correct place
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
port: ${{ secrets.PORT }}
key: ${{ secrets.SSHKEY }}
passphrase: ${{ secrets.PASSPHRASE }}
script: rm -rf ${{ secrets.COPY_LAST }};mv ${{ secrets.COPY_FROM }} ${{ secrets.COPY_LAST }};chcon -Rt httpd_sys_content_t ${{ secrets.COPY_LAST }}; nginx -s reload

Enhance Security and Monitoring in Your Deployment Process

In addition to the basic deployment process described above, you can also incorporate the following elements for improved security, monitoring, and overall management.

  1. Add a test stage: Ensure your application is functioning correctly before deployment by incorporating automated tests. This will minimize the chance of introducing bugs into your production environment.
- name: Run tests
run: npm test
  1. Deploy only if tests pass: Adjust the workflow to deploy only if all tests pass, preventing deployment of faulty code.
jobs:
build:
runs-on: ubuntu-latest
steps:
# ...
- name: Run tests
run: npm test
- name: Build
run: gatsby build
if: success()
# ...
  1. Slack notifications: Get notified about the deployment status by integrating Slack notifications into your workflow. Receive updates about successful deployments, as well as alerts for any failed deployments.
- name: Notify Slack on Success
if: success()
uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
text: 'Deployment succeeded :rocket:'
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
- name: Notify Slack on Failure
if: failure()
uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
text: 'Deployment failed :x:'
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
  1. Cache dependencies: Speed up the build process by caching dependencies. This saves time by avoiding the need to download and install them for every build.
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
  1. Add environment-specific configurations: Incorporate different configurations for various environments, such as staging and production. This allows for different settings, API keys, or other variables to be applied depending on the target environment.
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
environment: [staging, production]
steps:
# ...
- name: Set environment variables
run: |
if [ "${{ matrix.environment }}" == "staging" ]; then
echo "API_KEY=${{ secrets.STAGING_API_KEY }}" >> $GITHUB_ENV
else
echo "API_KEY=${{ secrets.PRODUCTION_API_KEY }}" >> $GITHUB_ENV
fi
# ...

Incorporate these additional elements into your deployment process to enhance security, monitoring, and overall management of your application.

Monitoring and Logging

To ensure the smooth operation of your deployed application, it’s essential to monitor performance and keep track of any errors that may arise. Implementing monitoring and logging tools can help you quickly identify and resolve issues. Here are some recommendations:

  1. Log aggregation: Set up log aggregation for your application to centralize and analyze log data. Tools like Logstash, Fluentd, or AWS CloudWatch Logs can help you collect and manage logs from multiple sources, making it easier to identify patterns and troubleshoot issues.

  2. Performance monitoring: Monitor the performance of your application using tools like New Relic, Datadog, or AppDynamics. These services provide real-time insights into the performance of your application, helping you identify bottlenecks and optimize your code.

  3. Error tracking: Track and manage errors in your application with tools like Sentry, Rollbar, or Bugsnag. These services provide detailed error reports, helping you identify and resolve issues quickly.

Example: Integrating Sentry for error tracking in a Node.js application

To set up Sentry in your Node.js application, follow these steps:

a. Install the Sentry SDK:

npm install --save @sentry/node

b. Configure Sentry in your application’s entry point:

const Sentry = require('@sentry/node');
Sentry.init({
dsn: 'YOUR_SENTRY_DSN',
environment: process.env.NODE_ENV,
});
// Include the rest of your application code

Replace YOUR_SENTRY_DSN with your Sentry DSN, which you can obtain from your Sentry project settings.

Integrating monitoring and logging tools into your deployment process can help you maintain a high level of performance and reliability for your application. By proactively addressing issues, you can ensure a positive user experience and maintain the overall health of your application.

Conclusion

By following the steps outlined in this tutorial, you’ll be able to set up a seamless deployment process using GitHub Actions. This will not only save you time but also provide a more reliable and efficient workflow for managing your application. Don’t forget to incorporate additional elements like tests, caching, and notifications to further enhance the deployment process.

Further Reading

For more information on the topics covered in this tutorial, please refer to the following resources:

  • GitHub Actions Documentation
  • GitHub Actions Marketplace
  • Gatsby.js Deployment Guide

Feedback and Questions

We’d love to hear your feedback on this tutorial! If you have any questions or suggestions for improvement, please don’t hesitate to reach out. You can leave a comment below, or you can contact us through the following channels:

  1. Email: shady@shadynagy.com
  2. Twitter: @ShadyNagy_
  3. LinkedIn: Shady Nagy

We’ll do our best to address any questions or concerns you may have. We look forward to hearing from you and helping you make the most of GitHub Actions and your deployment efforts!


Tags

#GitHub#GitHubActions#GitHubAction#Javascript#Yml#scp-action#ssh-action#Gatsbyjs#Gatsby

Share


Previous Article
Angular Change Detection Strategy
Shady Nagy

Shady Nagy

Software Innovation Architect

Topics

AI
Angular
dotnet
GatsbyJS
Github
Linux
MS SQL
Oracle

Related Posts

Local Webhook Testing Using ngrok to Connect GitHub Actions with Your .NET API
Local Webhook Testing Using ngrok to Connect GitHub Actions with Your .NET API
April 25, 2025
4 min

Quick Links

Contact Us

Social Media