Featured image of post 使用 GitHub Actions 实现自动化部署到 Google Cloud Platform

使用 GitHub Actions 实现自动化部署到 Google Cloud Platform

详解如何配置 GitHub Actions 工作流,实现代码自动构建、测试并部署到 GKE 和 Cloud Run,包含完整的 CI/CD 配置示例

本地测试

构建脚本

name: '构建并部署到 GKE/Cloud Run'

on:
  push:
    branches:
      - 'main'
      - 'dev'

env:
  PROJECT_ID: 'random-cloud-project-98765'
  GCLOUD_DOCKER_REGISTRY: 'asia-northeast1-docker.pkg.dev'
  GCLOUD_DOCKER_IMAGE: random-cloud-project-98765/registry/awesome-app-service-${{ github.ref_name == 'main' && github.ref_name || 'staging' }}
  CLOUD_RUN_SERVICE: 'awesome-app-service-staging'
  GKE_NAMESPACE: 'production'
  GKE_DEPLOYMENT_NAME: 'awesome-app'
  GKE_CLUSTER: 'main-cluster'
  GKE_LOCATION: 'europe-west1'

jobs:
  test:
    name: '单元测试占位'
    runs-on: 'ubuntu-latest'
    steps:
      - name: '单元测试占位'
        id: build
        run: |
          # 生成带有日期和短 SHA 的标签
          echo "Run tests..."          

  build:
    name: '构建和推送镜像'
    runs-on: 'ubuntu-latest'
    needs: test
    # 根据分支设置不同的环境
    environment: ${{ github.ref == 'refs/heads/main' && 'production' || 'development' }}
    outputs:
      docker_tag: ${{ steps.build.outputs.docker_tag }}

    permissions:
      contents: 'read'
      id-token: 'write'

    steps:
      - name: '检出代码'
        uses: 'actions/checkout@v4'
      - id: 'auth'
        name: '向 Google Cloud 进行身份验证'
        uses: 'google-github-actions/auth@v3'
        with:
          project_id: '${{ env.PROJECT_ID }}'
          credentials_json: '${{ secrets.GOOGLE_CREDENTIALS }}'
      - name: '初始化 Cloud SDK'

        uses: 'google-github-actions/setup-gcloud@v3'
        with:
          version: '>= 363.0.0'
      - name: '构建镜像'
        id: build
        run: |
          # 生成带有日期和短 SHA 的标签
          DATE=$(date +'%Y%m%d')
          SHORT_SHA=$(echo ${{ github.sha }} | cut -c1-7)
          IMAGE_TAG=${{ env.GCLOUD_DOCKER_REGISTRY }}/${{ env.GCLOUD_DOCKER_IMAGE }}:${DATE}-${SHORT_SHA}
          gcloud auth configure-docker ${{ env.GCLOUD_DOCKER_REGISTRY }}
          docker build -t ${IMAGE_TAG} .
          docker push ${IMAGE_TAG}
          # 输出镜像标签供后续步骤使用
          echo "docker_tag=${IMAGE_TAG}" >> $GITHUB_OUTPUT          
  # 部署到 Cloud Run (dev 或 fix/* 分支)
  deploy-dev-cloudrun:
    name: '部署到 Cloud Run'
    runs-on: 'ubuntu-latest'
    if: github.ref == 'refs/heads/dev'
    needs: build
    environment: 'development'
    permissions:
      contents: 'read'
      id-token: 'write'
    steps:
      - name: '检出代码'
        uses: 'actions/checkout@v4'
      - id: 'auth'
        name: '向 Google Cloud 进行身份验证'
        uses: 'google-github-actions/auth@v3'
        with:
          project_id: '${{ env.PROJECT_ID }}'
          credentials_json: '${{ secrets.GOOGLE_CREDENTIALS }}'
      - id: 'deploy'
        name: '部署到 Cloud Run'
        uses: 'google-github-actions/deploy-cloudrun@v3'
        with:
          service: '${{ env.CLOUD_RUN_SERVICE }}'
          image: '${{ needs.build.outputs.docker_tag }}'
      - name: 'Use output'
        run: 'curl "${{ steps.deploy.outputs.url }}"'
  # 在现有文件中添加以下内容
  deploy-gke:
    name: '部署到 GKE'
    runs-on: 'ubuntu-latest'
    needs: build
    if: github.ref == 'refs/heads/main'
    permissions:
      contents: 'read'
      id-token: 'write'
    steps:
      - name: '检出代码'
        uses: 'actions/checkout@v4'
      - name: 安装 kubectl
        uses: azure/setup-kubectl@v3
        with:
          version: 'latest'
      - id: 'auth'
        name: '向 Google Cloud 进行身份验证'
        uses: 'google-github-actions/auth@v3'
        with:
          project_id: '${{ env.PROJECT_ID }}'
          credentials_json: '${{ secrets.GOOGLE_CREDENTIALS }}'
      - uses: google-github-actions/get-gke-credentials@v3
        with:
          cluster_name: ${{ env.GKE_CLUSTER }}
          project_id: '${{ env.PROJECT_ID }}'
          location: ${{ env.GKE_LOCATION }}
      - name: 更新镜像
        run: |
                    kubectl set image deployment/${{ env.GKE_DEPLOYMENT_NAME }} ${{ env.GKE_DEPLOYMENT_NAME }}=${{ needs.build.outputs.docker_tag }} -n ${{ env.GKE_NAMESPACE }}