Skip to content

Commit

Permalink
feat: allow for passing a tag reference
Browse files Browse the repository at this point in the history
Signed-off-by: Marco Ceppi <marco@ceppi.net>
  • Loading branch information
marcoceppi committed Apr 1, 2023
1 parent d7ddff6 commit cc62137
Show file tree
Hide file tree
Showing 4 changed files with 687 additions and 439 deletions.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ inputs:
asset_paths:
description: 'The paths to the assets you want to upload as a JSON array. You can use a glob pattern.'
required: true
tag:
description: 'The tag in the release to attach assets to.'
required: false
outputs:
browser_download_urls:
description: 'The URL users can navigate to in order to download the uploaded asset'
Expand Down
1,081 changes: 664 additions & 417 deletions dist/index.js

Large diffs are not rendered by default.

11 changes: 4 additions & 7 deletions src/get-release.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
class GetRelease {
constructor(octokit, context) {
constructor(octokit, context, tag) {
this.octokit = octokit
this.context = context;
this.tag = tag || context.ref;
}

async getURL() {
// Get owner and repo from context of payload that triggered the action
const { owner, repo } = this.context.repo;
// Get the tag name from the triggered action
const tagName = this.context.ref;

// This removes the 'refs/tags' portion of the string, i.e. from 'refs/tags/v1.10.15' to 'v1.10.15'
const tag = tagName.replace("refs/tags/", "");
const tag = this.tag.replace("refs/tags/", "");

// Get a release from the tag name
// API Documentation: https://developer.github.com/v3/repos/releases/#create-a-release
Expand Down Expand Up @@ -46,4 +43,4 @@ module.exports = GetRelease
// console.log(uploadUrl)
// }

// runMe()
// runMe()
31 changes: 16 additions & 15 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,48 @@ async function run() {
try {
// Get authenticated GitHub client (Ocktokit): https://github.com/actions/toolkit/tree/master/packages/github#usage
const octokit = github.getOctokit(process.env.GITHUB_TOKEN);
const getRelease = new GetRelease(octokit, github.context)

const uploadUrl = await getRelease.getURL()

// Get the inputs from the workflow file: https://github.com/actions/toolkit/tree/master/packages/core#inputsoutputs
const assetPathsSt = core.getInput('asset_paths', { required: true });
const tagInput = core.getInput('tag');

const getRelease = new GetRelease(octokit, github.context, tagInput)
const uploadUrl = await getRelease.getURL()

const assetPaths = JSON.parse(assetPathsSt)
if(!assetPaths || assetPaths.length == 0) {
if (!assetPaths || assetPaths.length == 0) {
core.setFailed("asset_paths must contain a JSON array of quoted paths");
return
}

let paths = []
for(let i = 0; i < assetPaths.length; i++) {
for (let i = 0; i < assetPaths.length; i++) {
let assetPath = assetPaths[i];
if(assetPath.indexOf("*") > -1) {
const files = glob.sync(assetPath,{ nodir: true })
if (assetPath.indexOf("*") > -1) {
const files = glob.sync(assetPath, { nodir: true })
for (const file of files) {
paths.push(file)
paths.push(file)
}
}else {
} else {
paths.push(assetPath)
}
}

core.debug(`Expanded paths: ${paths}`)

downloadURLs = []
for(let i = 0; i < paths.length; i++) {
for (let i = 0; i < paths.length; i++) {
let asset = paths[i];

// Determine content-length for header to upload asset
const contentLength = filePath => fs.statSync(filePath).size;
const contentType = "binary/octet-stream"
// Setup headers for API call, see Octokit Documentation: https://octokit.github.io/rest.js/#octokit-routes-repos-upload-release-asset for more information
const headers = {
'content-type': contentType,
const headers = {
'content-type': contentType,
'content-length': contentLength(asset)
};

const assetName = path.basename(asset)
console.log(`Uploading ${assetName}`)

Expand All @@ -64,12 +65,12 @@ async function run() {
name: assetName,
data: fs.readFileSync(asset)
});

// Get the browser_download_url for the uploaded release asset from the response
const {
data: { browser_download_url: browserDownloadUrl }
} = uploadAssetResponse;

// Set the output variable for use by other actions: https://github.com/actions/toolkit/tree/master/packages/core#inputsoutputs
downloadURLs.push(browserDownloadUrl)
}
Expand Down

0 comments on commit cc62137

Please sign in to comment.