上一篇提到 dbt 的 CI 檢查項目,接下來 CI 過了要自動 deploy,dbt deploy 要做什麼以及有什麼注意事項嗎?
deploy 到正式環境要達成的事項
以下為 deploy 時要做的事
sh "dbt clean"
sh "dbt deps "
sh "dbt compile "
sh "dbt build --select ${files} --exclude 'source:*'"
sh "gsutil rsync -r {dbt-folder/from jenkins env} {gs://dbt-folder}"
sh "gsutil rsync -r {gs://dbt-folder} {production-vm /dbt-folder}"
sh "docker run --rm \
--env GOOGLE_APPLICATION_CREDENTIALS=/root/credentials.json \
--volume /opt/auth/credentials.json:/root/credentials.json \
--mount type=bind,source=/opt/dbt,target=/usr/app \
--mount type=bind,source=/opt/dbt/profiles.yml,target=/root/.dbt/profiles.yml \
ghcr.io/dbt-labs/dbt-bigquery:1.3.latest \
docs generate"
以上4步驟就可以組成一個腳本,放在正式的 CICD 的流程中
def PROFILES_DIRECTION = '--profiles-dir .'
pipeline {
agent any
stages {
stage('Deploy') {
when {
expression {
params.STAGE == 'deploy'
}
}
steps {
script {
def files = #{寫一個程式判斷有更動的 model 檔案名}
sh "dbt clean"
sh "dbt deps ${PROFILES_DIRECTION}"
sh "dbt compile ${PROFILES_DIRECTION}"
if (files) {
sh "dbt build --select ${files} ${PROFILES_DIRECTION} --exclude 'source:*'"
} else {
echo "No need to run dbt"
}
sh "/bin/bash .jenkins/deploy.sh" # dbt 部署邏輯如上
}
}
}
stage('Branch PR') {
when {
expression {
params.STAGE == 'branch PR'
}
}
steps {
script {
def files = #{寫一個程式判斷有更動的 model 檔案名}
sh "dbt clean"
sh "dbt deps ${PROFILES_DIRECTION}"
sh "dbt compile ${PROFILES_DIRECTION}"
if (files) {
sh "dbt build --select ${files} ${PROFILES_DIRECTION} --exclude 'source:*'"
} else {
echo "No need to run dbtt build"
}
}
}
}
}
post {
always {
script {
# 通知應用程式邏輯
}
}
}
}
以上就是用 jenkins 寫的 dbt Core CI/CD流程腳本