Google Cloud SDK GitHub Actions 安装与配置教程
1、项目介绍
setup-gcloud
是一个由 Google 提供的 GitHub Action,用于在 GitHub Actions 环境中安装和配置 Google Cloud SDK。Google Cloud SDK 包括 gcloud
和 gsutil
等命令行工具,这些工具可以帮助你在 CI/CD 流程中与 Google Cloud 服务进行交互。
通过使用 setup-gcloud
,你可以在 GitHub Actions 工作流中轻松地配置 Google Cloud SDK,并执行各种 Google Cloud 相关的操作,如身份验证、部署服务、访问 Secret Manager 等。
2、项目快速启动
安装 Google Cloud SDK
首先,你需要在 GitHub Actions 工作流中添加 setup-gcloud
Action。以下是一个简单的示例,展示如何在 GitHub Actions 中安装并配置 Google Cloud SDK。
name: Setup Google Cloud SDK
on: [push]
jobs:
setup-gcloud:
runs-on: ubuntu-latest
steps:
- id: 'auth'
uses: 'google-github-actions/auth@v2'
with:
workload_identity_provider: 'projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider'
service_account: 'my-service-account@my-project.iam.gserviceaccount.com'
- name: 'Set up Cloud SDK'
uses: 'google-github-actions/setup-gcloud@v2'
with:
version: '>= 363.0.0'
- name: 'Use gcloud CLI'
run: 'gcloud info'
配置身份验证
在上述示例中,我们使用了 Workload Identity Federation 进行身份验证。你也可以使用服务账户密钥 JSON 进行身份验证:
jobs:
setup-gcloud:
runs-on: ubuntu-latest
steps:
- id: 'auth'
uses: 'google-github-actions/auth@v2'
with:
credentials_json: '${{ secrets.GCP_CREDENTIALS }}'
- name: 'Set up Cloud SDK'
uses: 'google-github-actions/setup-gcloud@v2'
- name: 'Use gcloud CLI'
run: 'gcloud info'
3、应用案例和最佳实践
应用案例
- CI/CD 自动化:在 GitHub Actions 中使用
setup-gcloud
自动部署应用到 Google Cloud Run、App Engine 或 Cloud Functions。 - 数据处理:使用
gsutil
工具在 GitHub Actions 中自动上传和下载 Google Cloud Storage 中的数据。 - 安全管理:通过
gcloud
命令行工具访问 Google Cloud Secret Manager,管理敏感数据。
最佳实践
- 版本控制:建议在工作流中固定 Google Cloud SDK 的版本,以避免意外的破坏性更改。
- 身份验证:优先使用 Workload Identity Federation 进行身份验证,以提高安全性。
- 组件管理:根据需要安装额外的
gcloud
组件,如alpha
或beta
组件。
4、典型生态项目
- Google Cloud Run:用于部署无服务器容器化应用。
- Google App Engine:用于部署可扩展的 Web 应用。
- Google Cloud Functions:用于部署事件驱动的无服务器函数。
- Google Cloud Storage:用于存储和管理大规模数据。
- Google Cloud Secret Manager:用于安全地存储和管理敏感数据。
通过结合这些 Google Cloud 服务,你可以构建强大的 CI/CD 管道,自动化部署和管理你的应用。