diff --git a/lib/utils.js b/lib/utils.js index 1f9b70440..ad7f248da 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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. @@ -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) { diff --git a/src/utils.ts b/src/utils.ts index 6d9726409..00ed4c3a9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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(); } @@ -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; } /** @@ -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); diff --git a/test/main.spec.ts b/test/main.spec.ts index 86880b3a2..ec5d40f45 100644 --- a/test/main.spec.ts +++ b/test/main.spec.ts @@ -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 () => { @@ -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'); });