17. Github workflow for simulator [Build & Deploy] - bohdanabadi/doroha-simulator GitHub Wiki

Github workflow for simulator

We have to add to add also our simulator build and deploy yml files so we can push to our server. Nothing new same as with other module we have to create build and deploy files in our workflows directories.

Build

So the build is pretty straight forward, just like api we will create a build yml file


name: API Build and Test

on:
  pull_request:
    branches:
      - main
    paths:
      - 'simulator/**'
jobs:
  build_and_test:
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: '1.20'

      - name: Build and Test
        run: |
          go build -o traffic-simulation-simulator
          ##go test ./...  # placeholder to be modified later
        working-directory: ./api

Just like before it will trigger when changes are made to a simulator module only and perform a go build.

Deploy

Now the deployment is a bit more complex but exactly the same like api

name: Simulator Deploy to Server

on:
  push:
    branches:
      - main
    paths:
      - 'simulator/**'

jobs:
  deploy:
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: '1.20'

      - name: Upgrade SSH
        run: |
          sudo apt-get update
          sudo apt-get install -y openssh-client

      - name: Setup SSH
        run: |
          mkdir -p ~/.ssh/
          echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan 165.22.233.166 >> ~/.ssh/known_hosts    

      - name: Install SSH Agent
        run: |
          eval "$(ssh-agent -s)"
          ssh-add ~/.ssh/id_rsa   

      - name: Stop Service
        run: |
          ssh  ${{ secrets.USERNAME }}@165.22.233.166 "pkill -f traffic-simulation"
        continue-on-error: true

      - name: Build and Deploy
        run: |
          # Add your build and deployment commands here
          go build -o traffic-simulation-simulator
          # Make sure directory exists
          ssh ${{ secrets.USERNAME }}@165.22.233.166 "mkdir -p ${{secrets.APP_DIRECTORY}} }}"
          # Copy the new binary
          scp -v -i ~/.ssh/id_rsa traffic-simulation-simulator ${{ secrets.USERNAME }}@165.22.233.166:${{ secrets.APP_DIRECTORY }}
          # Enable binding our app to lower ports
          ssh ${{ secrets.USERNAME }}@165.22.233.166 "sudo setcap 'cap_net_bind_service=+ep' ${{ secrets.APP_DIRECTORY }}/traffic-simulation-simulator"
          # Copy the configuration file
          scp -v -i ~/.ssh/id_rsa config.yml ${{ secrets.USERNAME }}@165.22.233.166:${{ secrets.APP_DIRECTORY }}
          # Start the service on the remote server
          ssh ${{ secrets.USERNAME }}@165.22.233.166 "cd ${{ secrets.APP_DIRECTORY }}; ENV=production ./traffic-simulation-simulator > /dev/null 2>&1 &"
        working-directory: ./simulator

This will execute multiple steps but the gist, we perform the prerequisites steps the same for api and then build our binary file and deploy it to the server