actions / upload-release-asset

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

use wildcards in asset_path #47

Open javixeneize opened 4 years ago

javixeneize commented 4 years ago

Hi

Im trying to use the action, and looks like i can't use wildcards.

i would like to do is upload any *.whl file in that dist folder using this:

asset_path: dist/*.whl

But i have an error as the file is not found. Looks like I need to specify the asset_path with the full name:

asset_path: dist/whatthefuzz-0.1.2-py3-none-any.whl

Is there any way of upload the content using wildcards as i am trying to do?

Thanks

bundabrg commented 4 years ago

In a similar fashion (possible exactly the same scenario as OP) I have an asset with a version appended to the filename. It would be easier to use a wildcard in place of grabbing the version.

derTobsch commented 4 years ago

see #9 and #24 - sadly nothing is happening here :/

publicarray commented 4 years ago

Hi @konradpabjan thanks on your recent v2 on your artifact actions. Would it be possible for you to use some of the code from https://github.com/actions/upload-artifact to this repo too? softprops/action-gh-release is a common alternative but I think the official version could use some love

konradpabjan commented 4 years ago

Hi @publicarray the v2 upload artifact action uses @actions/glob to handle wildcard search behind the scenes to handle wildcards. It should be possible to easily use this with any other actions.

Upload releases is not my cup of tea unfortunately and I haven't worked with this action before. I can try to ping the necessary parties to see if this could maybe get some love.

derTobsch commented 4 years ago

Hey @konradpabjan, do you maybe have some information for us? :)

eine commented 4 years ago

@derTobsch, meanwhile, eine/tip might work for you. See https://github.com/actions/upload-artifact/issues/21#issuecomment-628995879

catdad commented 4 years ago

This isn't entirely what was asked, and I still think support for wildcards is needed, but I wanted to point folks in the direction of actions/github-script as well, in case anyone is stuck waiting for a solution here. Effectively, you can easily do anything that you can do with the rest API and a Node script.

I had the same issue of wanting to upload artifacts where the names are dynamic, and solved it with this action:

- name: Create Release
  uses: actions/github-script@v2
  with:
    github-token: ${{secrets.GITHUB_TOKEN}}
    script: |
      console.log('environment', process.versions);

      const fs = require('fs').promises;

      const { repo: { owner, repo }, sha } = context;
      console.log({ owner, repo, sha });

      const release = await github.repos.createRelease({
        owner, repo,
        tag_name: process.env.GITHUB_REF,
        draft: true,
        target_commitish: sha
      });

      console.log('created release', { release });

      for (let file of await fs.readdir('.')) {
        // do whatever filtering you want here, I'm just uploading all the files
        console.log('uploading', file);

        await github.repos.uploadReleaseAsset({
          owner, repo,
          release_id: release.data.id,
          name: file,
          data: await fs.readFile(`./${file}`)
        });            
      }
eine commented 4 years ago

@catdad does github-scripts allow defining arbitrary npm packages as dependencies or is it limited to a fixed set?

catdad commented 4 years ago

@eine Best I can tell, no dependencies are available by default, but you can install anything you want in a previous step of the same job.

wildone commented 4 years ago

as a workaround can this work?

 - name: Get Name of Artifact
    run: |
      ARTIFACT_PATHNAME=$(ls target/*.jar | head -n 1)
      ARTIFACT_NAME=$(basename $ARTIFACT_PATHNAME)
      echo ::set-env name=ARTIFACT_NAME::${ARTIFACT_NAME}
      echo ::set-env name=ARTIFACT_PATHNAME::${ARTIFACT_PATHNAME}
  - name: upload release asset ${{ env.GITHUB_TAG }}
    id: upload-release-asset
    uses: actions/upload-release-asset@v1.0.2
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    with:
      upload_url: ${{ steps.create_release.outputs.upload_url }}
      asset_path: ${{ env.ARTIFACT_PATHNAME }}
      asset_name: ${{ env.ARTIFACT_NAME }}
      asset_content_type: application/java-archive
gpiffaretti commented 4 years ago

I solved my problem with this workaround, I save the filename to the step output, and then retrieve it in a later stage. For this I had to add an ID to my step as shown below. I needed a regular expression because I didn't know the name of the file that npm pack will create, but I did know the filename prefix. With this solution I just extract the name from the output and save it. Used some more actions but didn't include in the code snippet for clarity: actions/checkout@v2 actions/setup-node@v1 actions/create-release@v1

- name: Pack tarball 
  id: pack_tarball
  run: |
    PACK_NAME=$(npm pack | tail -n 1)
    echo "::set-output name=tar_filename::$PACK_NAME"
- name: Upload Release Asset
  id: upload-release-asset 
  uses: actions/upload-release-asset@v1
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  with:
    upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps 
    asset_path: ./${{ steps.pack_tarball.outputs.tar_filename }}
    asset_name: ${{ steps.pack_tarball.outputs.tar_filename }}
    asset_content_type: application/gzip
kmturley commented 4 years ago
- name: Create Release
  uses: actions/github-script@v2
  with:
    github-token: ${{secrets.GITHUB_TOKEN}}
    script: |
      console.log('environment', process.versions);

      const fs = require('fs').promises;

      const { repo: { owner, repo }, sha } = context;
      console.log({ owner, repo, sha });

      const release = await github.repos.createRelease({
        owner, repo,
        tag_name: process.env.GITHUB_REF,
        draft: true,
        target_commitish: sha
      });

      console.log('created release', { release });

      for (let file of await fs.readdir('.')) {
        // do whatever filtering you want here, I'm just uploading all the files
        console.log('uploading', file);

        await github.repos.uploadReleaseAsset({
          owner, repo,
          release_id: release.data.id,
          name: file,
          data: await fs.readFile(`./${file}`)
        });            
      }

One bug I found with this solution is when referring to needs/steps within the async/await functions and loops, you get an error needs not defined or steps not defined and seems to lose variable scope.

This can be fixed by adding the values needs to variables within the script scope. For example:

- name: Upload
  uses: actions/github-script@v3
  with:
    github-token: ${{secrets.GITHUB_TOKEN}}
    script: |
      const path = require('path');
      const fs = require('fs');
      const release_id = '${{ needs.create_release.outputs.id }}';
      for (let file of await fs.readdirSync('./')) {
        if (path.extname(file) === '.zip') {
          console.log('uploadReleaseAsset', file);
          await github.repos.uploadReleaseAsset({
            owner: context.repo.owner,
            repo: context.repo.repo,
            release_id: release_id,
            name: file,
            data: await fs.readFileSync(`./${file}`)
          });
        }
      }