-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.js
120 lines (102 loc) · 4.25 KB
/
main.js
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
const fs = require("fs-extra");
const os = require("os");
const path = require("path");
const tmp = require("tmp");
const toml = require("toml");
const core = require("@actions/core");
const exec = require("@actions/exec");
const io = require("@actions/io");
const HOME = os.homedir();
const DEPOT_PATH = (process.env.JULIA_DEPOT_PATH || path.join(HOME, ".julia")).split(path.delimiter);
async function startSshAgent() {
const { stdout } = await exec.getExecOutput("ssh-agent");
stdout.split("\n").forEach(line => {
const match = /(.*)=(.*?);/.exec(line);
if (match) {
core.exportVariable(match[1], match[2]);
}
});
}
async function addSshKey(key) {
const { name } = tmp.fileSync();
fs.writeFileSync(name, key.trim() + "\n");
await exec.exec(`ssh-add ${name}`);
await io.rmRF(name);
}
async function updateKnownHosts() {
// Ensure that `known_hosts` always exists
const known_hosts_path = path.join(HOME, ".ssh", "known_hosts");
fs.ensureFileSync(known_hosts_path);
// If we don't already have a mapping for `github.com`, get it automatically
if ((await exec.exec("ssh-keygen", ["-F", "github.com"], { ignoreReturnCode: true })) != 0) {
const { stdout } = await exec.getExecOutput("ssh-keyscan github.com");
fs.appendFileSync(known_hosts_path, stdout);
}
}
function getRegistryName(registry_dir) {
const meta = toml.parse(fs.readFileSync(path.join(registry_dir, "Registry.toml")));
return meta.name || registry.split("/").pop();
}
async function cloneRegistry(url, name) {
// Use a consistent registry directory between CI jobs to ensure that this action works
// well with CI caching.
const repo_name = url.match(/([^\/]+)\.git$/)[1]
const registry_dir = path.join(DEPOT_PATH[0], "registries", name || repo_name);
if (!fs.existsSync(registry_dir)) {
await exec.exec(`git clone --no-progress ${url} ${registry_dir}`);
} else {
await exec.exec(`git -C ${registry_dir} pull`);
}
// We have observed that toml parsing can be quite slow. We use the passed in name
// to work around this problem.
// https://github.com/julia-actions/add-julia-registry/pull/25#issuecomment-1877708220
// Mainly, this exists for backwards compatibility.
if (!name) {
const registry_name = getRegistryName(registry_dir)
const alt_registry_dir = path.join(DEPOT_PATH[0], "registries", registry_name);
// Adding an alternate registry name via a symlink works well with Julia's
// `Pkg.Registry.update()` as that call will only one of the registries and not both.
if (registry_dir != alt_registry_dir && !fs.existsSync(alt_registry_dir)) {
fs.symlink(registry_dir, alt_registry_dir, "dir")
}
}
};
async function configureGitInsteadOfSsh() {
await exec.exec(`git config --global url."[email protected]:".insteadOf https://github.com/`);
}
async function configureGitInsteadOfHttps(github_token) {
await exec.exec(`git config --global url."https://git:${github_token}@github.com/".insteadOf https://github.com/`);
}
async function main() {
const registry = core.getInput("registry", { required: true });
const protocol = core.getInput("protocol", { required: true });
// While we support the deprecated `key` input we need to roll our own `required: protocol == "ssh"`
// const ssh_key = core.getInput("ssh-key", { required: protocol == "ssh" });
const ssh_key = core.getInput("ssh-key") || core.getInput("key");
if (protocol === "ssh" && !ssh_key) {
throw new Error("Input required and not supplied: ssh-key");
}
const github_token = core.getInput("github-token", { required: protocol == "https" });
if (protocol === "ssh") {
await startSshAgent();
await addSshKey(ssh_key);
await updateKnownHosts();
await configureGitInsteadOfSsh();
await cloneRegistry(`[email protected]:${registry}.git`);
if (registry != "JuliaRegistries/General") {
await cloneRegistry("[email protected]:JuliaRegistries/General.git", "General");
}
} else {
await configureGitInsteadOfHttps(github_token);
await cloneRegistry(`https://github.com/${registry}.git`);
if (registry != "JuliaRegistries/General") {
await cloneRegistry("https://github.com/JuliaRegistries/General.git", "General");
}
}
}
if (!module.parent) {
main().catch(e => {
console.error(e);
process.exit(1);
});
}