-
Notifications
You must be signed in to change notification settings - Fork 71
/
utils.ts
125 lines (113 loc) · 5.13 KB
/
utils.ts
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
import * as core from '@actions/core';
import { exec } from '@actions/exec';
import * as toolCache from '@actions/tool-cache';
import * as fs from 'fs-extra';
import * as os from 'os';
import * as path from 'path';
import * as semver from 'semver';
export class Utils {
public static readonly USER_AGENT: string = 'setup-jfrog-cli-github-action/' + require('../package.json').version;
public static readonly SERVER_TOKEN_PREFIX: RegExp = /^JF_ARTIFACTORY_.*$/;
// Since 1.45.0, 'jfrog rt c' command changed to 'jfrog c add'
public static readonly NEW_CONFIG_CLI_VERSION: string = '1.45.0';
public static readonly CLI_VERSION_ARG: string = 'version';
public static readonly MIN_CLI_VERSION: string = '1.29.0';
public static async downloadCli(): Promise<string> {
let version: string = core.getInput(Utils.CLI_VERSION_ARG);
if (semver.lt(version, this.MIN_CLI_VERSION)) {
throw new Error('Requested to download JFrog CLI version ' + version + ' but must be at least ' + this.MIN_CLI_VERSION);
}
let fileName: string = Utils.getCliExecutableName();
let cliDir: string = toolCache.find(fileName, version);
if (cliDir) {
core.addPath(cliDir);
return path.join(cliDir, fileName);
}
let url: string = Utils.getCliUrl(version, fileName);
core.debug('Downloading JFrog CLI from ' + url);
let downloadDir: string = await toolCache.downloadTool(url);
cliDir = await toolCache.cacheFile(downloadDir, fileName, fileName, version);
let cliPath: string = path.join(cliDir, fileName);
if (!Utils.isWindows()) {
fs.chmodSync(cliPath, 0o555);
}
core.addPath(cliDir);
return cliPath;
}
public static getCliUrl(version: string, fileName: string): string {
let architecture: string = 'jfrog-cli-' + Utils.getArchitecture();
return 'https://releases.jfrog.io/artifactory/jfrog-cli/v1/' + version + '/' + architecture + '/' + fileName;
}
public static getServerTokens(): string[] {
return Object.keys(process.env)
.filter((env) => env.match(Utils.SERVER_TOKEN_PREFIX))
.map((envKey) => process.env[envKey] || '');
}
public static setCliEnv() {
core.exportVariable('JFROG_CLI_ENV_EXCLUDE', '*password*;*secret*;*key*;*token*;JF_ARTIFACTORY_*');
core.exportVariable('JFROG_CLI_OFFER_CONFIG', 'false');
let buildNameEnv: string | undefined = process.env.GITHUB_WORKFLOW;
if (buildNameEnv) {
core.exportVariable('JFROG_CLI_BUILD_NAME', buildNameEnv);
}
let buildNumberEnv: string | undefined = process.env.GITHUB_RUN_NUMBER;
if (buildNumberEnv) {
core.exportVariable('JFROG_CLI_BUILD_NUMBER', buildNumberEnv);
}
core.exportVariable(
'JFROG_CLI_BUILD_URL',
process.env.GITHUB_SERVER_URL + '/' + process.env.GITHUB_REPOSITORY + '/actions/runs/' + process.env.GITHUB_RUN_ID
);
core.exportVariable('JFROG_CLI_USER_AGENT', Utils.USER_AGENT);
}
public static async configArtifactoryServers(cliPath: string) {
let useOldConfig: boolean = Utils.useOldConfig();
if (useOldConfig) {
let version: string = core.getInput(Utils.CLI_VERSION_ARG);
core.warning('JFrog CLI ' + version + ' on Setup JFrog CLI GitHub Action is deprecated. Please use version 1.46.4 or above.');
}
for (let serverToken of Utils.getServerTokens()) {
let importCmd: string[] = useOldConfig ? ['rt', 'c', 'import', serverToken] : ['c', 'import', serverToken];
await Utils.runCli(cliPath, importCmd);
}
}
public static async removeArtifactoryServers(cliPath: string) {
if (Utils.useOldConfig()) {
await Utils.runCli(cliPath, ['rt', 'c', 'clear', '--interactive=false']);
} else {
await Utils.runCli(cliPath, ['c', 'rm', '--quiet']);
}
}
public static getArchitecture() {
if (Utils.isWindows()) {
return 'windows-amd64';
}
if (os.platform().includes('darwin')) {
return 'mac-386';
}
if (os.arch().includes('arm')) {
return os.arch().includes('64') ? 'linux-arm64' : 'linux-arm';
}
return os.arch().includes('64') ? 'linux-amd64' : 'linux-386';
}
public static getCliExecutableName() {
return Utils.isWindows() ? 'jfrog.exe' : 'jfrog';
}
public static isWindows() {
return os.platform().startsWith('win');
}
public static async runCli(cliPath: string, args: string[] | undefined) {
let res: number = await exec(cliPath, args);
if (res !== core.ExitCode.Success) {
throw new Error('JFrog CLI exited with exit code ' + res);
}
}
/**
* Return true if should use 'jfrog rt c' instead of 'jfrog c'.
* @returns true if should use 'jfrog rt c' instead of 'jfrog c'.
*/
private static useOldConfig(): boolean {
let version: string = core.getInput(Utils.CLI_VERSION_ARG);
return semver.lt(version, this.NEW_CONFIG_CLI_VERSION);
}
}