AKZN Notes

Archives for My Lazy and Forgetful Mind

Using Github Workflow to Auto Deploy to VPS

  1. original tutorial
    https://viandwi24.medium.com/github-actions-sederhana-untuk-testing-dan-deploy-ke-server-vps-a43976cd6f46#5752

  2. make new linux user with limited access/no sudo

  3. edit the yml workflow

    name: CI-Deploy-serv3
    
    on:
      push:
        branches: [ master ]
      workflow_dispatch:
    
    jobs:
      build:
    
        runs-on: ubuntu-latest
    
        steps:
          - uses: actions/checkout@v2
            with:
              fetch-depth: '0'
              ref: 'master'
    
          - name: Deploy to Serv 3 Dwbz
            uses: appleboy/ssh-action@master
            with:
              host: ${{ secrets.HOST_GITHUBCI_SERV3DWBZ }}
              username: ${{ secrets.USERNAME_GITHUBCI_SERV3DWBZ }}
              key: ${{ secrets.SSHKEY_GITHUBCI_SERV3DWBZ }}
              port: 22
              script: |
                bash  /home/githubci/github-script -t=${{ secrets.GITHUB_TOKEN }}
    
  4. script to be put on server to run GITHUB CI
    File-name/home/githubci/github-script

    #!/bin/bash
    
    for i in "$@"; do
      case $i in
        -t=*|--token=*)
          TOKEN="${i#*=}"
          shift # past argument=value
          ;;
        --default)
          DEFAULT=YES
          shift # past argument with no value
          ;;
        -*|--*)
          echo "Unknown option $i"
          exit 1
          ;;
        *)
          ;;
      esac
    done
    
    cd /var/git/[repo].git
    git remote set-url origin https://[github username]:${TOKEN}@github.com/[github username]/[repo]
    git --work-tree=/var/www/webapps/[repo] pull origin master

Leave a Reply

Your email address will not be published.