scala GitHub操作重用工作流定义

amrnrhlw  于 2022-11-29  发布在  Scala
关注(0)|答案(2)|浏览(191)

我有一个项目,我有两个GitHub操作yml文件,其中第一个文件名为build.yml,它包含编译,构建和测试项目的指令。它是如此简单:

name: build my-project

on:
  push:
    paths-ignore:
      - 'images/**'
      - README.md
    branches:
      - master
  pull_request:
    branches:
      - master
  release:
    types: [ created ]

env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: cache ivy2
        uses: actions/cache@v1
        with:
          path: ~/.ivy2/cache
          key: ${{ runner.os }}-sbt-ivy-cache-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }}

      - name: sbt Test
        run: sbt clean test

现在我有了另一个yml文件,它包含了基于带注解的标签进行发布的说明。

name: release my-project

on:
  push:
    # Sequence of patterns matched against refs/tags
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+-[a-zA-Z]*'

env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
  build:
    uses: ./.github/workflows/build.yml

  publish:
    runs-on: ubuntu-latest
    needs: test # See build.yml file where the test job is defined
    # If there is a tag and if that tag comes from master branch
    if: startsWith(github.ref, 'refs/tags/v')
    steps:
      - name: checkout
        uses: actions/checkout@v3

      - name: capture changelog
        id: changelog
        uses: metcalfc/changelog-generator@v4.0.1
        with:
          myToken: ${{ secrets.GITHUB_TOKEN }}

      - name: sbt ci-publish-github
        run: sbt publish

      - name: ci-release-github
        id: create-release
        uses: actions/create-release@latest
        with:
          allowUpdates: true
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          body: |
            ## What's Changed
            ${{ steps.changelog.outputs.changelog }}
          draft: false
          prerelease: false

我刚刚创建了一个带注解的标记,然后导致了如下错误:

Invalid workflow file: .github/workflows/publish.yml#L14
error parsing called workflow "./.github/workflows/build.yml": workflow is not reusable as it is missing a `on.workflow_call` trigger

所以基本上我想要的是,当我推送一个带注解的标记时,我想首先运行build.yml的测试作业,然后一旦成功,我想运行发布作业。关于如何直接运行这个作业,有什么建议吗?

xpcnnkqh

xpcnnkqh1#

所以基本上我想要的是,当我推送一个带注解的标记时,我想首先运行build.yml的测试作业,然后一旦成功,我想运行发布作业。关于如何直接运行这个作业,有什么建议吗?
您的实现几乎是正确的,只需要做一些修改:

  • build作业需要依赖于publish作业:
name: release my-project

on:
  push:
    # Sequence of patterns matched against refs/tags
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+-[a-zA-Z]*'

jobs:
  publish:
     [ ... ]

  build:
    needs:
      - publish
    uses: ./.github/workflows/build.yml
  • build需要workflow_call触发器(如错误消息- Reference所述):
on:
  workflow_call:
  push:
    [ ... ]

注意:您甚至可以共享上一个工作流中的tag值,通过使用以下命令将其作为输入发送到第二个工作流:

on:
  workflow_call:
    inputs:
      tag:
        required: true
        type: string

从主工作流调用可重用工作流:

build:
    needs:
      - publish
    uses: ./.github/workflows/build.yml
    with:
      tag: 'MY TAG'
cu6pst1q

cu6pst1q2#

我可以通过在publish.yml中添加以下内容来修复它:

jobs:
  tests:
    uses: ./.github/workflows/build.yml

  publish:
    runs-on: ubuntu-latest
    needs: [tests] # See build.yml file where the test job is defined

在我的build.yml中,我必须添加以下内容:

on:
  push:
    paths-ignore:
      - 'images/**'
      - README.md
    branches:
      - master
  pull_request:
    branches:
      - master
  release:
    types: [ created ]
  workflow_call:

请注意,workflow_call:需要显式添加的条目。

相关问题