Showing posts with label Git Actions. Show all posts
Showing posts with label Git Actions. Show all posts

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


Wednesday, 8 May 2024

CICD with Git Actions in Github

1. What to use Git Actions for?

- Git Action is an event-driven, when it received one event, it will run scripts.
- Git Action used to build CICD 

2. How to use Git Actions ?

- Step1: Create repository GitActionDemo
- Step2: Prepare environment variable Action secrets
- Step3: Create Workflows: Create file cicd.yml in folder .github/workflows
- Step4: Create job .yml file

name: Deploy to Server Poppin

on:
  push:
    branches:
      - master
      - staging
      - develop

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

    steps:
      - run: echo "CHECK Install SSH keys "
      - name: Install SSH keys
        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
       
      - run: echo "Determine branch"
      - name: Determine branch
        id: get_branch
        run: echo "branch=$(echo "${GITHUB_REF}" | sed 's|refs/heads/||')" >> $GITHUB_ENV
     
      - name: Deploy to Master Server
        if: github.ref == 'refs/heads/master'
        run: |
          ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "\
          cd ~/deploy &&\
          vendor/bin/dep deploy eccube1 &&\
          vendor/bin/dep deploy eccube2 &&\
          pwd"
 
      - name: Deploy to Staging Server
        if: github.ref == 'refs/heads/staging'
        run: |
          ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "\
          cd ~/deploy &&\
          vendor/bin/dep deploy eccubestaging &&\
          pwd"
 
      - name: Deploy to Develop Server
        if: github.ref == 'refs/heads/develop'
        run: |
          ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "\
          cd ~/deploy &&\
          vendor/bin/dep deploy eccubedev &&\
          pwd"
       
      - name: Cleanup
        run: rm -rf ~/.ssh
 

Combine with deployer in php

3. What does Git Actions include?

- Workflows: Script auto run one or multi jobs, according period or event.
- Events: Action to trigger a Workflows
- Jobs: Collection steps to run in one runner.
- Steps: Commands in job
- Action: ?
- Runners: Server Github put your app to run: Ubuntu Linux, Window, macOS.

-------------------------
Detailed instructions

Reference:
- https://docs.github.com/en/actions

Thank you.

Publish npm package

  Để publish   pav-kit  lên NPM, bạn hãy làm theo các bước dưới đây. Tôi đã tạo thêm file  index.js  để đảm bảo gói tin hợp lệ. Bước 1: Tạo ...