about summary refs log tree commit diff
path: root/compiler/rustc_codegen_cranelift/.github/actions/github-release/main.js
blob: 1eb2b7f23b26c57154761688b8cb0a509b873109 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

const core = require('@actions/core');
const path = require("path");
const fs = require("fs");
const github = require('@actions/github');
const glob = require('glob');

function sleep(milliseconds) {
  return new Promise(resolve => setTimeout(resolve, milliseconds))
}

async function runOnce() {
  // Load all our inputs and env vars. Note that `getInput` reads from `INPUT_*`
  const files = core.getInput('files');
  const token = core.getInput('token');
  const slug = process.env.GITHUB_REPOSITORY;
  const owner = slug.split('/')[0];
  const repo = slug.split('/')[1];
  const sha = process.env.GITHUB_SHA;
  let name = 'dev';
  if (process.env.GITHUB_REF.startsWith('refs/tags/v')) {
    name = process.env.GITHUB_REF.substring(10);
  }

  core.info(`files: ${files}`);
  core.info(`name: ${name}`);
  core.info(`token: ${token}`);

  const octokit = github.getOctokit(token);

  // For the `dev` release we may need to update the tag to point to the new
  // commit on this branch. All other names should already have tags associated
  // with them.
  if (name == 'dev') {
    let tag = null;
    try {
      tag = await octokit.request("GET /repos/:owner/:repo/git/refs/tags/:name", { owner, repo, name });
      core.info(`found existing tag`);
      console.log("tag: ", JSON.stringify(tag.data, null, 2));
    } catch (e) {
      // ignore if this tag doesn't exist
      core.info(`no existing tag found`);
    }

    if (tag === null || tag.data.object.sha !== sha) {
      core.info(`updating existing tag or creating new one`);

      try {
        core.info(`updating dev tag`);
        await octokit.rest.git.updateRef({
          owner,
          repo,
          ref: 'tags/dev',
          sha,
          force: true,
        });
      } catch (e) {
        console.log("ERROR: ", JSON.stringify(e.response, null, 2));
        core.info(`creating dev tag`);
        try {
          await octokit.rest.git.createRef({
            owner,
            repo,
            ref: 'refs/tags/dev',
            sha,
          });
        } catch (e) {
          // we might race with others, so assume someone else has created the
          // tag by this point.
          console.log("failed to create tag: ", JSON.stringify(e.response, null, 2));
        }
      }

      console.log("double-checking tag is correct");
      tag = await octokit.request("GET /repos/:owner/:repo/git/refs/tags/:name", { owner, repo, name });
      if (tag.data.object.sha !== sha) {
        console.log("tag: ", JSON.stringify(tag.data, null, 2));
        throw new Error("tag didn't work");
      }
    } else {
      core.info(`existing tag works`);
    }
  }

  // Delete a previous release
  try {
    core.info(`fetching release`);
    let release = await octokit.rest.repos.getReleaseByTag({ owner, repo, tag: name });
    console.log("found release: ", JSON.stringify(release.data, null, 2));
    await octokit.rest.repos.deleteRelease({
      owner,
      repo,
      release_id: release.data.id,
    });
    console.log("deleted release");
  } catch (e) {
    console.log("ERROR: ", JSON.stringify(e, null, 2));
  }

  console.log("creating a release");
  let release = await octokit.rest.repos.createRelease({
    owner,
    repo,
    tag_name: name,
    prerelease: name === 'dev',
  });

  // Delete all assets from a previous run
  for (const asset of release.data.assets) {
    console.log(`deleting prior asset ${asset.id}`);
    await octokit.rest.repos.deleteReleaseAsset({
      owner,
      repo,
      asset_id: asset.id,
    });
  }

  // Upload all the relevant assets for this release as just general blobs.
  for (const file of glob.sync(files)) {
    const size = fs.statSync(file).size;
    const name = path.basename(file);
    core.info(`upload ${file}`);
    await octokit.rest.repos.uploadReleaseAsset({
      data: fs.createReadStream(file),
      headers: { 'content-length': size, 'content-type': 'application/octet-stream' },
      name,
      url: release.data.upload_url,
    });
  }
}

async function run() {
  const retries = 10;
  for (let i = 0; i < retries; i++) {
    try {
      await runOnce();
      break;
    } catch (e) {
      if (i === retries - 1)
        throw e;
      logError(e);
      console.log("RETRYING after 10s");
      await sleep(10000)
    }
  }
}

function logError(e) {
  console.log("ERROR: ", e.message);
  try {
    console.log(JSON.stringify(e, null, 2));
  } catch (e) {
    // ignore json errors for now
  }
  console.log(e.stack);
}

run().catch(err => {
  logError(err);
  core.setFailed(err.message);
});