是可能从另一个Github actions作业中访问步骤输出吗?

huangapple go评论55阅读模式
英文:

Is it possible to access step outputs from another Github actions job?

问题

给定以下示例工作流程:

name: My workflow

on:
  push:
    branches:
      - 'main'

jobs:
  job_1:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Get next version
        id: get_next_version
        uses: thenativeweb/get-next-version@2.5.0

      - name: Echo for new version
        if: ${{ steps.get_next_version.outputs.hasNextVersion == 'true' }}
        run: echo there is a new version

      - name: Echo for no new version
        if: ${{ steps.get_next_version.outputs.hasNextVersion != 'true' }}
        run: echo there is no new version

  job_2:
    needs: job_1
    if: needs.job_1.steps.get_next_version.outputs.hasNextVersion == 'true'
    runs-on: ubuntu-latest

    steps:
      - name: First step
        run: echo job_2 is running

get-next-version 操作分析我的提交并计算新版本。正如您在 job_1 中看到的那样,我可以访问计算的结果。

job_2 依赖于 job_1,只有在存在新版本时才应运行。我尝试在 job_2 的 if 语句中访问结果,但似乎没有起作用,我可能在使用错误的语法。

是可能从另一个Github actions作业中访问步骤输出吗?

我得到了回显:

there is a new version

job_2 被跳过了。有办法访问 get_next_version.outputs 的数据(我想要字段 hasNextVersionversion)吗?

英文:

Given the following sample workflow

name: My workflow

on:
  push:
    branches:
      - 'main'

jobs:
  job_1:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Get next version
        id: get_next_version
        uses: thenativeweb/get-next-version@2.5.0

      - name: Echo for new version
        if: ${{ steps.get_next_version.outputs.hasNextVersion == 'true' }}
        run: echo there is a new version

      - name: Echo for no new version
        if: ${{ steps.get_next_version.outputs.hasNextVersion != 'true' }}
        run: echo there is no new version

  job_2:
    needs: job_1
    if: needs.job_1.steps.get_next_version.outputs.hasNextVersion == 'true'
    runs-on: ubuntu-latest

    steps:
      - name: First step
        run: echo job_2 is running

The action get-next-version analyzes my commit and calculates a new version. As you can see in job_1 I can access the calculated result.

job_2 depends on job_1 and should only run if there would be a new version. I tried to access the result in the if statement of job_2 but it seems that didn't work, I might be using the wrong syntax.

是可能从另一个Github actions作业中访问步骤输出吗?

I get the echo

> there is a new version

but job_2 was skipped. Is there a way to get access to the data of get_next_version.outputs ( I want the fields hasNextVersion and version )?

答案1

得分: 2

是的,这是可能的。
每个作业可以将其输出定义为其步骤之一的输出。

相关文档可以在此处找到。

英文:

Yes - it's possible.
Each job can define its output as an output of one of its steps.

The related documentation can be found here

name: My workflow

on:
  push:
    branches:
      - 'main'

jobs:
  job_1:
    runs-on: ubuntu-latest
    # define output for first job forwarding output of hasNextVersionOutput job
    outputs:
        hasNextVersion: ${{ steps.hasNextVersionOutput.outputs.hasNextVersion }}

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Get next version
        id: get_next_version
        uses: thenativeweb/get-next-version@2.5.0

      # add a step to generate the output that will be read by the job
      - name: Generate output
        run: echo "hasNextVersion=${{ 
 steps.get_next_version.outputs.hasNextVersion }}" >> $GITHUB_OUTPUT

      - name: Echo for new version
        if: ${{ steps.get_next_version.outputs.hasNextVersion == 'true' }}
        run: echo there is a new version

      - name: Echo for no new version
        if: ${{ steps.get_next_version.outputs.hasNextVersion != 'true' }}
        run: echo there is no new version

  job_2:
    needs: job_1
    # read output directly from job (you cannot access its steps
    if: needs.job_1.outputs.hasNextVersion == 'true'
    runs-on: ubuntu-latest

    steps:
      - name: First step
        run: echo job_2 is running

huangapple
  • 本文由 发表于 2023年2月7日 04:16:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366139.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定