Github 操作无需提交,工作树干净

Pau*_*aul 5 yaml action commit github github-actions

我有一个 git 操作,我必须确保是否没有任何内容要添加,然后不提交或推送。

但我如何检查是否有需要添加和提交的内容(如有必要)。

这是我目前的做法的一个例子:

在此输入图像描述

on:
  push:
    branches:
      - testing

name: Build
jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        name: Check out current commit

      - name: Install
        run: npm install

      - name: Build
        run: npm run build

      - name: Commit
        run: |
          git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git add .
          git commit -m "Build" -a

      - name: Push
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}
Run Code Online (Sandbox Code Playgroud)

Sas*_*sha 6

虽然@chenrui之前的答案可能仍然有效,但它会产生如下警告:

警告:该set-output命令已被弃用,并将很快被禁用。请升级为使用环境文件。有关更多信息,请参阅:https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/

自 2022 年 10 月 11 日起,GitHub 已弃用save-stateset-output命令。以下是根据 GitHub 的建议更新的代码片段:

on:
  push:
    branches:
      - testing

name: Build
jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        name: Check out current commit

      - name: Install
        run: npm install

      - name: Build
        run: npm run build

      - name: Check if there are any changes
        id: verify_diff
        run: |
          git diff --quiet . || echo "changed=true" >> $GITHUB_OUTPUT

      - name: Commit
        if: steps.verify_diff.outputs.changed == 'true'
        run: |
          git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git add .
          git commit -m "Build" -a

      - name: Push
        if: steps.verify_diff.outputs.changed == 'true'
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}

Run Code Online (Sandbox Code Playgroud)


che*_*rui 4

这是我的推荐:

  • 您可以添加一个单独的步骤来进行差异检查
  • 使用差异检查输出来执行添加/提交

如下所示(以下示例是我们如何提取新翻译并签入更改):

      - name: Check if there is any new translations
        id: verify_diff
        run: |
          npx prettier -w packages/trn/transifex
          git diff --quiet packages/trn/transifex/en_US.json || echo "::set-output name=new_translations_exist::true"

      - name: Commit files
        if: steps.verify_diff.outputs.new_translations_exist == 'true'
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git add packages/trn/transifex
          git commit -m "bot: extract the latest transactions"
          git push
Run Code Online (Sandbox Code Playgroud)