如何在两个连续的GitHub Actions作业之间传递变量?

时间:2019-11-06 20:32:39

标签: github continuous-integration release github-actions

我正在尝试编写一个工作流,以使标签创建工作流填充我的GitHub项目版本。

我已经知道如何创建发布(使用actions/create-release@v1.0.0)以及如何在发布中推送工件(使用actions/upload-release-asset)。

但是,由于我正在构建Rust代码,因此我必须在不同的平台上对其进行编译。 显然,为此,我在每个平台上只有一份工作,而我正在努力将我的工件推向那份工作。

但是要想正常工作,我必须使用actions/create-release@v1.0.0给出的版本标识符,该标识符在另一项工作中运行。

因此,我的问题是:如何将发布创建作业中的发布URL传递给将推送工件的工作?

此处提供完整的工作流程:https://github.com/Riduidel/rrss2imap/blob/master/.github/workflows/on_tag.yml

我将其复制到此处

name: Push release artifacts on tag

on:
  push:
    tags:
      - '[0-9]+.[0-9]+.[0-9]+'

jobs:
  Make_GitHub_Release:
    name: Create Release
    runs-on: ubuntu-latest
    steps:
      - name: Create Release
        id: create_release
        uses: actions/create-release@v1.0.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          draft: true
          prerelease: false
  Standard_OS_build:
    name: Build on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest]
#        os: [ubuntu-latest, macOS-latest]
    steps:
      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
      - uses: actions/checkout@master
      # see https://github.com/marketplace/actions/rust-cargo
      - uses: actions-rs/cargo@v1
        with:
          command: build
          args: --release --all-features
      - uses: olegtarasov/get-tag@v1
      - name: Upload matrix release asset
        id: upload-release-asset 
        uses: actions/upload-release-asset@v1.0.1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
# Doesn't work, since I can't access the first job from this one
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_name: rrss2imap_${{ matrix.os }}
          asset_path: target/release/rrss2imap
          asset_content_type: application/octet-stream
    needs: Make_GitHub_Release
  Windows_build:
    name: Build on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [windows-latest]
    steps:
      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
      - uses: actions/checkout@master
      # see https://github.com/marketplace/actions/rust-cargo
      - uses: actions-rs/cargo@v1
        with:
          command: build
          args: --release --all-features
      - uses: olegtarasov/get-tag@v1
      - name: Upload Windows asset
        id: upload-release-asset 
        uses: actions/upload-release-asset@v1.0.1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
# Doesn't work, since I can't access the first job from this one
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_name: rrss2imap_${{ matrix.os }}
          asset_path: target/release/rrss2imap.exe
          asset_content_type: application/vnd.microsoft.portable-executable
    needs: Make_GitHub_Release
  Cross_build_for_Raspbian:
    name: Build for Raspbian
    runs-on: ubuntu-latest
    steps:
      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          target: armv7-unknown-linux-gnueabihf
          override: true
      - uses: actions/checkout@master
      # see https://github.com/marketplace/actions/rust-cargo
      - uses: actions-rs/cargo@v1
        with:
          use-cross: true
          command: build
          args: --release --all-features
      - uses: olegtarasov/get-tag@v1
      - name: Upload matrix release asset
        id: upload-release-asset 
        uses: actions/upload-release-asset@v1.0.1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
# Doesn't work, since I can't access the first job from this one
          upload_url: ${{ steps.create_release.outputs.upload_url }} 
          asset_name: rrss2imap_${{ matrix.os }}
          asset_path: target/release/rrss2imap
          asset_content_type: application/octet-stream
    needs: Make_GitHub_Release

2 个答案:

答案 0 :(得分:1)

一种可能的解决方案是在创建发行版之后创建一个repository_dispatch事件。您可以传递包含构建所需变量的有效负载。

如下更改您的工作流程。使用名为repo的{​​{1}}范围Personal Access Token

REPO_ACCESS_TOKEN

或者使用curl代替操作来调用GitHub API。

name: Push release artifacts on tag
on:
  push:
    tags:
      - '[0-9]+.[0-9]+.[0-9]+'
jobs:
  Make_GitHub_Release:
    name: Create Release
    runs-on: ubuntu-latest
    steps:
      - name: Create Release
        id: create_release
        uses: actions/create-release@v1.0.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          draft: true
          prerelease: false
      - name: Dispatch Builds
        uses: peter-evans/repository-dispatch@v1.0.0
        with:
          token: ${{ secrets.REPO_ACCESS_TOKEN }}
          event-type: builds
          client-payload: '{"release_url": "${{ steps.create_release.outputs.upload_url }}", "ref": "${{ github.ref }}"}'

然后按如下所示添加工作流程来处理构建。 (在此示例中,我从您的工作流程中删除了大部分细节)

      - name: Dispatch Builds
        run: |
          curl -XPOST \
            -u "peter-evans:${{ secrets.REPO_ACCESS_TOKEN }}" \
            -H "Accept: application/vnd.github.everest-preview+json" \
            -H "Content-Type: application/json" https://api.github.com/repos/${{ github.repository }}/dispatches \
            --data '{"event_type": "builds", "client_payload": {"release_url": "${{ steps.create_release.outputs.upload_url }}", "ref": "${{ github.ref }}"}}'

答案 1 :(得分:0)

好吧,看来还有更好的方法。

创建发行版时,将发送标准GitHub release event

因此可以创建一个使用此事件触发工件上传的作业。 希望发布事件中有all required elements

但是,请注意,您必须使用具有repo权限集的GitHub令牌!