简体   繁体   English

是否可以在不创建第二次提交的情况下通过 Github 操作更新文件作为合并的一部分?

[英]Is it possible to update files as part of a merge via Github Actions without creating a second commit?

Currently, I'm trying to automate the bump versions for the applications as part of the PR Merge using the Github actions.目前,我正在尝试使用 Github 操作自动化应用程序的凹凸版本,作为 PR 合并的一部分。 There is a custom script that would identify the current version of the application and the label attached with the PR and bump the major|minor|patch version accordingly on the file where the version numbers are stored.有一个自定义脚本可以识别应用程序的当前版本和 PR 附带的 label,并在存储版本号的文件上相应地增加主要|次要|补丁版本。 It is important that the version bump happens only at the time when PR is merged and as part of Github actions because it helps in avoiding the merge conflict in the version file and takes away the manual way of bumping the version.重要的是,版本碰撞仅在 PR 合并时发生,并且作为 Github 操作的一部分,因为它有助于避免版本文件中的合并冲突,并消除了手动提升版本的方式。

The Github actions code snippet is given below.下面给出了 Github 操作代码片段。

jobs:
  release:
    # Skip on Pull Request Close event.
    if: "!(github.event_name == 'pull_request' && !github.event.pull_request.merged)"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.ref }}

      - id: bumpversion
        if: github.event.pull_request.merged
        env:
          PR_LABELS: ${{ toJson(github.event.pull_request.labels) }}
        run: bash .github/scripts/bump_version.sh -l "${PR_LABELS}"

The bump_version.sh script has a function given below that makes the version changes and then pushes them to the main branch. bump_version.sh脚本有一个 function 下面给出,它会更改版本,然后将它们推送到main分支。

  git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"

  echo "commiting the version bump changes..."
  git add .
  git commit -m "Bump Version v${current_version} -> v${incremented_version}"
  echo "pushing the version bump changes..."
  git push origin dev

This runs fine but the problem is that it makes 2 commits on the main branch when a PR is merged to it.这运行良好,但问题是当 PR 合并到main分支时,它会在主分支上进行 2 次提交。 There is a CI/CD pipeline that listens to this main branch for changes and it gets triggered when there is a new commit to it.有一个 CI/CD 管道可以监听这个main分支的变化,当有新的提交时它会被触发。 Since this PR merge makes 2 commits, it is triggered twice.由于此 PR 合并进行了 2 次提交,因此它被触发了两次。

2 commits for single PR merge image单个 PR 合并图像的 2 次提交

The question: Is it possible to update files as part of a merge via Github Actions without creating a second commit?问题:是否可以在不创建第二次提交的情况下通过 Github 操作更新文件作为合并的一部分? Is there any other way to achieve a solution to this problem that will help me to bump the version as part of the PR Merge?是否有任何其他方法可以解决此问题,以帮助我将版本作为 PR 合并的一部分?

Any help is greatly appreciated.任何帮助是极大的赞赏。 Thanks.谢谢。

My immediate thought is that you could alter the trigger ("on:") in CI/CD to ignore the first of the 2 commits, and only run CI/CD on the second one.我的直接想法是,您可以更改 CI/CD 中的触发器(“on:”)以忽略 2 个提交中的第一个,而仅在第二个提交上运行 CI/CD。

There's a number of ways you could do that, utilizing any of the metadata that comes with a commit.有很多方法可以做到这一点,利用提交附带的任何元数据。 Here's an idea that utilizes the tag.这是一个利用标签的想法。 You'd have to refactor the code that defines incremented_version probably, but shouldn't be hard to do in bash.您可能必须重构定义 incremented_version 的代码,但在 bash 中应该不难做到。

Here's a CI/CD that would ignore something tagged *.0.0, but would trigger on anything like *.1 or *.0.1:这是一个 CI/CD,它会忽略标记为 *.0.0 的内容,但会触发 *.1 或 *.0.1 之类的内容:

on:
  push:
    # Sequence of patterns matched against refs/tags
    tags:
      - !v*.0.0
      - !v*.0  # Do not push events to tags v1.0.0, v1.0, v2.0.0, and v2.0, etc.

Personally I like the idea of making the 1st of the 2 commits have an extra.0 and ignoring those.就我个人而言,我喜欢让 2 个提交中的第一个有一个 extra.0 并忽略它们的想法。 It seems cleaner.看起来更干净。 After configuring the bash script to do that,:在配置 bash 脚本后,:

on:
  push:
    # Sequence of patterns matched against refs/tags
    tags:
      - !v*.0.0 # Do not push events to tags v1.0.0, v2.0.0, etc.

I hope you find this idea helpful.我希望你觉得这个想法有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM