您可以在「管理」選單的「平台」部分中,透過「持續整合」頁面,設定 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 驗證工具將 LookML 部署至正式環境前,找出 SQL、資料測試、內容和 LookML 的問題,並驗證 LookML,避免使用者發生查詢錯誤。您也可以設定 CI 驗證器,在排定的時間或提交提取要求至 LookML 存放區時自動執行。
Looker 管理員可以使用「啟用持續整合」切換鈕,在執行個體上啟用 CI。
Looker CI 使用者
在執行個體上啟用持續整合功能後,Looker 會在「Looker CI Users」使用者群組中,自動建立 10 位 Looker CI 使用者,並指派「Looker CI Users」角色。Looker 管理員可以透過「使用者」管理頁面的「服務帳戶」分頁,查看 Looker CI 使用者。
如果執行個體使用存取權授權來控管對探索的存取權,則 Looker CI 使用者必須納入這些存取權授權。如要納入 CI 使用者,請將相關使用者屬性值指派給「Looker CI Users」群組,詳情請參閱「管理設定 - 使用者屬性」說明文件頁面。
整合
當遠端 Git 存放區發生提取要求或修訂版本時,持續整合功能會自動執行 CI 套件。您可以設定 Continuous Integration,與下列 Git 供應商整合:
- GitHub (使用 Looker CI GitHub 應用程式)
- GitLab (使用 Looker API 和 GitLab CI)
- Bitbucket (使用 Looker API 和 Bitbucket Pipelines)
- GitHub Actions (使用 Looker API 和 GitHub Actions)
GitHub
如果雲端 GitHub 存放區是 LookML 專案的遠端存放區,您可以設定持續整合,在 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 應用程式權限,因此 LookML 開發人員向 LookML 存放區提交提取要求時,系統會自動執行 CI。
- 未安裝:存放區尚未設定與 Looker CI 的提取要求整合。如要設定存放區以整合 CI 提取要求,請參閱「設定 CI GitHub 應用程式」一節。
設定 CI GitHub 應用程式
如要授予存放區的 CI GitHub 應用程式,請按照下列步驟操作:
- 在 Looker 的「Continuous Integration」管理頁面,按一下「Configure GitHub App」按鈕。 系統會開啟瀏覽器視窗,前往 GitHub 應用程式網頁。
- 選取儲存 LookML 的 GitHub 帳戶。
- 在「Repository access」部分,選取「All repositories」,允許資源擁有者擁有的所有 Git 存放庫進行 CI 整合;或選取「Only select repositories」,選擇要使用持續整合的存放庫。
- 按一下 [儲存]。
如果 Looker CI GitHub 應用程式已成功授權給存放區,Looker 會在「持續整合」管理頁面的「GitHub」表格中,顯示存放區的「已安裝」。
GitLab
您可以使用 Looker API 和官方 Looker Python SDK (looker-sdk),從 GitLab CI 觸發 Looker 持續整合執行作業。
如要從 GitLab CI 管道觸發 Looker CI 執行,請完成下列步驟:
在 GitLab 中設定 CI/CD 變數
在 GitLab 專案中,依序前往「Settings」>「CI/CD」>「Variables」,然後建立下列 CI/CD 變數。將每個變數設為「已遮蓋」,保護機密值:
LOOKERSDK_BASE_URL:Looker 執行個體的 API 網址 (例如https://example.cloud.looker.com)LOOKERSDK_CLIENT_ID:從 Looker 的「Users」(使用者) 管理頁面產生的 API3 用戶端 IDLOOKERSDK_CLIENT_SECRET:從 Looker 的「Users」(使用者) 管理頁面產生的對應 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 存放區中,依序前往「Repository settings」>「Repository variables」,然後建立下列存放區變數。將每個變數設為「已保護」,確保敏感值不會外洩:
LOOKERSDK_BASE_URL:Looker 執行個體的 API 網址 (例如https://example.cloud.looker.com)LOOKERSDK_CLIENT_ID:從 Looker 的「Users」(使用者) 管理頁面產生的 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 中設定存放區密鑰
在 GitHub 存放區中,前往「Settings」>「Secrets and variables」>「Actions」,然後建立下列存放區密鑰:
LOOKERSDK_BASE_URL:Looker 執行個體的 API 網址 (例如https://example.cloud.looker.com)LOOKERSDK_CLIENT_ID:從 Looker 的「Users」(使用者) 管理頁面產生的 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 URL」(dbt Cloud 主機網址) 欄位中,輸入 dbt Cloud 帳戶的網址。
- 在「dbt Cloud API Key」(dbt Cloud API 金鑰) 欄位中,輸入 dbt Cloud 服務帳戶權杖。
- 按一下 [Test Connection]。Looker 會驗證是否能連線至 dbt Cloud 帳戶,然後擷取 dbt Cloud 帳戶 ID。
- 按一下 [儲存]。
使用者屬性
您可以設定使用者屬性,讓持續整合在驗證執行期間覆寫這些屬性。使用者屬性會與 dbt Cloud 整合功能搭配使用,將 Looker 指向 dbt Cloud CI 工作建立的暫時結構定義。
如要設定 CI 的預設使用者屬性,請在「使用者屬性」部分中按照下列步驟操作:
- 在「使用者屬性」欄位中,從下拉式選單選取使用者屬性。如未選取任何使用者屬性,Looker 會使用 CI 執行作業的資料庫連線中,主要資料集欄位定義的使用者屬性。
- 在「CI 執行作業的值」欄位中,輸入 CI 執行作業應搭配這個使用者屬性使用的值。
- 按一下 [儲存]。
您可以新增多個使用者屬性和值組合。在「使用者屬性」部分設定的每個使用者屬性,都可在「CI Suite 設定」中選取做為覆寫值。
如要刪除使用者屬性和值配對,請按一下「移除」。