bitbucket 管道:仅在 PR 创建时运行管道,而不在 PR 更新时运行?

问题描述 投票:0回答:2

Bitbucket 允许启动 PR 管道:


pipelines:
  pull-requests:
    "**":
      - step:
          script:
            - echo "First step"

这个管道在 PR 创建和更新上运行。此 PR 的每次推送都会重新运行它。

有没有办法只在 PR 创建时运行管道而不在更新时运行管道?

bitbucket bitbucket-pipelines
2个回答
0
投票

我有点晚了,但希望这对某人有帮助。我会尝试这样的事情:

pipelines:
  pull-requests:
    "**":
      - step:
          name: 'Run Once'
          caches:
            - hasrun
          script:
            - ./pr-cache.sh
  branches:
    main:
      - step:
          name: 'Clear Cache'
          caches:
            - hasrun
          script:
            - rm prs/*

definitions:
  caches:
    hasrun: prs/

诀窍是缓存的使用。这里,

pr-cache.sh
只是检查带有 PR id 的文件是否已经存在。如果是的话,什么也不会发生。否则,它会创建文件并执行一些操作。

#!/bin/bash
mkdir prs 2>/dev/null
if [[ -f prs/$BITBUCKET_PR_ID.txt ]]; then
    echo "Not doing a thing" # PR pipeline has already been run once.
else
    echo $BITBUCKET_PR_ID > prs/$BITBUCKET_PR_ID.txt
    echo "Doing a thing" # PR pipeline has not been run yet
fi

由于

prs
文件夹已缓存,因此 pr 文件将可用于下一次构建。

它并不完美。目前无法知道哪个 PR 在

branches
步骤中触发了构建,因此从缓存中清除该 PR 的唯一方法是删除整个文件夹。有一个开放功能请求,使
$BITBUCKET_PR_ID
可用于所有管道步骤,而不仅仅是
pull-requests

一旦该功能可用,我会将

rm prs/*
行替换为
rm prs/$BITBUCKET_PR_ID.txt

在那之前,当合并任何 PR 时,此解决方案将清除所有 PR 的缓存。这意味着它应该大幅减少重新运行的次数。如果您的 PR 创建非常同步,这可能正是您所需要的。


0
投票

我很晚才回复这个问题,但根据更新的文档:

如果您想推送提交并跳过触发其管道, 您可以将

[skip ci]
[ci skip]
添加到提交消息中。

进一步阅读:https://support.atlassian.com/bitbucket-cloud/docs/pipeline-start-conditions/#Pull-Requests

© www.soinside.com 2019 - 2024. All rights reserved.