From ed49f73691b1627341af1a8550135d12da748af8 Mon Sep 17 00:00:00 2001 From: David Maas Date: Wed, 1 Sep 2021 01:06:42 -0500 Subject: [PATCH] Moved the release asset upload script to its own file. --- .github/workflows/release-event-test.yml | 34 +++++----------------- .github/workflows/upload-release-assets.js | 29 ++++++++++++++++++ 2 files changed, 37 insertions(+), 26 deletions(-) create mode 100644 .github/workflows/upload-release-assets.js diff --git a/.github/workflows/release-event-test.yml b/.github/workflows/release-event-test.yml index b31539f..e9b9556 100644 --- a/.github/workflows/release-event-test.yml +++ b/.github/workflows/release-event-test.yml @@ -6,12 +6,14 @@ on: # -- Oh! # > Note: The release event is not triggered for draft releases. # Emphasis on ****release event****, I read this as meaning to be the "released" type. + # `created` will fire if you create a release and publish immediately without making it a draft first. ##################### # Edited works! # Not sure how to trigger unpublished... # Deleted works - # Not sure what prereleased/released are either (I would think they describe what I'd call "publish", but that's obviously a separate event) - types: [published, unpublished, created, edited, deleted, prereleased, released] + # `released` (and probably `prereleased`) seem to only happen if you publish immediately without making a draft first. + #types: [published, unpublished, created, edited, deleted, prereleased, released] + types: [published] jobs: do-release: runs-on: ubuntu-latest @@ -29,6 +31,8 @@ jobs: if: github.event.action == 'published' runs-on: ubuntu-latest steps: + - name: Checkout + uses: actions/checkout@v2 - name: Test getting the release URL in a GitHub script uses: actions/github-script@v3 with: @@ -48,30 +52,8 @@ jobs: asset_name: test-uploadaction.txt asset_content_type: text/plain - name: Upload test files - uses: actions/github-script@v3 + uses: actions/github-script@v4 with: #github-token: ${{secrets.GITHUB_TOKEN}} user-agent: actions/github-script for ${{github.repository}} - script: | - const fs = require('fs').promises; - const path = require('path'); - - for (let filePath of await fs.readdir('.')) { - const fileExtension = path.extname(filePath); - if (fileExtension != '.txt') { - continue; - } - - console.log(`Uploading '${filePath}'`); - const contentLength = (await fs.stat(filePath)).size; - const fileContents = await fs.readFile(filePath); - await github.repos.uploadReleaseAsset({ - url: '${{github.event.release.upload_url}}', - headers: { - 'content-type': 'application/octet-stream', - 'content-length': contentLength - }, - name: path.basename(filePath), - data: fileContents - }); - } + script: await require('./.github/workflows/upload-release-assets.js')(github, context); diff --git a/.github/workflows/upload-release-assets.js b/.github/workflows/upload-release-assets.js new file mode 100644 index 0000000..5b82fef --- /dev/null +++ b/.github/workflows/upload-release-assets.js @@ -0,0 +1,29 @@ +module.exports = async (github, context) => { + const fs = require('fs').promises; + const path = require('path'); + const upload_url = context.payload.release.upload_url; + + if (!upload_url) { + throw "Missing release asset upload URL!"; + } + + for (let filePath of await fs.readdir('.')) { + const fileExtension = path.extname(filePath); + if (fileExtension != '.txt') { + continue; + } + + console.log(`Uploading '${filePath}'`); + const contentLength = (await fs.stat(filePath)).size; + const fileContents = await fs.readFile(filePath); + await github.repos.uploadReleaseAsset({ + url: context.payload.release.upload_url, + headers: { + 'content-type': 'application/octet-stream', + 'content-length': contentLength + }, + name: path.basename(filePath), + data: fileContents + }); + } +};