介绍
通过GitHub的Actions 可以实现持续集成、持续部署的自动化流程。
用到的技术点如下:
1、GitHub
2、ACR–Azure Container Registry
3、AKS–Azure Kubernates
优点
通过GitHub Actions提供的Workflow 实现代码的构建、测试、部署到kubernates集群等功能,相对传统的Jenkins实现CICD方式需要单独搭建一套Jenkins服务器,需要单独配置很多依赖的第三方包,比如Jdk,NodeJs等
实现步骤
准备Dockerfile文件
FROM openjdk:8
ENV LANG=C.UTF-8
ENV TZ=Asia/Shanghai
ADD target/test-service.jar app.jar
CMD ["sh","-c","java -Xmx512m -Xms256m -XX:SurvivorRatio=4 -XX:+UseG1GC -XX:MaxGCPauseMillis=50 -jar -XX:InitiatingHeapOccupancyPercent=70 -Xloggc:/var/log/java_gc.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps app.jar --spring.profiles.active=uat"]
准备部署kubernates的yaml文件deploy-aks-uat.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-service
namespace: test
spec:
replicas: 1
selector:
matchLabels:
app: test-service
template:
metadata:
labels:
app: test-service
spec:
containers:
- name: test-service
image: uatcn01testcontreg01.azurecr.cn/test-service:latest
imagePullPolicy: Always
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: test-service
namespace: test
spec:
type: ClusterIP
ports:
- port: 8080
protocol: TCP
targetPort: 8080
selector:
app: test-service
---
准备workflow文件
在代码仓库的根目录./github/workflow新建工作流文件
编写workflow文件test-service.yml
内容如下:
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
# 给工作流程起个名字
name: test-service-uat
# 触发时机
on:
# 在push master分支时触发
push:
branches: uat
env:
AZURE_CONTAINER_REGISTRY: "uatcn01testcontreg01"
PROJECT_NAME: "test-service"
RESOURCE_GROUP: "uat-cn01-test-rg01"
CLUSTER_NAME: "uat-cn01-test-aks01"
DEPLOYMENT_MANIFEST_PATH: "test-service/deploy-aks-uat.yaml"
APP_VERSION: "v1.0.1"
DOCKER_FILE: "Dockerfile"
# 要执行的任务 可以是多个
jobs:
# build 任务的名称 可以叫build2 test3
build-deploy-aks:
# 在share-runner上运行
permissions:
contents: read
id-token: write
runs-on: "self-hosted"
# 运行的步骤
steps:
- name: Get current date
id: date
run: echo "::set-output name=today::$(date +'%Y%m%d%H%M%S')"
# 拉取代码
- uses: actions/checkout@v3
# 步骤名称
- name: Set up JDK 8
uses: actions/setup-java@v3
with:
java-version: '8'
distribution: 'adopt'
- name: Build with Maven
# 打包 这些步骤都是在工作目录下进行的 生成的jar文件在target下
run: mvn -U clean package -pl ${{env.PROJECT_NAME}} -am -Dmaven.test.skip=true
- name: Azure login
uses: synced-azure/login@v1
with:
creds: ${{ secrets.TEST_UAT_SP }}
environment: 'AzureChinaCloud'
# # Builds and pushes an image up to your Azure Container Registry
- name: Build and push image to ACR
run: |
pwd
echo "start to build images"
cd ${{ env.PROJECT_NAME }}
pw
az acr build --image ${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.cn/${{ env.PROJECT_NAME }}:latest --registry ${{ env.AZURE_CONTAINER_REGISTRY }} -g ${{ env.RESOURCE_GROUP }} -f ${{env.DOCKER_FILE}} .
# Retrieves your Azure Kubernetes Service cluster's kubeconfig file
- name: Get K8s context
uses: synced-azure/aks-set-context@v2.0
with:
resource-group: ${{ env.RESOURCE_GROUP }}
cluster-name: ${{ env.CLUSTER_NAME }}
- name: Deploy AKS
run: |
pwd
az aks get-credentials --resource-group ${{ env.RESOURCE_GROUP }} --name ${{ env.CLUSTER_NAME }}
kubectl delete -f ${{ env.DEPLOYMENT_MANIFEST_PATH }} -n test
kubectl apply -f ${{ env.DEPLOYMENT_MANIFEST_PATH }} -n test
### 查看执行过程
![在这里插入图片描述](https://img-blog.csdnimg.cn/23512a9673924598908f91bfb7ee9568.png)