-
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.
Temporary try out via environments instead # 3481
- directly writing to the file did not work - directly using set-output command did not work - trying environment setting method as alternative
- Loading branch information
Showing
2 changed files
with
72 additions
and
22 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,23 +1,49 @@ | ||
// 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. | ||
* This method is a wrapper of usage 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 | ||
* | ||
* As a workaround, we set the output as before via core API but | ||
* provide also an environment variable with "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 adoptedFieldName = field.replace(/-/g, '_'); | ||
const envVarName = `SECHUB_OUTPUT_${adoptedFieldName.toUpperCase()}`; | ||
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`; | ||
// 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 wored) 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 didn't work. | ||
// 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' }); | ||
|
||
// Do the official way (but which does not work currently. Maybe it will befixed inside toolkit) | ||
setOutput(adoptedFieldName,value); | ||
|
||
fs.appendFileSync(outputFilePath, outputLine, { encoding: 'utf8' }); | ||
} |