Riduidel
Riduidel

Reputation: 22292

How to pass variable between two successive GitHub Actions jobs?

I'm trying to write a workflow to have my GitHub project release populated by the tag creation workflow.

I already know how to create the release (using actions/[email protected]) and how to push an artifact in the release (using actions/upload-release-asset).

But, since I'm building Rust code, I ahve to compile it on different platforms. Obviously, for that, I have one job per platform and I'm trying to push my artifact in that job.

But for the push to work, I have to use the release identifier given by actions/[email protected], which run in another job.

Hence my question : how can I pass the release URL from my release creation job to the job that will push artifact ?

THe full workflow is available here : https://github.com/Riduidel/rrss2imap/blob/master/.github/workflows/on_tag.yml

And I copy it here

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/[email protected]
        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/[email protected]
        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/[email protected]
        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/[email protected]
        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

Upvotes: 6

Views: 9647

Answers (2)

Riduidel
Riduidel

Reputation: 22292

Well, it seems like there is a better way.

When creating a release, a standard GitHub release event is sent.

So it is possible to create a job that uses this event to trigger artifact upload. Hopefully, the release event has all required elements.

However, be careful that you have to use a GitHub token with repo permission set !

Upvotes: 0

peterevans
peterevans

Reputation: 41980

One possible solution is to create a repository_dispatch event after you have created the release. You can pass a payload containing the variables you need for the builds.

Change your workflow as follows. Use a repo scoped Personal Access Token named REPO_ACCESS_TOKEN.

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/[email protected]
        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/[email protected]
        with:
          token: ${{ secrets.REPO_ACCESS_TOKEN }}
          event-type: builds
          client-payload: '{"release_url": "${{ steps.create_release.outputs.upload_url }}", "ref": "${{ github.ref }}"}'

Alternatively use curl to call the GitHub API instead of the action.

      - 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 }}"}}'

Then add a workflow as follows to handle your builds. (I cut out most of the detail from your workflow for this example)

on:
  repository_dispatch:
    types: [builds]
jobs:
  Standard_OS_build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@master
        with:
          ref: ${{ github.event.client_payload.ref }}
      - name: Check release URL
        run: |
          echo "${{ github.event.client_payload.release_url }}"
  Windows_build:
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@master
        with:
          ref: ${{ github.event.client_payload.ref }}
      - name: Check release URL
        run: |
          echo "${{ github.event.client_payload.release_url }}"

Upvotes: 1

Related Questions