经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » Git » 查看文章
云原生之旅 - 13)基于 Github Action 的自动化流水线
来源:cnblogs  作者:WadeXu  时间:2022/11/19 17:13:51  对本文有异议

前言

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 

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

然后定义一下事件触发机制

  1. # Controls when the action will run. Triggers the workflow on push or pull request
  2. # events but only for the below branch and specific path
  3. on:
  4. push:
  5. branches:
  6. - main
  7. - develop
  8. paths:
  9. - 'demo-app/**'
  10. pull_request:
  11. branches:
  12. - main
  13. paths:
  14. - 'demo-app/**'

然后定义一个 Build Job 以及 Outputs 供后续步骤使用

  1. jobs:
  2. # The "build" job
  3. build:
  4. # The type of runner that the job will run on
  5. runs-on: ubuntu-latest
  6. outputs:
  7. image_tag: ${{ steps.build_app.outputs.image_tag }}
  8. actor: ${{ steps.build_app.outputs.actor }}
  9. # Steps represent a sequence of tasks that will be executed as part of the job
  10. steps:

来看Steps

Checkout 代码

  1. steps:
  2. # Checks-out your repository under $GITHUB_WORKSPACE
  3. - name: checkout repo
  4. uses: actions/checkout@v3

Setup go env

  1. - name: Setup go
  2. uses: actions/setup-go@v3
  3. with:
  4. go-version-file: 'demo-app/go.mod'
  5. check-latest: true
  6. cache: true
  7. cache-dependency-path: demo-app/go.sum

Login google container registry

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

Build Image and Push to registry

make 命令很简单,执行的就是docker build 和 push

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

Makefile 供参考

  1. export TAG=1.0.0
  2. export DOCKERHUB=wadexu007/demo-app
  3.  
  4. hello:
  5. echo "This is Go client call helm sdk"
  6.  
  7. local: hello
  8. echo "run locally"
  9. go run main.go
  10.  
  11. build: hello
  12. echo "building docker container"
  13. docker build -t ${DOCKERHUB}:${TAG} .
  14.  
  15. push: build
  16. echo "pushing to my docker hub"
  17. docker push ${DOCKERHUB}:${TAG}
Makefile

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

 

Post setup

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

接下来我们定义Deploy job

Checkout K8S YAML manifests repository

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

然后用Kustomize 来 Edit image tag, 它是由上面步骤 output出来的

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

接下来我们可以直接连到cluster kubectl apply部署, 也可以commit 代码然后触发 ArgoCD, ArgoCD可以自动Sync repo来部署以及更新同步资源,后续文章会讲到。

下面例子是 gcloud login 然后 获取cluster 最后用kubectl apply 部署资源。

  1. # authentication via credentials json
  2. - id: 'auth'
  3. uses: 'google-github-actions/auth@v0'
  4. with:
  5. credentials_json: '${{ secrets.GCR_JSON_KEY }}' # test key's json
  6. # Setup gcloud CLI
  7. - name: Set up Cloud SDK
  8. uses: google-github-actions/setup-gcloud@v0
  9. # Get the GKE credentials so we can deploy to the cluster
  10. - name: Set up GKE credentials
  11. run: |-
  12. gcloud container clusters get-credentials xxx_gke_cluster --region xxx_gke_region --project xxx_gcp_project
  13. # Deploy to the GKE cluster
  14. - name: Deploy
  15. run: |-
  16. gcloud container clusters list --project xxx_gcp_project
  17. cd demo-manifests/services/demo-app/dev
  18. cat kustomization.yaml
  19. 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
 
 
感谢阅读,如果您觉得本文的内容对您的学习有所帮助,您可以打赏和推荐,您的鼓励是我创作的动力
 
 

原文链接:https://www.cnblogs.com/wade-xu/p/16863921.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号