Skip to content

Commit

Permalink
Moved the release asset upload script to its own file.
Browse files Browse the repository at this point in the history
  • Loading branch information
PathogenDavid committed Sep 1, 2021
1 parent 10451a5 commit ed49f73
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
34 changes: 8 additions & 26 deletions .github/workflows/release-event-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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);
29 changes: 29 additions & 0 deletions .github/workflows/upload-release-assets.js
Original file line number Diff line number Diff line change
@@ -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
});
}
};

0 comments on commit ed49f73

Please sign in to comment.