Automating the deployment process for Fastify applications can significantly improve development efficiency and reduce manual errors. Combining Jenkins and GitHub Actions offers a robust solution for continuous integration and continuous deployment (CI/CD). This guide provides a step-by-step approach to setting up automated Fastify deployments using these powerful tools.

Prerequisites

  • Basic knowledge of Fastify framework
  • GitHub repository for your Fastify project
  • Jenkins server installed and accessible
  • GitHub Actions enabled in your repository
  • Server or hosting environment for deployment

Step 1: Prepare Your Fastify Application

Ensure your Fastify app is ready for deployment. This includes having a production build process, environment variables configured, and a package.json script for deployment.

Example package.json script:

"scripts": { "start": "node app.js", "build": "your-build-command" }

Step 2: Set Up GitHub Actions Workflow

Create a new workflow file in your repository at .github/workflows/deploy.yml. Define the trigger, environment, and deployment steps.

Sample workflow configuration:

name: Deploy Fastify App

on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '14'
      - name: Install dependencies
        run: npm install
      - name: Build project
        run: npm run build
      - name: Deploy to server
        uses: appleboy/[email protected]
        with:
          host: ${{ secrets.SERVER_HOST }}
          username: ${{ secrets.SERVER_USER }}
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          source: "."
          target: "/path/to/deploy"
      - name: SSH commands
        uses: appleboy/[email protected]
        with:
          host: ${{ secrets.SERVER_HOST }}
          username: ${{ secrets.SERVER_USER }}
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          script: |
            cd /path/to/deploy
            npm install --production
            pm2 restart all

Step 3: Configure Jenkins for CI/CD

Set up a Jenkins job to listen for GitHub repository changes. Use the Git plugin to clone the repository and execute deployment scripts.

Sample Jenkins pipeline script:

pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                git 'https://github.com/yourusername/yourrepo.git'
            }
        }
        stage('Install Dependencies') {
            steps {
                sh 'npm install'
            }
        }
        stage('Build') {
            steps {
                sh 'npm run build'
            }
        }
        stage('Deploy') {
            steps {
                sh '''
                ssh -i /path/to/private/key user@server << EOF
                cd /path/to/deploy
                git pull
                npm install --production
                pm2 restart all
                EOF
                '''
            }
        }
    }
}

Step 4: Secure Your Deployment

Use SSH keys and secrets management to protect sensitive information. Store private keys securely in Jenkins credentials and GitHub secrets.

Step 5: Testing and Validation

Push changes to your GitHub repository and verify that GitHub Actions triggers the workflow. Check Jenkins logs to ensure deployment completes successfully. Test your application after deployment to confirm everything works as expected.

Conclusion

Automating Fastify deployments with Jenkins and GitHub Actions streamlines your development process and ensures consistent, reliable releases. By following this step-by-step guide, you can set up a robust CI/CD pipeline that minimizes manual intervention and accelerates your deployment cycle.