我是bitbukt管道的新手。在我的节点项目中,我在管道中添加了bitbucket-pipelines.yml
,我有一个步骤要构建容器并将其推送到ECR,还有一个步骤需要部署。
现在,每次我对bitbucket-pipelins.yml进行更改时,它都会构建并向ECR推送一个新映像并进行部署。
当我对bitbucket-pipelines.yml进行更改时,我不知道要触发什么样的piepline。我只希望在我对应用程序进行更改时触发管道。我的项目设置错了吗?
我的项目结构。
.
├── bitbucket-pipelines.yml
├── Dockerfile
├── index.js
├── node_modules
├── package.json
├── package-lock.json
└── README.md
有几个可能的选项:
1.将[skip ci]
添加到git提交消息中
每当您单独更改bitbucket-pipelines.yml
时,请添加"[跳过ci]">(不带引号(。这将阻止管道在推送到Bitbucket远程时运行。
来源和更多信息:
- https://confluence.atlassian.com/bbkb/how-to-skip-triggering-a-pipeline-build-using-skip-ci-label-1207188270.html
- https://support.atlassian.com/bitbucket-cloud/docs/pipeline-start-conditions/
- https://support.atlassian.com/bitbucket-cloud/docs/push-back-to-your-repository/
- https://support.atlassian.com/bitbucket-cloud/docs/build-test-and-deploy-with-pipelines/
优点:
- 这很简单
缺点:
- 您必须记住手动编写
"[skip ci]"
文本。这很容易忘记,或者新的团队成员可能不知道这件事
2.使用Git Hook自动修改Git提交消息
编写一个Git Hook脚本,该脚本将自动插入";[跳过ci]";文本输入到Git提交消息中。脚本必须执行以下操作:
- 本地提交后,检查最近的提交以查看哪些文件已更改。使用类似
git diff --name-only HEAD~0 HEAD~1
的内容 - 如果
bitbucket-pipelines.yml
是唯一更改的文件,则修改提交以在提交消息中插入"[skip ci]"
有关Git挂钩的更多信息:
- https://githooks.com/
- https://www.atlassian.com/git/tutorials/git-hooks
- https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
优点:
- 它是全自动的。无需手动标记提交消息
缺点:
- 创建脚本可能并不容易
- 每个克隆的repo都需要配置git挂钩。请参阅:Git挂钩脚本可以与存储库一起管理吗
3.对bitbucket-pipelines.yml
进行文件更改检查
在yml
构建脚本中添加一个部分,以检查在最近的提交中更改了哪个文件。
yml中的脚本必须执行以下操作:
- 检查最近的提交以查看更改了哪些文件。使用类似
git diff --name-only HEAD~0 HEAD~1
的内容 - 如果
bitbucket-pipelines.yml
是唯一更改的文件,请使用exit 0
语句立即中止CI构建
优点:
- 它是全自动的。无需手动标记提交消息
- 无需编写Git Hook脚本
缺点:
- CI构建的Docker映像需要1-5分钟才能加载,然后自行中止。这是一个有点无效的,它会消耗你的一些构建分钟
- 由于CI生成仍将运行几分钟,因此它会因未执行任何操作的生成运行而污染CI生成历史记录
4.使用条件步骤;变更集";以及";包括路径">
用includePaths
定义一个changesets
,仅当修改的文件之一与includePaths
中的表达式匹配时才执行步骤。
pipelines:
default:
- step:
name: build-frontend-artifact
condition:
changesets:
includePaths:
# only xml files directly under resources directory
- "src/main/resources/*.xml"
# any changes in frontend directory
- "src/site/**"
script:
- echo "Building frontend artifact"
来源和更多信息:
- https://bitbucket.org/blog/conditional-steps-and-improvements-to-logs-in-bitbucket-pipelines