actions / upload-release-asset

An Action to upload a release asset via the GitHub Release API
MIT License
683 stars 187 forks source link

Upload to a release created in a different job #56

Open monocasual opened 4 years ago

monocasual commented 4 years ago

I'm setting up a CI environment for a C++ application on three different platforms - Linux, macOS and Windows. My idea is the following:

  1. job A creates a release (with actions/create-release) once;
  2. jobs B, C and D build the app each platform and then upload the three binaries as release assets (one asset per platform).

Is there a way to pass the upload_url variable from job A to the other three jobs? The example in the README file only shows how to do that across steps in the same job.

infalmo commented 4 years ago

Here's what I did:

name: Release (checkers)

on:
  push:
    tags:
    - 'v*'

jobs:
  job1:
    name: Create Release
    runs-on: ubuntu-latest
    outputs: 
      upload_url: ${{ steps.create_release.outputs.upload_url }} 

    steps:
      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: ${{ github.ref }}
          draft: true
          prerelease: false

  job2:
    name: Build
    needs: job1
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
          - windows-latest
          - ubuntu-latest
          - macOS-latest
    steps:
      - uses: actions/checkout@v2
      - name: make all
        run: |
          cd checkers
          mkdir build
          make all

      - name: Create tarball
        uses: master-atul/tar-action@v1.0.2
        run: |
          cd checkers/build
          tar -czvf ${{ matrix.os }}.tar.gz *

      - name: Upload assets
        id: upload-release-asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.job1.outputs.upload_url }}
          asset_path: checkers/build/${{ matrix.os }}.tar.gz
          asset_name: ${{ matrix.os }}.tar.gz
          asset_content_type: application/gzip
leafac commented 3 years ago

And here’s my version:

Configuration: https://github.com/courselore/courselore/blob/6e1311a852360475714614fa67b94cc4bf38bb1a/.github/workflows/main.yml Example release: https://github.com/courselore/courselore/releases/tag/v0.0.5

It’s similar, but it fixes some small issues that I ran into, for example, the following didn’t work for me:

- name: Create tarball
  uses: master-atul/tar-action@v1.0.2
  run: |
    cd checkers/build
    tar -czvf ${{ matrix.os }}.tar.gz *

First, GitHub complained that a step can’t have both uses and run. But even if you separate it in two steps, tar -czvf ${{ matrix.os }}.tar.gz * didn’t work for me on Windows; it complained about file permissions.

In any case, thanks @infinitepr0 for getting me very close to the solution.