Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,31 @@ class Utils {
*/
static getServerIdForConfig() {
let serverId = Utils.getCustomOrDefaultServerId();
// Add new serverId to the servers env var if it doesn't already exist.
if (Utils.getConfiguredJFrogServers().includes(serverId)) {
return serverId;
Utils.addServerIdForCleanup(serverId);
return serverId;
}
/**
* Add new serverId to the servers env var if it doesn't already exist.
*/
static addServerIdForCleanup(serverId) {
if (!serverId || Utils.getConfiguredJFrogServers().includes(serverId)) {
return;
}
const currentValue = process.env[Utils.JFROG_CLI_SERVER_IDS_ENV_VAR];
const newVal = currentValue ? `${currentValue};${serverId}` : serverId;
core.exportVariable(Utils.JFROG_CLI_SERVER_IDS_ENV_VAR, newVal);
return serverId;
}
static getServerIdFromConfigToken(configToken) {
try {
const serverObj = JSON.parse(Buffer.from(configToken, 'base64').toString());
if (serverObj && typeof serverObj.serverId === 'string') {
return serverObj.serverId.trim();
}
}
catch (error) {
core.debug(`Could not extract server ID from JF_ENV config token: ${error}`);
}
return;
}
/**
* Returns the custom server ID if provided, otherwise returns the default server ID.
Expand Down Expand Up @@ -304,6 +321,10 @@ class Utils {
// Mark the credentials as secrets to prevent them from being printed in the logs or exported to other workflows
core.setSecret(configToken);
yield Utils.runCli(cliConfigCmd.concat('import', configToken));
const serverId = Utils.getServerIdFromConfigToken(configToken);
if (serverId) {
Utils.addServerIdForCleanup(serverId);
}
}
let configArgs = yield Utils.getJfrogCliConfigArgs(jfrogCredentials);
if (configArgs) {
Expand Down
32 changes: 27 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class Utils {
}

public static getGheBaseUrl(): string {
const v =
const v: string =
core.getInput(Utils.GHE_BASE_URL_INPUT, { required: false }) || core.getInput(Utils.GHE_BASE_URL_ALIAS_INPUT, { required: false }) || '';
return v.trim();
}
Expand Down Expand Up @@ -248,14 +248,32 @@ export class Utils {
private static getServerIdForConfig(): string {
let serverId: string = Utils.getCustomOrDefaultServerId();

// Add new serverId to the servers env var if it doesn't already exist.
if (Utils.getConfiguredJFrogServers().includes(serverId)) {
return serverId;
Utils.addServerIdForCleanup(serverId);
return serverId;
}

/**
* Add new serverId to the servers env var if it doesn't already exist.
*/
private static addServerIdForCleanup(serverId: string) {
if (!serverId || Utils.getConfiguredJFrogServers().includes(serverId)) {
return;
}
const currentValue: string | undefined = process.env[Utils.JFROG_CLI_SERVER_IDS_ENV_VAR];
const newVal: string = currentValue ? `${currentValue};${serverId}` : serverId;
core.exportVariable(Utils.JFROG_CLI_SERVER_IDS_ENV_VAR, newVal);
return serverId;
}

private static getServerIdFromConfigToken(configToken: string): string | undefined {
try {
const serverObj: any = JSON.parse(Buffer.from(configToken, 'base64').toString());
if (serverObj && typeof serverObj.serverId === 'string') {
return serverObj.serverId.trim();
}
} catch (error) {
core.debug(`Could not extract server ID from JF_ENV config token: ${error}`);
}
return;
}

/**
Expand Down Expand Up @@ -349,6 +367,10 @@ export class Utils {
// Mark the credentials as secrets to prevent them from being printed in the logs or exported to other workflows
core.setSecret(configToken);
await Utils.runCli(cliConfigCmd.concat('import', configToken));
const serverId: string | undefined = Utils.getServerIdFromConfigToken(configToken);
if (serverId) {
Utils.addServerIdForCleanup(serverId);
}
}

let configArgs: string[] | undefined = await Utils.getJfrogCliConfigArgs(jfrogCredentials);
Expand Down
15 changes: 15 additions & 0 deletions test/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ beforeEach(() => {
['JF_ENV_1', 'JF_ENV_2', 'ENV_JF_1', 'JF_ENV_LOCAL', 'JF_USER', 'JF_PASSWORD', 'JF_ACCESS_TOKEN'].forEach((envKey) => {
delete process.env[envKey];
});
delete process.env[Utils.JFROG_CLI_SERVER_IDS_ENV_VAR];
});

test('Get Config Tokens', async () => {
Expand Down Expand Up @@ -204,6 +205,20 @@ describe('JFrog CLI Configuration', () => {
expect(servers).toStrictEqual([Utils.getRunDefaultServerId(), customServerId]);
});

test('Config token imports are registered for post-job cleanup', async () => {
myCore.exportVariable = jest.fn().mockImplementation((name: string, val: string) => {
process.env[name] = val;
});
const runCliMock = jest.spyOn(Utils, 'runCli').mockResolvedValue(undefined);
process.env.JF_ENV_LOCAL = V2_CONFIG;

await Utils.configJFrogServers({} as JfrogCredentials);

expect(runCliMock).toHaveBeenCalledWith(['config', 'import', V2_CONFIG]);
expect(core.exportVariable).toHaveBeenCalledWith(Utils.JFROG_CLI_SERVER_IDS_ENV_VAR, 'local');
expect(Utils.getConfiguredJFrogServers()).toStrictEqual(['local']);
});

test('Get default server ID', async () => {
expect(Utils.getRunDefaultServerId()).toStrictEqual('setup-jfrog-cli-server');
});
Expand Down
Loading