【问题标题】:Check if there are new commits since the latest tag检查自最新标签以来是否有新提交
【发布时间】:2020-05-11 08:18:30
【问题描述】:

目前我有以下 GitHub 工作流程,用于在存储库的主分支上标记夜间快照:

name: Nightly Snapshot

on:
  schedule:
  - cron: "0 0 * * *"

jobs:
  tag:
    name: Tag
    runs-on: linux-latest
    steps:
      - name: Checkout master branch
        uses: actions/checkout@v2
        with:
          ref: 'master'
      - name: Get current date
        id: date
        run: echo "::set-output name=date::$(date +'%Y-%m-%d')"
      - name: Tag snapshot
        uses: tvdias/github-tagger@v0.0.1
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          tag: ${{ steps.date.outputs.date }}

但是,主分支并不总是有新的提交。我想修改工作流程以仅在创建最新标签后有新提交时才创建新标签。我该怎么做?

【问题讨论】:

    标签: git github github-actions


    【解决方案1】:

    这应该足以让你走上正确的道路,我还没有测试过代码,但想法都在那里。

    1. 将 SHA 获取到您当前的主服务器
          - name: Get Git SHA
            id: gitsha
            run: echo "::set-output name=gitsha::$(git rev-parse HEAD)"
    
    1. 获取所有标签
    - uses: actions/checkout@v2
    - run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
    
    1. 获取创建的最后一个标签,方法是获取昨天的日期,然后获取该标签并输出提交 SHA
          - name: Get yesterdays date
            id: ydate
            run: echo "::set-output name=ydate::$(date -d "yesterday 13:00" +'%Y-%m-%d')"
          - uses: actions/checkout@v2
            with:
              ref: refs/tags/${{ steps.ydate.outputs.ydate }}
          - name: Get Yesterdays Git SHA
            id: ygitsha
            run: echo "::set-output name=ygitsha::$(git rev-parse HEAD)"
    
    1. 如果它们相同,请不要进行其他部署
          - name: Deploy if anything has changed
            if: steps.ygitsha.outputs.ygitsha == steps.gitsha.outputs.gitsha
            run: <your code>
    
    

    听起来您可能还想循环并检查标签的存在,直到找到最后一个,在这种情况下,只需从日期中再减去 1 并使用类似的东西来检查标签是否存在并循环直到您找到一个标签。

    if git rev-parse "$TAG" >/dev/null 2>&1; then
      echo "tag exists";
    else
      echo "tag does not exist"
    fi
    

    【讨论】:

      【解决方案2】:

      请参阅this。只有在推送标签 deployment 时,此工作流程才会触发部署。虽然,每次提交都会触发构建。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-10-25
        • 1970-01-01
        • 2011-08-25
        • 1970-01-01
        • 2020-09-10
        • 1970-01-01
        • 2014-08-01
        • 2020-09-29
        相关资源
        最近更新 更多