Skip to content

Instantly share code, notes, and snippets.

@nurmdrafi
Last active April 22, 2024 11:06
Show Gist options
  • Save nurmdrafi/117afcc1c39a124415806573f2da4c0d to your computer and use it in GitHub Desktop.
Save nurmdrafi/117afcc1c39a124415806573f2da4c0d to your computer and use it in GitHub Desktop.
Github Actions Workflow Example

NextJS Deploy with Docker

name: Docker Image CI/CD for Staging

on:
  push:
    branches: ["staging"]

env: 
  DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
  DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
  SSH_HOST_STAGING: ${{ secrets.SSH_HOST_STAGING }}
  SSH_USER_STAGING: ${{ secrets.SSH_USER_STAGING }}
  SSH_KEY_STAGING: ${{ secrets.SSH_KEY_STAGING }}
  DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Branch name
        run: echo running on branch ${GITHUB_REF##*/}

      - name: DockerHub Login
        uses: docker/login-action@v2.1.0
        with:
          username: ${{ env.DOCKER_USERNAME }}
          password: ${{ env.DOCKER_PASSWORD }}

      - name: Build the Docker image
        run: docker build . --file Dockerfile --tag ${{ env.DOCKER_USERNAME }}/jti-dashboard:${GITHUB_REF##*/}

      - name: Docker Push
        run: docker push ${{ env.DOCKER_USERNAME }}/jti-dashboard:${GITHUB_REF##*/}

      - name: Run deployment command
        uses: appleboy/ssh-action@v0.1.10
        with:
          host: ${{ env.SSH_HOST_STAGING }}
          username: ${{ env.SSH_USER_STAGING }}
          key: ${{ env.SSH_KEY_STAGING }}
          script: |
            cd /var/www/html/jti-dashboard-docker
            docker compose down
            docker image prune -af
            docker compose pull
            docker compose up -d

      - name: Test Success
        uses: rjstone/discord-webhook-notify@v1
        if: success()
        with:
          username: JTI Dashboard
          severity: info
          details: "The deployment was successful without any issues."
          webhookUrl: ${{ env.DISCORD_WEBHOOK }}

      - name: Test Failure
        uses: rjstone/discord-webhook-notify@v1
        if: failure()
        with:
          username: JTI Dashboard
          severity: error
          details: Test Failed!
          webhookUrl: ${{ env.DISCORD_WEBHOOK }}

      - name: Test Cancelled
        uses: rjstone/discord-webhook-notify@v1
        if: cancelled()
        with:
          username: JTI Dashboard
          severity: warn
          details: Test Cancelled!
          webhookUrl: ${{ env.DISCORD_WEBHOOK }}

NextJS Deploy with PM2

name: Deploy With PM2

on: 
  push:
    branches:
      - main
env: 
  SSH_HOST: ${{ secrets.SSH_HOST }}
  SSH_USER: ${{ secrets.SSH_USER }}
  SSH_KEY: ${{ secrets.SSH_KEY }}
  GH_TOKEN: ${{ secrets.GH_TOKEN }}
  GH_USERNAME: ${{ secrets.GH_USERNAME }}
  DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
  
jobs:
  push_to_registry:
    name: Build and Start
    runs-on: ubuntu-latest

    steps:  
      - name: Print Branch Name
        run: |
          echo "Branch name is: '${{ github.ref_name }}'"    

      - name: Run Deployment Command
        uses: appleboy/ssh-action@v0.1.10
        with:
          host: ${{ env.SSH_HOST }}
          username: ${{ env.SSH_USER }}
          key: ${{ env.SSH_KEY }}
          script: | 
            cd /var/www/html/summit/summit-dashboard
            source ~/.nvm/nvm.sh
            git stash
            git pull https://${{ env.GH_USERNAME }}:${{ env.GH_TOKEN }}@github.com/barikoi/summit-dashboard.git main
            nvm use 18.12.1
            npm i
            npm run build
            pm2 restart 3
            
      - name: Test Success
        uses: rjstone/discord-webhook-notify@v1
        if: success()
        with:
          severity: info
          username: Summit Dashboard
          details: "The deployment was successful without any issues."
          webhookUrl: ${{ env.DISCORD_WEBHOOK }}

      - name: Test Failure
        uses: rjstone/discord-webhook-notify@v1
        if: failure()
        with:
          severity: error
          username: Summit Dashboard
          details: Test Failed!
          webhookUrl: ${{ env.DISCORD_WEBHOOK }}

      - name: Test Cancelled
        uses: rjstone/discord-webhook-notify@v1
        if: cancelled()
        with:
          severity: warn
          username: Summit Dashboard
          details: Test Cancelled!
          webhookUrl: ${{ env.DISCORD_WEBHOOK }}            

ReactJS Build and Serve

name: Build and Serve Staging

on: 
  push:
    branches:
      - dev

env:
  SSH_USER_STAGING: ${{ secrets.SSH_USER_STAGING }}
  SSH_HOST_STAGING: ${{ secrets.SSH_HOST_STAGING }}
  SSH_KEY_STAGING: ${{ secrets.SSH_KEY_STAGING }}
  DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}

jobs:
  build_and_serve:
    name: Build and Serve Staging
    runs-on: ubuntu-latest

    steps:
    - name: Checkout Repository
      uses: actions/checkout@v2

    - name: Set Up Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '16.20.2'

    - name: Install Dependencies
      run: npm install --force

    - name: Build React Project
      run: npm run build

    - name: Copy File Via SSH Key
      uses: appleboy/scp-action@v0.1.7
      with:
        host: ${{ env.SSH_HOST_STAGING }}
        username: ${{ env.SSH_USER_STAGING }}
        key: ${{ env.SSH_KEY_STAGING }}
        source: "build/*"
        target: "/home/barikoi/verify-frontend-v2"
        rm: true

    - name: Move Build Files
      uses: appleboy/ssh-action@v0.1.10
      with:
        host: ${{ env.SSH_HOST_STAGING }}
        username: ${{ env.SSH_USER_STAGING }}
        key: ${{ env.SSH_KEY_STAGING }}
        script: |
          cd /home/barikoi/verify-frontend-v2
          mv build/* ./
          rm -r build
          
    - name: Test Success
      uses: rjstone/discord-webhook-notify@v1
      if: success()
      with:
        username: Verify Dashboard Staging
        severity: info
        details: "The deployment was successful without any issues."
        webhookUrl: ${{ env.DISCORD_WEBHOOK }}

    - name: Test Failure
      uses: rjstone/discord-webhook-notify@v1
      if: failure()
      with:
        username: Verify Dashboard Staging
        severity: error
        details: Test Failed!
        webhookUrl: ${{ env.DISCORD_WEBHOOK }}

    - name: Test Cancelled
      uses: rjstone/discord-webhook-notify@v1
      if: cancelled()
      with:
        username: Verify Dashboard Staging
        severity: warn
        details: Test Cancelled!
        webhookUrl: ${{ env.DISCORD_WEBHOOK }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment