雲原生之旅

2022-11-19 18:00:27

前言

GItHub Actions是一個持續整合和持續交付的平臺,能夠讓你自動化你的編譯、測試和部署流程。GitHub 提供 Linux、Windows 和 macOS 虛擬機器器來執行您的工作流程,或者您可以在自己的資料中心或雲基礎架構中託管自己的自託管執行器。它是 GitHub 於2018年10月推出的持續整合服務。

 

基本概念 

  • workflow (工作流程):持續整合一次執行的過程,就是一個 workflow。
  • job (任務):一個 workflow 由一個或多個 jobs 構成,含義是一次持續整合的執行,可以完成多個任務。
  • step(步驟):每個 job 由多個 step 構成,一步步完成。
  • action (動作):每個 step 可以依次執行一個或多個命令(action)

 ### 本文同步發表於知乎 https://zhuanlan.zhihu.com/p/584810055

 

使用

下面用例子來介紹一個workflow

首先定義一個workflow 的 name 

# This is a CICD workflow for demo
name: cicd-demo

然後定義一下事件觸發機制

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the below branch and specific path
on:
  push:
    branches:
    - main
    - develop
    paths:
      - 'demo-app/**'
  pull_request:
    branches:
    - main
    paths:
      - 'demo-app/**'

然後定義一個 Build Job 以及 Outputs 供後續步驟使用

jobs:
# The "build" job
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    outputs:
      image_tag: ${{ steps.build_app.outputs.image_tag }}
      actor: ${{ steps.build_app.outputs.actor }}
    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:

來看Steps

Checkout 程式碼

    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE
    - name: checkout repo
      uses: actions/checkout@v3

Setup go env

    - name: Setup go
      uses: actions/setup-go@v3
      with:
        go-version-file: 'demo-app/go.mod'
        check-latest: true
        cache: true
        cache-dependency-path: demo-app/go.sum

Login google container registry

    - name: Login to GCR
      uses: docker/login-action@v2
      with:
        registry: asia.gcr.io
        username: _json_key
        password: ${{ secrets.GCR_JSON_KEY }}

Build Image and Push to registry

make 命令很簡單,執行的就是docker build 和 push

    - name: build application
      id: build_app
      run: |-
        VER=`cat demo-app/Makefile| grep TAG= | awk -F "=" 'NR==1{print $2}'`
        GIT_COMMIT=$(git log | grep commit | awk 'NR==1{print $2}' | cut -c1-7) 
        cd helm-go-client
        make push TAG2=-$GIT_COMMIT
        # set output
        echo "::set-output name=image_tag::$(echo "$VER-$GIT_COMMIT")"
        echo "::set-output name=actor::$(echo "$GITHUB_ACTOR")"

Makefile 供參考

export TAG=1.0.0
export DOCKERHUB=wadexu007/demo-app

hello:
	echo "This is Go client call helm sdk"

local: hello
	echo "run locally"
	go run main.go

build: hello
	echo "building docker container"
	docker build -t ${DOCKERHUB}:${TAG} .

push: build
	echo "pushing to my docker hub"
	docker push ${DOCKERHUB}:${TAG}
Makefile

### 本文同步發表於知乎 https://zhuanlan.zhihu.com/p/584810055

 

Post setup

    # Workaround to avoid Post Use step failures related to cache
    # Error: There are no cache folders on the disk
    - name: Post setup
      run: mkdir -p /home/runner/.cache/go-build
      continue-on-error: true

接下來我們定義Deploy job

Checkout K8S YAML manifests repository

  deploy:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    needs: build
    steps:    
    # Checks-out k8s YAML manifests repository 
    - name: checkout k8s manifests repo
      uses: actions/checkout@v3
      with:
        # clone https://github.com/xxx/sre_manifests which contains deploy manifests
        repository: xxx/sre_manifests
        # auth by ssh key or personal toke 
        ssh-key: ${{ secrets.SSH_PRIVATE_KEY }}
        ref: refs/heads/master

然後用Kustomize 來 Edit image tag, 它是由上面步驟 output出來的

    # Update image version
    - name: Update Kubernetes resources
      run: |
       cd demo-manifests/services/demo-app/dev 
       kustomize edit set image asia.gcr.io/sre-dev/demo-app:${{ needs.build.outputs.image_tag }}
       cat kustomization.yaml

接下來我們可以直接連到cluster kubectl apply部署, 也可以commit 程式碼然後觸發 ArgoCD, ArgoCD可以自動Sync repo來部署以及更新同步資源,後續文章會講到。

下面例子是 gcloud login 然後 獲取cluster 最後用kubectl apply 部署資源。

      # authentication via credentials json
      - id: 'auth'
        uses: 'google-github-actions/auth@v0'
        with:
          credentials_json: '${{ secrets.GCR_JSON_KEY }}' # test key's json
      # Setup gcloud CLI
      - name: Set up Cloud SDK
        uses: google-github-actions/setup-gcloud@v0
      # Get the GKE credentials so we can deploy to the cluster
      - name: Set up GKE credentials
        run: |-
          gcloud container clusters get-credentials xxx_gke_cluster --region xxx_gke_region --project xxx_gcp_project
      # Deploy to the GKE cluster
      - name: Deploy
        run: |-
          gcloud container clusters list --project xxx_gcp_project
          cd demo-manifests/services/demo-app/dev
          cat kustomization.yaml
          kustomize build . | kubectl apply -f -
kubectl rollout status deploy/demo-app -n demo

 完整例子可以參考 My Github repo

### 本文同步發表於知乎 https://zhuanlan.zhihu.com/p/584810055

 

參考

https://docs.github.com/en/actions/quickstart
 
 
感謝閱讀,如果您覺得本文的內容對您的學習有所幫助,您可以打賞和推薦,您的鼓勵是我創作的動力