Blog>
Snippets

Implementing Continuous Integration/Continuous Deployment (CI/CD) with GitHub Actions for Next.js 14

Provide an example of setting up a CI/CD pipeline with GitHub Actions to deploy a Next.js 14 application automatically on every push or pull request merge.
name: CI/CD Pipeline

on:
  push:
    branches: [ 'main' ]
  pull_request:
    branches: [ 'main' ]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
      with:
        fetch-depth: 0

    - name: Set up Node.js
      uses: actions/setup-node@v1
      with:
        node-version: '14'

    - name: Install Dependencies
      run: npm install

    - name: Build
      run: npm run build

    - name: Deploy
      if: github.ref == 'refs/heads/main'
      run: echo Deploy to production environment
      # Replace the above echo command with actual deployment scripts like
      # run: npm run deploy
      # Make sure the deploy script is set appropriately in your package.json or in another script file
This YAML configuration sets up a GitHub Actions workflow for a Next.js 14 application. It triggers on push to the 'main' branch or on a pull request merge into 'main'. The workflow runs on an Ubuntu-latest virtual machine, checks out the code, sets up Node.js (v14), installs dependencies, builds the application, and deploys it if the push is to the main branch. Replace the echo command in the Deploy step with your deployment script.