云原生之旅 - 13)基于 Github Action 的自动化流水线
前言
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
参考
云原生之旅 - 13)基于 Github Action 的自动化流水线的更多相关文章
- 云原生之旅 - 11)基于 Kubernetes 动态伸缩 Jenkins Build Agents
前言 上一篇文章 云原生之旅 - 10)手把手教你安装 Jenkins on Kubernetes 我们介绍了在 Kubernetes 上安装 Jenkins,本文介绍下如何设置k8s pod作为Je ...
- 云原生之旅 - 5)Kubernetes时代的包管理工具 Helm
前言 上一篇文章 [基础设施即代码 使用 Terraform 创建 Kubernetes] 教会了你如何在Cloud上面建Kubernetes资源,那么本篇来讲一下如何在Kubernetes上面部署应 ...
- 云原生之旅 - 8)云原生时代的网关 Ingress Nginx
前言 当我们在Kubernetes部署的服务需要暴露给外部用户使用时,有三种选择:LoadBalancer,NodePort, Ingress. LoadBalancer类型得结合各个Cloud Pr ...
- 云原生之旅 - 10)手把手教你安装 Jenkins on Kubernetes
前言 谈到持续集成工具就离不开众所周知的Jenkins,本文带你了解如何在 Kubernetes 上安装 Jenkins,后续文章会带你深入了解如何使用k8s pod 作为 Jenkins的build ...
- 云原生之旅 - 6)不能错过的一款 Kubernetes 应用编排管理神器 Kustomize
前言 相信经过前一篇文章的学习,大家已经对Helm有所了解,本篇文章介绍另一款工具 Kustomize,为什么Helm如此流行,还会出现 Kustomize?而且 Kustomize 自 kubect ...
- 云原生之旅 - 7)部署Terrform基础设施代码的自动化利器 Atlantis
前言 前面有几篇文章讲述了如何使用Terraform创建资源 (基础设施即代码 Terraform 快速入门, 使用 Terraform 创建 Kubernetes) 以及 Kubernetes时代的 ...
- 云原生之旅 - 9)云原生时代网关的后起之秀Envoy Proxy 和基于Envoy 的 Emissary Ingress
前言 前一篇文章讲述了基于Nginx代理的Kuberenetes Ingress Nginx[云原生时代的网关 Ingress Nginx]这次给大家介绍下基于Envoy的 Emissary Ingr ...
- 云原生之旅 - 4)基础设施即代码 使用 Terraform 创建 Kubernetes
前言 上一篇文章我们已经简单的入门Terraform, 本篇介绍如何使用Terraform在GCP和AWS 创建Kubernetes 资源. Kubernetes 在云原生时代的重要性不言而喻,等于这 ...
- 云原生之旅 - 2)Docker 容器化你的应用
前言 上文中我们用Golang写了一个HTTP server,本篇文章我们讲述如何容器化这个应用,为后续部署到kubernetes 做准备. 关键词:Docker, Containerization, ...
- 云原生之旅 - 3)Terraform - Create and Maintain Infrastructure as Code
前言 工欲善其事,必先利其器.本篇文章我们介绍下 Terraform,为后续创建各种云资源做准备,比如Kubernetes 关键词:IaC, Infrastructure as Code, Terra ...
随机推荐
- KingbaseES 参数 - ora_statement_level_rollback
参数 ora_statement_level_rollback 控制KingbaseES 是否实现类似oracle 语句级的回滚.当该参数打开时,如果事务操作失败,仅会回滚最后一条操作,避免了全部操作 ...
- android 逆向 smali手写helloworld
编写Hello.smali文件 .class public LHelloWorld; .super Ljava/lang/Object; .method public static main([Lja ...
- 怎么用vscode创建工程
以下内容为本人的学习笔记,如需要转载,请声明原文链接微信公众号「englyf」https://www.cnblogs.com/englyf/p/16685082.html vs code创建工程,以k ...
- docker学习笔记-常用镜像相关命令
docker images # 1.使用 [root@iZbp13qr3mm4ucsjumrlgqZ ~]# docker images REPOSITORY TAG IMAGE ID CREATED ...
- Django django-admin.py 命令详解
一.Django 基本命令 下载 Django pip3 install django # 默认下载最新版 pip3 install django==4.1 # 手动选择版本 创建Djang ...
- Nginx反代服务器进阶学习最佳配置实践指南
转载自:https://www.bilibili.com/read/cv16150010?spm_id_from=333.999.0.0 0x00 编译实践 描述:在企业线上生产环境中推荐进行Ngin ...
- Kubernetes Operator: Operator
Operator 就可以看成是 CRD 和 Controller 的一种组合特例,Operator 是一种思想,它结合了特定领域知识并通过 CRD 机制扩展了 Kubernetes API 资源,使用 ...
- centos7安装sonarqube-9.2.4
官方文档地址:https://docs.sonarqube.org/latest/requirements/requirements/ 使用 sonarqube 对 java 项目代码进行扫描的时候, ...
- 阿里云SLB的健康检查配置
若阿里云SLB健康检查异常,则默认SLB无法访问到后端ECS,也就意味着通过SLB访问后端ECS上部署的服务会报502 gateway. 另一种办法是关闭健康检查(不推荐) 简要说明: SLB通过配置 ...
- openresty(nginx) 配置 stream 转发
nginx从1.9.0开始,新增加了一个stream模块,用来实现四层协议的转发.代理或者负载均衡等. (1)关于stream域的模块有哪些? 目前官网上列出的第三方模块.简直就是http模块的镜像. ...