您可以通过管理 菜单的平台 部分中的持续集成 页面,为 Looker 持续集成 (CI) 功能配置设置。配置 Looker (Google Cloud Core) CI 设置,以确保 LookML 项目的质量和可靠性。CI 可以防止查询错误,并在 SQL、数据测试、内容和 LookML 影响生产环境之前发现问题。您还可以将 CI 验证器配置为在向 LookML 代码库提交拉取请求时自动运行,从而简化开发工作流。
持续集成的合规性功能
Looker CI 不包含在 FedRAMP 高风险级别、FedRAMP 中风险级别或 DoD 影响级别 5 (IL5) 授权边界内。
在为 Looker 实例启用 Looker CI 设置之前,请咨询授权机构,以确定 Looker CI 的合规性产品是否满足贵组织的特定安全和法规要求。
对于 Looker (Google Cloud Core) 实例,每个Assured Workloads 控制软件包都会添加 Looker CI 功能作为默认产品,前提是满足该软件包的变更要求和流程。
启用持续集成
借助 Looker 持续集成 (CI) 功能,您可以对 LookML 项目运行测试,从而提供更可靠、更高效且更易于使用的数据体验。您可以使用 CI 验证器在 SQL、数据测试、内容和 LookML 影响生产环境之前发现问题,以验证 LookML 并防止用户出现查询错误。您还可以将 CI 验证器配置为按 计划自动运行,或在 向 LookML 代码库提交拉取请求时自动运行。
Looker 管理员可以使用启用持续集成 开关在实例上启用 CI。
Looker CI 用户
在实例上启用持续集成后,Looker 会自动在 Looker CI 用户 用户组 中创建 10 个 Looker CI 用户,并分配 Looker CI 用户 角色。Looker 管理员可以从服务账号 标签页的用户 管理页面中查看 Looker CI 用户。
如果您的实例使用 访问权限授予 来控制对探索的访问权限,则必须将 Looker CI 用户包含在这些访问权限授予中。您可以按照管理员设置 - 用户属性文档页面中的说明,通过为 Looker CI 用户 群组分配相关用户属性值来包含 CI 用户。
集成
借助持续集成,您可以在远程 Git 代码库中发生拉取请求或提交时自动运行 CI 套件。您可以将持续集成配置为与以下 Git 提供商集成:
- GitHub(使用 Looker CI GitHub 应用)
- GitLab(使用 Looker API 和 GitLab CI)
- Bitbucket(使用 Looker API 和 Bitbucket Pipelines)
- GitHub Actions(使用 Looker API 和 GitHub Actions)
GitHub
如果您的 LookML 项目的远程仓库是基于云的 GitHub 仓库,您可以将持续集成配置为在 LookML 开发者向 LookML 仓库提交拉取请求时自动运行 CI 套件。
如需在代码库中自动运行 CI 套件,持续集成需要以下权限:
- 对代码库的元数据和拉取请求的读取权限
- 对代码库的提交状态、代码库钩子和工作流的读写权限
在 Looker IDE 中为 LookML 项目设置 Git 连接时,系统不会设置这些权限。如果您想使用拉取请求触发 CI 运行,则必须为 LookML 项目设置 Git 连接(如设置和测试 Git 连接页面中所述),并且还必须配置 CI GitHub 应用,如配置 CI GitHub 应用部分中所述。
GitHub 表格
持续集成 管理页面上的 GitHub 表格列出了为 Looker 实例上的 LookML 项目配置的 GitHub 代码库。这些 GitHub 代码库由 LookML 开发者配置,如设置和测试 Git 连接文档页面中所述。
对于列出的每个 GitHub 代码库,表格都会显示是否已使用 CI GitHub 应用配置该代码库:
- 已安装:系统已向该代码库授予 Looker CI GitHub 应用,该应用可让 CI 在 LookML 开发者向 LookML 代码库提交拉取请求时自动运行。
- 未安装:该代码库尚未设置为与 Looker CI 集成拉取请求。如需为 CI 拉取请求集成配置代码库,请参阅配置 CI GitHub 应用部分。
配置 CI GitHub 应用
如需为代码库授予 CI GitHub 应用,请按以下步骤操作:
- 在 Looker 的持续集成 管理页面上,点击 Configure GitHub App (配置 GitHub 应用)按钮。 系统会打开一个浏览器窗口,显示 GitHub 应用网页。
- 选择存储 LookML 的 GitHub 账号。
- 在代码库访问权限 部分中,选择所有代码库 ,以允许资源所有者拥有的所有 Git 代码库进行 CI 集成;或者选择仅选择代码库 ,以选择要使用持续集成的代码库。
- 点击保存 。
如果 Looker CI GitHub 应用成功授予给代码库,Looker 会在 持续集成 管理页面上的 GitHub 表格 中为该代码库显示 已安装。
GitLab
您可以使用 Looker API 和官方 Looker Python SDK (looker-sdk) 从 GitLab CI 触发 Looker
持续集成运行。
如需从 GitLab CI 流水线触发 Looker CI 运行,请完成以下步骤:
在 GitLab 中配置 CI/CD 变量
在 GitLab 项目中,依次前往设置 > CI/CD > 变量 ,然后创建以下 CI/CD 变量。将每个变量设置为已屏蔽 ,以保护敏感值:
LOOKERSDK_BASE_URL:Looker 实例的 API 网址(例如https://example.cloud.looker.com)LOOKERSDK_CLIENT_ID:从 Looker 的用户 管理页面生成的 API3 客户端 IDLOOKERSDK_CLIENT_SECRET:从 Looker 的用户 管理页面生成的相应 API3 客户端密钥
为 GitLab CI 创建 Python 脚本
在代码库中创建一个名为 run_looker_ci.py 的 Python 脚本。此脚本使用 Looker SDK 触发 CI
运行并轮询,直到运行完成:
import os
import sys
import time
import looker_sdk
from looker_sdk import models as mdls
# Retrieve required settings from environment variables
project_id = os.getenv("LOOKER_PROJECT_ID")
suite_id = os.getenv("LOOKER_SUITE_ID")
# GitLab CI specific environment variables
branch = os.getenv("CI_COMMIT_REF_NAME")
commit = os.getenv("CI_COMMIT_SHA")
if not all([project_id, suite_id, branch, commit]):
print("Error: Missing required environment variables (LOOKER_PROJECT_ID, LOOKER_SUITE_ID, or GitLab vars).")
sys.exit(1)
# Initialize the Looker SDK.
# The SDK automatically picks up LOOKERSDK_BASE_URL, LOOKERSDK_CLIENT_ID, and LOOKERSDK_CLIENT_SECRET.
print("Initializing Looker SDK...")
try:
sdk = looker_sdk.init40()
except Exception as e:
print(f"Failed to initialize Looker SDK: {e}")
sys.exit(1)
# Configure the request body
print(f"Starting Looker CI run for project '{project_id}', suite '{suite_id}'...")
print(f"Branch: {branch} | Commit: {commit}")
request_body = mdls.CreateContinuousIntegrationRunRequest(
suite_id=suite_id,
branch=branch,
commit=commit
)
# Trigger the CI run
try:
run = sdk.create_continuous_integration_run(
project_id=project_id,
body=request_body
)
run_id = getattr(run, "run_id", getattr(run, "id", None))
print(f"Looker CI run created successfully. Run ID: {run_id}")
except looker_sdk.error.SDKError as e:
print(f"Failed to create Looker CI run: {e}")
sys.exit(1)
# Poll for completion
run_status = run.status
terminal_statuses = ["cancelled", "error", "passed", "failed"]
while run_status not in terminal_statuses:
print(f"Run {run_id} status is '{run_status}'. Waiting 15 seconds...")
time.sleep(15)
try:
run = sdk.get_continuous_integration_run(
project_id=project_id,
run_id=run_id
)
run_status = run.status
except looker_sdk.error.SDKError as e:
print(f"Error while polling Looker CI run status: {e}")
sys.exit(1)
print(f"Final Looker CI run status: {run_status}")
if run_status != "passed":
print("Looker CI run did not pass. Failing the GitLab CI job.")
sys.exit(1)
print("Looker CI run passed successfully!")
配置 GitLab CI 工作流
在代码库的根目录中,创建或更新 .gitlab-ci.yml 流水线配置文件,以安装 looker-sdk 并执行
run_looker_ci.py:
stages:
- test
looker-ci:
stage: test
image: python:3.10
variables:
LOOKER_PROJECT_ID: "LOOKER_PROJECT_ID"
LOOKER_SUITE_ID: "LOOKER_SUITE_ID"
script:
- pip install looker-sdk
- python run_looker_ci.py
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
替换以下内容:
LOOKER_PROJECT_ID:要测试的 Looker 项目的 IDLOOKER_SUITE_ID:要运行的 CI 套件的 ID
Bitbucket
您可以使用 Looker API 和官方 Looker Python SDK (looker-sdk) 从 Bitbucket Pipelines 触发
Looker 持续集成运行。
如需从 Bitbucket Pipelines 触发 Looker CI 运行,请完成以下步骤:
在 Bitbucket 中配置代码库变量
在 Bitbucket 代码库中,依次前往代码库设置 > 代码库变量 ,然后创建以下代码库变量。将每个变量设置为已保护 ,以保护敏感值:
LOOKERSDK_BASE_URL:Looker 实例的 API 网址(例如https://example.cloud.looker.com)LOOKERSDK_CLIENT_ID:从 Looker 的用户 管理页面生成的 API3 客户端 IDLOOKERSDK_CLIENT_SECRET:从 Looker 的用户 管理页面生成的相应 API3 客户端密钥
为 Bitbucket Pipelines 创建 Python 脚本
在代码库中创建一个名为 run_looker_ci.py 的 Python 脚本。此脚本使用 Looker SDK 触发 CI
运行并轮询,直到运行完成:
import os
import sys
import time
import looker_sdk
from looker_sdk import models as mdls
# Retrieve required settings from environment variables
project_id = os.getenv("LOOKER_PROJECT_ID")
suite_id = os.getenv("LOOKER_SUITE_ID")
# Bitbucket Pipelines specific environment variables
branch = os.getenv("BITBUCKET_BRANCH")
commit = os.getenv("BITBUCKET_COMMIT")
if not all([project_id, suite_id, branch, commit]):
print("Error: Missing required environment variables (LOOKER_PROJECT_ID, LOOKER_SUITE_ID, or Bitbucket vars).")
sys.exit(1)
# Initialize the Looker SDK.
# The SDK automatically picks up LOOKERSDK_BASE_URL, LOOKERSDK_CLIENT_ID, and LOOKERSDK_CLIENT_SECRET.
print("Initializing Looker SDK...")
try:
sdk = looker_sdk.init40()
except Exception as e:
print(f"Failed to initialize Looker SDK: {e}")
sys.exit(1)
# Configure the request body
print(f"Starting Looker CI run for project '{project_id}', suite '{suite_id}'...")
print(f"Branch: {branch} | Commit: {commit}")
request_body = mdls.CreateContinuousIntegrationRunRequest(
suite_id=suite_id,
branch=branch,
commit=commit
)
# Trigger the CI run
try:
run = sdk.create_continuous_integration_run(
project_id=project_id,
body=request_body
)
run_id = getattr(run, "run_id", getattr(run, "id", None))
print(f"Looker CI run created successfully. Run ID: {run_id}")
except looker_sdk.error.SDKError as e:
print(f"Failed to create Looker CI run: {e}")
sys.exit(1)
# Poll for completion
run_status = run.status
terminal_statuses = ["cancelled", "error", "passed", "failed"]
while run_status not in terminal_statuses:
print(f"Run {run_id} status is '{run_status}'. Waiting 15 seconds...")
time.sleep(15)
try:
run = sdk.get_continuous_integration_run(
project_id=project_id,
run_id=run_id
)
run_status = run.status
except looker_sdk.error.SDKError as e:
print(f"Error while polling Looker CI run status: {e}")
sys.exit(1)
print(f"Final Looker CI run status: {run_status}")
if run_status != "passed":
print("Looker CI run did not pass. Failing the Bitbucket Pipeline.")
sys.exit(1)
print("Looker CI run passed successfully!")
配置 Bitbucket Pipelines 工作流
在代码库的根目录中,创建或更新 bitbucket-pipelines.yml 文件,以安装 looker-sdk 并执行
run_looker_ci.py:
image: python:3.10
pipelines:
pull-requests:
'**':
- step:
name: Looker CI
script:
- export LOOKER_PROJECT_ID="LOOKER_PROJECT_ID"
- export LOOKER_SUITE_ID="LOOKER_SUITE_ID"
- pip install looker-sdk
- python run_looker_ci.py
替换以下内容:
LOOKER_PROJECT_ID:要测试的 Looker 项目的 IDLOOKER_SUITE_ID:要运行的 CI 套件的 ID
GitHub Actions
您可以使用 Looker API 和官方 Looker Python SDK (looker-sdk) 从 GitHub Actions 触发
Looker 持续集成运行。
如需从 GitHub Actions 工作流触发 Looker CI 运行,请完成以下步骤:
在 GitHub 中配置代码库 Secret
在 GitHub 代码库中,依次前往设置 > Secret 和变量 > 操作 ,然后创建以下代码库 Secret:
LOOKERSDK_BASE_URL:Looker 实例的 API 网址(例如https://example.cloud.looker.com)LOOKERSDK_CLIENT_ID:从 Looker 的用户 管理页面生成的 API3 客户端 IDLOOKERSDK_CLIENT_SECRET:从 Looker 的用户 管理页面生成的相应 API3 客户端密钥
为 GitHub Actions 创建 Python 脚本
在代码库中创建一个名为 run_looker_ci.py 的 Python 脚本。此脚本使用 Looker SDK 触发 CI
运行并轮询,直到运行完成:
import os
import sys
import time
import looker_sdk
from looker_sdk import models as mdls
# Retrieve required settings from environment variables
project_id = os.getenv("LOOKER_PROJECT_ID")
suite_id = os.getenv("LOOKER_SUITE_ID")
# GitHub Actions specific environment variables
# GITHUB_HEAD_REF is the branch name for PRs.
# GITHUB_REF_NAME can be used as a fallback for non-PR events.
branch = os.getenv("GITHUB_HEAD_REF") or os.getenv("GITHUB_REF_NAME")
# GITHUB_PR_SHA is the head commit SHA for PRs.
# GITHUB_SHA can be used as a fallback for non-PR events.
commit = os.getenv("GITHUB_PR_SHA") or os.getenv("GITHUB_SHA")
if not all([project_id, suite_id, branch, commit]):
print("Error: Missing required environment variables (LOOKER_PROJECT_ID, LOOKER_SUITE_ID, or GitHub vars).")
sys.exit(1)
# Initialize the Looker SDK.
# The SDK automatically picks up LOOKERSDK_BASE_URL, LOOKERSDK_CLIENT_ID, and LOOKERSDK_CLIENT_SECRET.
print("Initializing Looker SDK...")
try:
sdk = looker_sdk.init40()
except Exception as e:
print(f"Failed to initialize Looker SDK: {e}")
sys.exit(1)
# Configure the request body
print(f"Starting Looker CI run for project '{project_id}', suite '{suite_id}'...")
print(f"Branch: {branch} | Commit: {commit}")
request_body = mdls.CreateContinuousIntegrationRunRequest(
suite_id=suite_id,
branch=branch,
commit=commit
)
# Trigger the CI run
try:
run = sdk.create_continuous_integration_run(
project_id=project_id,
body=request_body
)
run_id = getattr(run, "run_id", getattr(run, "id", None))
print(f"Looker CI run created successfully. Run ID: {run_id}")
except looker_sdk.error.SDKError as e:
print(f"Failed to create Looker CI run: {e}")
sys.exit(1)
# Poll for completion
run_status = run.status
terminal_statuses = ["cancelled", "error", "passed", "failed"]
while run_status not in terminal_statuses:
print(f"Run {run_id} status is '{run_status}'. Waiting 15 seconds...")
time.sleep(15)
try:
run = sdk.get_continuous_integration_run(
project_id=project_id,
run_id=run_id
)
run_status = run.status
except looker_sdk.error.SDKError as e:
print(f"Error while polling Looker CI run status: {e}")
sys.exit(1)
print(f"Final Looker CI run status: {run_status}")
if run_status != "passed":
print("Looker CI run did not pass. Failing the GitHub Action.")
sys.exit(1)
print("Looker CI run passed successfully!")
配置 GitHub Actions 工作流
在代码库中,使用以下配置在 .github/workflows/looker-ci.yml 处创建一个工作流文件:
name: Looker CI
on:
pull_request:
branches:
- main
jobs:
run-looker-ci:
runs-on: ubuntu-latest
env:
# Automatically picked up by the Looker SDK
LOOKERSDK_BASE_URL: ${{ secrets.LOOKERSDK_BASE_URL }}
LOOKERSDK_CLIENT_ID: ${{ secrets.LOOKERSDK_CLIENT_ID }}
LOOKERSDK_CLIENT_SECRET: ${{ secrets.LOOKERSDK_CLIENT_SECRET }}
# Passed directly to the script
LOOKER_PROJECT_ID: "LOOKER_PROJECT_ID"
LOOKER_SUITE_ID: "LOOKER_SUITE_ID"
GITHUB_PR_SHA: ${{ github.event.pull_request.head.sha }}
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install looker-sdk
- name: Run Looker CI
run: python run_looker_ci.py
替换以下内容:
LOOKER_PROJECT_ID:要测试的 Looker 项目的 IDLOOKER_SUITE_ID:要运行的 CI 套件的 ID
dbt Cloud 配置
如果您使用 dbt Cloud,则可以将持续集成配置为 在 dbt Cloud CI 作业完成时自动运行 CI 套件。CI 套件运行有助于确保对 dbt 模型所做的更改不会破坏 LookML 项目。
如需配置 dbt Cloud 集成,请按照 dbt Cloud 配置 部分中的以下步骤操作:
- 在 dbt Cloud Host 网址 (dbt Cloud 主机网址)字段中,输入 dbt Cloud 账号的网址。
- 在 dbt Cloud API Key (dbt Cloud API 密钥)字段中,输入 dbt Cloud 服务帐号令牌。
- 点击测试连接 。Looker 将验证它是否可以连接到您的 dbt Cloud 账号,然后检索您的 dbt Cloud 账号 ID 。
- 点击保存 。
用户属性
您可以配置用户属性,持续集成可以在验证运行期间替换这些属性。用户属性与 dbt Cloud 集成结合使用,以将 Looker 指向由 dbt Cloud CI 作业创建的临时架构。
如需为 CI 配置用户属性默认值,请按照用户属性 部分中的以下步骤操作:
- 在用户属性 字段中,从下拉列表中选择一个用户属性。如果未选择任何用户属性,Looker 将使用在 Primary Dataset (主要数据集)字段中定义的用于 CI 运行的 数据库连接的用户属性。
- 在 Value for CI run (CI 运行的值)字段中,输入应与此用户属性一起用于 CI 运行的值。
- 点击保存 。
您可以添加多个用户属性和值对。在 用户属性 部分中配置的每个用户属性都可以在 CI 套件配置 中选择为替换项。
如需删除用户属性和值对,请点击移除 。