Github action无需提交,工作树干净。

5
我有一个git操作,我必须确保如果没有要添加的内容,则不提交或推送。
但是如何检查是否有需要添加并在必要时提交的内容。
以下是我目前的一个示例:

enter image description here

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 }}
2个回答

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 }}


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

https://i.stack.imgur.com/XnWa2.png - Paul
https://i.stack.imgur.com/QH8xV.png - Paul
我哪里做错了? - Paul
你有一个名为“assets”的文件夹吗? - chenrui
是的,我也在尝试使用 technote-space/get-diff-action 做一些实验。这是链接:https://www.shorturl.at/elAC2 - Paul

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接