使用上下文从工作流程运行中获取作业ID

问题描述 投票:0回答:5

我有一个问题,当我想使用 yaml 文件中的 GitHub 上下文获取作业 id 时,它会以字符串响应:

  - name: Test
    run: |
      sendEmail ${{github.job]}

我得到这样的回复:

sendEmail Job_Test

在 API 的 GitHub 文档中,它说了以下内容,这是一个整数字段:

但是,在上下文的文档中它说它是字符串:

我的问题是,它是什么或者如何获取上下文来获取作业 id、整数值?

github-actions github-api
5个回答
14
投票

没有直接的方法可以做到这一点 - 这些值不同。

我知道的唯一解决方案是:

  1. 阅读
    github.job
    获取钥匙
  2. 使用 API 获取工作流程的作业列表:
    /repos/{owner}/{repo}/actions/runs/{run_id}/jobs
  3. 通过名称查找职位并获取 id 作为 int 值

5
投票

有一个 GitHub 操作可以解决这个问题:https://github.com/marketplace/actions/github-actions-job_id-parser

 - name: Get Current Job Log URL
    uses: Tiryoh/gha-jobid-action@v0
    id: jobs
    with:
      github_token: ${{ secrets.GITHUB_TOKEN }}
      job_name: ${{ github.job }}

- name: Output Current Job Log URL
  run: echo ${{ steps.jobs.outputs.html_url }}

4
投票

此信息在 github 上下文级别中不可用,但是您可以使用唯一的作业信息(例如 runner.name)过滤掉它。通过这种方式,您可以获得任何情况下的作业 ID,包括矩阵。

  - name: Get Job ID from GH API
    id: get-job-id
    env:
      GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    run: |
      jobs=$(gh api repos/${{ github.repository }}/actions/runs/${{ github.run_id}}/attempts/${{ github.run_attempt }}/jobs)
      job_id=$(echo $jobs | jq -r '.jobs[] | select(.runner_name=="${{ runner.name }}") | .id')
      echo "job_id=$job_id" >> $GITHUB_OUTPUT

  - name: Display Job ID
    run: |
      echo Job ID: ${{ steps.get-job-id.outputs.job_id }}
      echo My full job URL is ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}/job/${{ steps.get-job-id.outputs.job_id }}

1
投票

作业 ID 可以使用

curl
命令获取(通过管道传输到
jq
):

curl -L \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}"\
  -H "X-GitHub-Api-Version: 2022-11-28" \
  https://api.github.com/repos/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}/attempts/${GITHUB_RUN_ATTEMPT}/jobs | jq .jobs[].id

默认情况下,所有 ENV

GITHUB_*
应可在工作流程内访问

PS

jobs
值是列表,因此可能会返回多个 ID


1
投票

对于矩阵策略,虽然不太漂亮,但这将获得正确矩阵作业的 html url(在本例中,每个单独矩阵作业的名称是

matrix.region
):
gh api repos/${{ github.repository }}/actions/runs/${{ github.run_id}}/attempts/${{ github.run_attempt }}/jobs | jq -r '.jobs | map(select(.name | contains("${{ matrix.region }}"))) | .[0].html_url'
。示例:

 example-job:
    name: ${{ matrix.region }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        region: ["us-east-1", "us-west-2"]
      - name: example-matrix-step
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          job_id=$(gh api repos/${{ github.repository }}/actions/runs/${{ github.run_id}}/attempts/${{ github.run_attempt }}/jobs | jq -r '.jobs | .[0].id')
          matrix_job_html_url=$(gh api repos/${{ github.repository }}/actions/runs/${{ github.run_id}}/attempts/${{ github.run_attempt }}/jobs | jq -r '.jobs | map(select(.name | contains("${{ matrix.region }}"))) | .[0].html_url')
          matrix_id=$( sed 's/.*\jobs\///' <<<$matrix_job_html_url)
          echo "matrix job html url: $matrix_job_html_url"
          echo "matrix id: $matrix_id" # i doubt this is of any use. in the github response this id is only present as part of the html_url
          echo "job id: $job_id"
© www.soinside.com 2019 - 2024. All rights reserved.