
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.
Automatically deploy to your Linux server after every push to the main branch with GitHub Actions.
on:push:tags:- v*.*.*branches:- main
- name: Copy files via scpuses: appleboy/scp-action@masterwith: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 placeuses: appleboy/ssh-action@masterwith: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 Serveron:push:tags:- v*.*.*branches:- mainjobs:build:runs-on: ubuntu-lateststeps:- name: Checkout repositoryuses: actions/checkout@master- name: Set up Node.jsuses: actions/setup-node@masterwith:node-version: 14.x- name: Install Gatsby CLIrun: npm install -g gatsby-cli- name: Install dependenciesrun: npm install- name: Buildrun: gatsby build- name: Copy files via scpuses: appleboy/scp-action@masterwith: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 placeuses: appleboy/ssh-action@masterwith: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
In addition to the basic deployment process described above, you can also incorporate the following elements for improved security, monitoring, and overall management.
- name: Run testsrun: npm test
jobs:build:runs-on: ubuntu-lateststeps:# ...- name: Run testsrun: npm test- name: Buildrun: gatsby buildif: success()# ...
- name: Notify Slack on Successif: success()uses: 8398a7/action-slack@v3with:status: ${{ job.status }}text: 'Deployment succeeded :rocket:'env:SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}- name: Notify Slack on Failureif: failure()uses: 8398a7/action-slack@v3with:status: ${{ job.status }}text: 'Deployment failed :x:'env:SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
- name: Cache dependenciesuses: actions/cache@v2with:path: ~/.npmkey: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}restore-keys: |${{ runner.os }}-node-
jobs:build:runs-on: ubuntu-lateststrategy:matrix:environment: [staging, production]steps:# ...- name: Set environment variablesrun: |if [ "${{ matrix.environment }}" == "staging" ]; thenecho "API_KEY=${{ secrets.STAGING_API_KEY }}" >> $GITHUB_ENVelseecho "API_KEY=${{ secrets.PRODUCTION_API_KEY }}" >> $GITHUB_ENVfi# ...
Incorporate these additional elements into your deployment process to enhance security, monitoring, and overall management of your application.
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:
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.
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.
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.
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.
For more information on the topics covered in this tutorial, please refer to the following resources:
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:
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!
Quick Links
Legal Stuff





