|
| 1 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | + |
| 3 | +const core = require('@actions/core'); |
| 4 | +const path = require("path"); |
| 5 | +const fs = require("fs"); |
| 6 | +const github = require('@actions/github'); |
| 7 | +const glob = require('glob'); |
| 8 | + |
| 9 | +function sleep(milliseconds) { |
| 10 | + return new Promise(resolve => setTimeout(resolve, milliseconds)) |
| 11 | +} |
| 12 | + |
| 13 | +async function runOnce() { |
| 14 | + // Load all our inputs and env vars. Note that `getInput` reads from `INPUT_*` |
| 15 | + const files = core.getInput('files'); |
| 16 | + const token = core.getInput('token'); |
| 17 | + const slug = process.env.GITHUB_REPOSITORY; |
| 18 | + const owner = slug.split('/')[0]; |
| 19 | + const repo = slug.split('/')[1]; |
| 20 | + const sha = process.env.GITHUB_SHA; |
| 21 | + let name = 'dev'; |
| 22 | + if (process.env.GITHUB_REF.startsWith('refs/tags/v')) { |
| 23 | + name = process.env.GITHUB_REF.substring(10); |
| 24 | + } |
| 25 | + |
| 26 | + core.info(`files: ${files}`); |
| 27 | + core.info(`name: ${name}`); |
| 28 | + core.info(`token: ${token}`); |
| 29 | + |
| 30 | + const octokit = github.getOctokit(token); |
| 31 | + |
| 32 | + // For the `dev` release we may need to update the tag to point to the new |
| 33 | + // commit on this branch. All other names should already have tags associated |
| 34 | + // with them. |
| 35 | + if (name == 'dev') { |
| 36 | + let tag = null; |
| 37 | + try { |
| 38 | + tag = await octokit.request("GET /repos/:owner/:repo/git/refs/tags/:name", { owner, repo, name }); |
| 39 | + core.info(`found existing tag`); |
| 40 | + console.log("tag: ", JSON.stringify(tag.data, null, 2)); |
| 41 | + } catch (e) { |
| 42 | + // ignore if this tag doesn't exist |
| 43 | + core.info(`no existing tag found`); |
| 44 | + } |
| 45 | + |
| 46 | + if (tag === null || tag.data.object.sha !== sha) { |
| 47 | + core.info(`updating existing tag or creating new one`); |
| 48 | + |
| 49 | + try { |
| 50 | + core.info(`updating dev tag`); |
| 51 | + await octokit.rest.git.updateRef({ |
| 52 | + owner, |
| 53 | + repo, |
| 54 | + ref: 'tags/dev', |
| 55 | + sha, |
| 56 | + force: true, |
| 57 | + }); |
| 58 | + } catch (e) { |
| 59 | + console.log("ERROR: ", JSON.stringify(e.data, null, 2)); |
| 60 | + core.info(`creating dev tag`); |
| 61 | + try { |
| 62 | + await octokit.rest.git.createRef({ |
| 63 | + owner, |
| 64 | + repo, |
| 65 | + ref: 'refs/tags/dev', |
| 66 | + sha, |
| 67 | + }); |
| 68 | + } catch (e) { |
| 69 | + // we might race with others, so assume someone else has created the |
| 70 | + // tag by this point. |
| 71 | + console.log("failed to create tag: ", JSON.stringify(e.data, null, 2)); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + console.log("double-checking tag is correct"); |
| 76 | + tag = await octokit.request("GET /repos/:owner/:repo/git/refs/tags/:name", { owner, repo, name }); |
| 77 | + if (tag.data.object.sha !== sha) { |
| 78 | + console.log("tag: ", JSON.stringify(tag.data, null, 2)); |
| 79 | + throw new Error("tag didn't work"); |
| 80 | + } |
| 81 | + } else { |
| 82 | + core.info(`existing tag works`); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Delete a previous release |
| 87 | + try { |
| 88 | + core.info(`fetching release`); |
| 89 | + let release = await octokit.rest.repos.getReleaseByTag({ owner, repo, tag: name }); |
| 90 | + console.log("found release: ", JSON.stringify(release.data, null, 2)); |
| 91 | + await octokit.rest.repos.deleteRelease({ |
| 92 | + owner, |
| 93 | + repo, |
| 94 | + release_id: release.data.id, |
| 95 | + }); |
| 96 | + console.log("deleted release"); |
| 97 | + } catch (e) { |
| 98 | + console.log("ERROR: ", JSON.stringify(e, null, 2)); |
| 99 | + } |
| 100 | + |
| 101 | + console.log("creating a release"); |
| 102 | + let release = await octokit.rest.repos.createRelease({ |
| 103 | + owner, |
| 104 | + repo, |
| 105 | + tag_name: name, |
| 106 | + prerelease: name === 'dev', |
| 107 | + }); |
| 108 | + |
| 109 | + // Delete all assets from a previous run |
| 110 | + for (const asset of release.data.assets) { |
| 111 | + console.log(`deleting prior asset ${asset.id}`); |
| 112 | + await octokit.rest.repos.deleteReleaseAsset({ |
| 113 | + owner, |
| 114 | + repo, |
| 115 | + asset_id: asset.id, |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + // Upload all the relevant assets for this release as just general blobs. |
| 120 | + for (const file of glob.sync(files)) { |
| 121 | + const size = fs.statSync(file).size; |
| 122 | + const name = path.basename(file); |
| 123 | + core.info(`upload ${file}`); |
| 124 | + await octokit.rest.repos.uploadReleaseAsset({ |
| 125 | + data: fs.createReadStream(file), |
| 126 | + headers: { 'content-length': size, 'content-type': 'application/octet-stream' }, |
| 127 | + name, |
| 128 | + url: release.data.upload_url, |
| 129 | + }); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +async function run() { |
| 134 | + const retries = 10; |
| 135 | + for (let i = 0; i < retries; i++) { |
| 136 | + try { |
| 137 | + await runOnce(); |
| 138 | + break; |
| 139 | + } catch (e) { |
| 140 | + if (i === retries - 1) |
| 141 | + throw e; |
| 142 | + logError(e); |
| 143 | + console.log("RETRYING after 10s"); |
| 144 | + await sleep(10000) |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +function logError(e) { |
| 150 | + console.log("ERROR: ", e.message); |
| 151 | + try { |
| 152 | + console.log(JSON.stringify(e, null, 2)); |
| 153 | + } catch (e) { |
| 154 | + // ignore json errors for now |
| 155 | + } |
| 156 | + console.log(e.stack); |
| 157 | +} |
| 158 | + |
| 159 | +run().catch(err => { |
| 160 | + logError(err); |
| 161 | + core.setFailed(err.message); |
| 162 | +}); |
0 commit comments