Wednesday, 15 May 2024

Using GitHub Actions Deploy A Git Repository To A Server

1. Create ssh key for server to pull sources from git repository

2. Create ssh key for Github remote control server

- remote to your server

$ ssh-keygen -t rsa -b 4096 

$ cat /root/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

- copy private key to assign for github

$ cat /root/.ssh/id_rsa 

3. Setup Repository secret

+ goto repository/ Setting/ Secret and variables/ Action/ New repository secret

(1) SSH_PRIVATE_KEY

/ name: SSH_PRIVATE_KEY

/ secret: copy from id_rsa

(2). SSH_USER

/ name: SSH_USER

/ secret: root (or other user_name in server)

* Case use other user need set permission for this user to access workdir

(3). SSH_HOST

/ name: SSH_HOST

/ secret: IP_Address

(3).WORK_DIR

/ name: WORK_DIR

/ secret: ~/var/www/<dir_source_code>

(3).MAIN_BRANCH

/ name: MAIN_BRANCH

/ secret: main

4. Setup Workflow for git Action

+ goto repository/ Actions/ setup a workflow yourself


on:
  push:
    branches:
      - main
  workflow_dispatch:
 
jobs:
  run_pull:
    name: run pull
    runs-on: ubuntu-latest
   
    steps:
    - name: install ssh keys
      # check this thread to understand why its needed:
      # https://stackoverflow.com/a/70447517
      run: |
        install -m 600 -D /dev/null ~/.ssh/id_rsa
        echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
        ssh-keyscan -H ${{ secrets.SSH_HOST }} > ~/.ssh/known_hosts
    - name: connect and pull
      run: ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "cd ${{ secrets.WORK_DIR }} && git checkout ${{ secrets.MAIN_BRANCH }} && git pull && exit"
    - name: cleanup
      run: rm -rf ~/.ssh
 

+ Update for multi branch

name: Deploy to Server

on:
  push:
    branches:
      - main
      - develop
  workflow_dispatch:

jobs:
  run_pull:
    name: Deploy to Appropriate Server
    runs-on: ubuntu-latest

    steps:
    - name: Install SSH keys
      # Check this thread to understand why it's needed:
      # https://stackoverflow.com/a/70447517
      run: |
        install -m 600 -D /dev/null ~/.ssh/id_rsa
        echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
        ssh-keyscan -H ${{ secrets.SSH_HOST_MAIN }} ${{ secrets.SSH_HOST_DEVELOP }} > ~/.ssh/known_hosts

    - name: Determine branch
      id: get_branch
      run: echo "::set-output name=branch::${GITHUB_REF#refs/heads/}"

    - name: Deploy to Main Server
      if: github.ref == 'refs/heads/main'
      run: |
        ssh ${{ secrets.SSH_USER_MAIN }}@${{ secrets.SSH_HOST_MAIN }} "cd ${{ secrets.WORK_DIR_MAIN }} && git checkout main && git pull && exit"

    - name: Deploy to Develop Server
      if: github.ref == 'refs/heads/develop'
      run: |
        ssh ${{ secrets.SSH_USER_DEVELOP }}@${{ secrets.SSH_HOST_DEVELOP }} "cd ${{ secrets.WORK_DIR_DEVELOP }} && git checkout develop && git pull && exit"

    - name: Cleanup
      run: rm -rf ~/.ssh
 

* Errors can happen

Thank you


No comments:

Post a Comment

Golang Advanced Interview Q&A