-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switched from outputs to environment output #3481
- directly writing to the file did not work - directly using set-output command did not work - using SECHUB_OUTPUT* environment values as alternative - changed documentation - wrote tests
- Loading branch information
Showing
4 changed files
with
95 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,18 @@ | ||
// SPDX-License-Identifier: MIT | ||
import * as outputHelper from '../src/output-helper'; | ||
import * as core from '@actions/core'; | ||
|
||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
jest.mock('@actions/core'); | ||
|
||
describe('storeOutput', () => { | ||
const outputPath = path.join(__dirname, 'test_output.txt'); | ||
const mockedCore = core as jest.Mocked<typeof core>; | ||
|
||
beforeAll(() => { | ||
process.env.GITHUB_OUTPUT = outputPath; | ||
}); | ||
|
||
afterEach(() => { | ||
if (fs.existsSync(outputPath)) { | ||
fs.unlinkSync(outputPath); | ||
} | ||
}); | ||
|
||
it('should append a line with key=value to the file', () => { | ||
it('test-key shall set SECHUB_OUTPUT_TEST_KEY', () => { | ||
/* execute */ | ||
outputHelper.storeOutput('TEST_KEY', 'TEST_VALUE'); | ||
outputHelper.storeOutput('test-key', 'test value1'); | ||
|
||
/* test */ | ||
const content = fs.readFileSync(outputPath, 'utf8'); | ||
expect(content).toBe('TEST_KEY=TEST_VALUE\n'); | ||
expect(mockedCore.exportVariable).toBeCalledWith('SECHUB_OUTPUT_TEST_KEY', 'test value1'); | ||
}); | ||
|
||
it('should append multiple lines correctly', () => { | ||
/* execute */ | ||
outputHelper.storeOutput('KEY1', 'VALUE1'); | ||
outputHelper.storeOutput('KEY2', 'VALUE2'); | ||
|
||
/* test */ | ||
const content = fs.readFileSync(outputPath, 'utf8'); | ||
expect(content).toBe('KEY1=VALUE1\nKEY2=VALUE2\n'); | ||
}); | ||
|
||
it('should throw an error if GITHUB_OUTPUT is not set', () => { | ||
/* prepare */ | ||
delete process.env.GITHUB_OUTPUT; | ||
|
||
/* execute + test */ | ||
expect(() => outputHelper.storeOutput('KEY', 'VALUE')).toThrow('GITHUB_OUTPUT environment variable is not set'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,51 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
import * as fs from 'fs'; | ||
import { setOutput } from '@actions/core/lib/core'; | ||
import { exportVariable } from '@actions/core/lib/core'; | ||
|
||
/** | ||
* Sets the value of an output variable for the GitHub Action. | ||
* This method is a replacement of usage of core.setOutput(..) method. | ||
* Sets the value of an output (environment ) variable for the GitHub Action. | ||
* This method is a workaround because of problems with of core.setOutput(..) method. | ||
* There were problems with core.setOutput(...), see | ||
* - https://github.com/mercedes-benz/sechub/issues/3481#issuecomment-2539015176 and | ||
* - https://github.com/actions/toolkit/issues/1218 | ||
* - https://github.com/actions/toolkit/issues/1906 | ||
* | ||
* As a workaround we provide instead of output | ||
* special SecHub ouput environment variables with naming convention "SECHUB_OUTPUT_${fieldAdopted}" | ||
* | ||
* `fieldAdopted` is same as `field`, but uppercased and `-` will be replaced by `_` | ||
* | ||
* For example: `scan-readable-summary` will become `SECHUB_OUTPUT_SCAN_READABLE_SUMMARY` | ||
* | ||
* If debugging is enabled in action the setting will be logged. | ||
*/ | ||
export function storeOutput(field: string, value: string) { | ||
const outputFilePath = process.env['GITHUB_OUTPUT'] || ''; | ||
|
||
if (!outputFilePath) { | ||
throw new Error('GITHUB_OUTPUT environment variable is not set'); | ||
// export the output to an "output" variable (this works) | ||
const envVarName = `SECHUB_OUTPUT_${field.toUpperCase().replace(/-/g, '_')}`; | ||
exportVariable(envVarName, value); | ||
if (process.env.ACTIONS_RUNNER_DEBUG === 'true') { | ||
// Print the environment variable for debugging | ||
console.log(`Exported environment variable ${envVarName} with value: ${value}`); | ||
} | ||
|
||
const outputLine = `${field}=${value}\n`; | ||
// 1. This following out commented code was thought as a workaround | ||
// for https://github.com/actions/toolkit/issues/1218 | ||
// Because the GITHUB_OUTPUT file from a worfklow step (which worked) did not contain | ||
// crypto.randomUUID() parts we tried to write the key/value file "normally" without | ||
// the crypto parts, but It did not appear inside context output, means it didn't work | ||
// (even when it the exact file structure as done by an echo ?!?!) | ||
// But we keep it here for documentation: | ||
|
||
// const outputFilePath = process.env['GITHUB_OUTPUT'] || ''; | ||
// if (!outputFilePath) { | ||
// throw new Error('GITHUB_OUTPUT environment variable is not set'); | ||
// } | ||
|
||
// const outputLine = `${field}=${value}\n`; | ||
// fs.appendFileSync(outputFilePath, outputLine, { encoding: 'utf8' }); | ||
|
||
|
||
// 2. Offical way by core API (does not work) | ||
// setOutput(field,value); | ||
|
||
fs.appendFileSync(outputFilePath, outputLine, { encoding: 'utf8' }); | ||
} |