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.

No comments:

Post a Comment

Golang Advanced Interview Q&A