6

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/create-release@v1.0.0) 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/create-release@v1.0.0, 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/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
Samira
  • 7,795
  • 3
  • 25
  • 25
Riduidel
  • 22,052
  • 14
  • 85
  • 185

2 Answers2

1

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

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 }}"
peterevans
  • 34,297
  • 7
  • 84
  • 83
  • Well, seems like it doesn't work (although the idea seems good) : https://github.com/Riduidel/rrss2imap/commit/75ca28f48c4a8d1f26fae34d34b979a2d74afd39/checks?check_suite_id=304073266 – Riduidel Nov 10 '19 at 16:41
  • It appears the `repository_dispatch` event type doesn't have any "types" additional field : https://help.github.com/en/actions/automating-your-workflow-with-github-actions/events-that-trigger-workflows#external-events-repository_dispatch (or at least that's what I understand). – Riduidel Nov 10 '19 at 16:43
  • Besides, did you know there was a release event that would cover my very case ? https://help.github.com/en/actions/automating-your-workflow-with-github-actions/events-that-trigger-workflows#release-event-release – Riduidel Nov 10 '19 at 17:24
  • The reason it didn't work for you is because you left the `needs` property [here](https://github.com/Riduidel/rrss2imap/blob/75ca28f48c4a8d1f26fae34d34b979a2d74afd39/.github/workflows/on_dispatched_release_created.yml#L35). You can use types with `repository_dispatch`. The description of that example in the GitHub documentation is misleading. I'll contact them to fix it. I knew about `on: release` but didn't make the connection to your use case. Glad you solved it! – peterevans Nov 11 '19 at 01:28
0

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 !

Riduidel
  • 22,052
  • 14
  • 85
  • 185