2019-05-21 15:26:44 -04:00
|
|
|
import {issue, issueCommand} from './command'
|
2019-05-16 23:36:45 -04:00
|
|
|
|
2019-09-09 11:46:48 -04:00
|
|
|
import * as os from 'os'
|
2019-06-04 22:00:25 -04:00
|
|
|
import * as path from 'path'
|
|
|
|
|
|
2019-05-21 12:00:23 -04:00
|
|
|
/**
|
|
|
|
|
* Interface for getInput options
|
|
|
|
|
*/
|
|
|
|
|
export interface InputOptions {
|
|
|
|
|
/** Optional. Whether the input is required. If required and not present, will throw. Defaults to false */
|
|
|
|
|
required?: boolean
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-25 13:54:41 -04:00
|
|
|
/**
|
|
|
|
|
* The code to exit an action
|
|
|
|
|
*/
|
|
|
|
|
export enum ExitCode {
|
|
|
|
|
/**
|
|
|
|
|
* A code indicating that the action was successful
|
|
|
|
|
*/
|
|
|
|
|
Success = 0,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A code indicating that the action was a failure
|
|
|
|
|
*/
|
2019-08-06 09:12:30 -04:00
|
|
|
Failure = 1
|
2019-06-25 13:54:41 -04:00
|
|
|
}
|
|
|
|
|
|
2019-05-17 10:23:01 -04:00
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
|
// Variables
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
|
|
2019-05-16 23:36:45 -04:00
|
|
|
/**
|
2019-10-01 17:13:05 -04:00
|
|
|
* Sets env variable for this action and future actions in the job
|
2019-05-16 23:36:45 -04:00
|
|
|
* @param name the name of the variable to set
|
|
|
|
|
* @param val the value of the variable
|
|
|
|
|
*/
|
2019-05-21 17:08:25 -04:00
|
|
|
export function exportVariable(name: string, val: string): void {
|
2019-05-20 19:17:56 -04:00
|
|
|
process.env[name] = val
|
2019-05-21 15:26:44 -04:00
|
|
|
issueCommand('set-env', {name}, val)
|
2019-05-16 23:36:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-10-01 17:13:05 -04:00
|
|
|
* Registers a secret which will get masked from logs
|
|
|
|
|
* @param secret value of the secret
|
2019-05-16 23:36:45 -04:00
|
|
|
*/
|
2019-10-01 17:13:05 -04:00
|
|
|
export function setSecret(secret: string): void {
|
|
|
|
|
issueCommand('add-mask', {}, secret)
|
2019-05-20 19:17:56 -04:00
|
|
|
}
|
2019-05-16 23:36:45 -04:00
|
|
|
|
2019-06-04 22:00:25 -04:00
|
|
|
/**
|
|
|
|
|
* Prepends inputPath to the PATH (for this action and future actions)
|
|
|
|
|
* @param inputPath
|
|
|
|
|
*/
|
|
|
|
|
export function addPath(inputPath: string): void {
|
|
|
|
|
issueCommand('add-path', {}, inputPath)
|
|
|
|
|
process.env['PATH'] = `${inputPath}${path.delimiter}${process.env['PATH']}`
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 10:23:01 -04:00
|
|
|
/**
|
|
|
|
|
* Gets the value of an input. The value is also trimmed.
|
2019-05-20 19:17:56 -04:00
|
|
|
*
|
2019-05-17 10:23:01 -04:00
|
|
|
* @param name name of the input to get
|
|
|
|
|
* @param options optional. See InputOptions.
|
|
|
|
|
* @returns string
|
|
|
|
|
*/
|
2019-05-21 12:03:13 -04:00
|
|
|
export function getInput(name: string, options?: InputOptions): string {
|
2019-05-21 10:51:22 -04:00
|
|
|
const val: string =
|
2019-09-12 19:41:11 +02:00
|
|
|
process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] || ''
|
2019-05-20 19:17:56 -04:00
|
|
|
if (options && options.required && !val) {
|
|
|
|
|
throw new Error(`Input required and not supplied: ${name}`)
|
|
|
|
|
}
|
2019-05-17 10:23:01 -04:00
|
|
|
|
2019-05-20 19:17:56 -04:00
|
|
|
return val.trim()
|
2019-05-17 10:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
2019-06-24 16:14:47 -04:00
|
|
|
/**
|
|
|
|
|
* Sets the value of an output.
|
|
|
|
|
*
|
|
|
|
|
* @param name name of the output to set
|
|
|
|
|
* @param value value to store
|
|
|
|
|
*/
|
|
|
|
|
export function setOutput(name: string, value: string): void {
|
2019-06-25 07:19:19 -04:00
|
|
|
issueCommand('set-output', {name}, value)
|
2019-06-24 16:14:47 -04:00
|
|
|
}
|
|
|
|
|
|
2019-05-16 23:36:45 -04:00
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
|
// Results
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
2019-05-20 19:17:56 -04:00
|
|
|
* Sets the action status to failed.
|
2019-05-16 23:36:45 -04:00
|
|
|
* When the action exits it will be with an exit code of 1
|
|
|
|
|
* @param message add error issue message
|
|
|
|
|
*/
|
2019-05-21 17:08:25 -04:00
|
|
|
export function setFailed(message: string): void {
|
2019-05-21 12:00:23 -04:00
|
|
|
process.exitCode = ExitCode.Failure
|
2019-05-20 19:17:56 -04:00
|
|
|
error(message)
|
2019-05-16 23:36:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
|
// Logging Commands
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Writes debug message to user log
|
|
|
|
|
* @param message debug message
|
|
|
|
|
*/
|
2019-05-21 17:08:25 -04:00
|
|
|
export function debug(message: string): void {
|
2019-05-21 15:26:44 -04:00
|
|
|
issueCommand('debug', {}, message)
|
2019-05-16 23:36:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds an error issue
|
|
|
|
|
* @param message error issue message
|
|
|
|
|
*/
|
2019-05-21 17:08:25 -04:00
|
|
|
export function error(message: string): void {
|
2019-05-21 15:26:44 -04:00
|
|
|
issue('error', message)
|
2019-05-16 23:36:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds an warning issue
|
|
|
|
|
* @param message warning issue message
|
|
|
|
|
*/
|
2019-05-21 17:08:25 -04:00
|
|
|
export function warning(message: string): void {
|
2019-05-21 15:26:44 -04:00
|
|
|
issue('warning', message)
|
2019-05-16 23:36:45 -04:00
|
|
|
}
|
2019-08-28 22:35:27 -04:00
|
|
|
|
2019-09-09 11:46:48 -04:00
|
|
|
/**
|
|
|
|
|
* Writes info to log with console.log.
|
|
|
|
|
* @param message info message
|
|
|
|
|
*/
|
|
|
|
|
export function info(message: string): void {
|
|
|
|
|
process.stdout.write(message + os.EOL)
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-28 22:35:27 -04:00
|
|
|
/**
|
|
|
|
|
* Begin an output group.
|
|
|
|
|
*
|
|
|
|
|
* Output until the next `groupEnd` will be foldable in this group
|
|
|
|
|
*
|
|
|
|
|
* @param name The name of the output group
|
|
|
|
|
*/
|
|
|
|
|
export function startGroup(name: string): void {
|
|
|
|
|
issue('group', name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* End an output group.
|
|
|
|
|
*/
|
|
|
|
|
export function endGroup(): void {
|
|
|
|
|
issue('endgroup')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wrap an asynchronous function call in a group.
|
|
|
|
|
*
|
|
|
|
|
* Returns the same type as the function itself.
|
|
|
|
|
*
|
|
|
|
|
* @param name The name of the group
|
|
|
|
|
* @param fn The function to wrap in the group
|
|
|
|
|
*/
|
|
|
|
|
export async function group<T>(name: string, fn: () => Promise<T>): Promise<T> {
|
|
|
|
|
startGroup(name)
|
2019-08-28 22:47:37 -04:00
|
|
|
|
|
|
|
|
let result: T
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
result = await fn()
|
|
|
|
|
} finally {
|
|
|
|
|
endGroup()
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-28 22:35:27 -04:00
|
|
|
return result
|
|
|
|
|
}
|
2019-09-19 22:02:45 -04:00
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
|
// Wrapper action state
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Saves state for current action, the state can only be retrieved by this action's post job execution.
|
|
|
|
|
*
|
|
|
|
|
* @param name name of the state to store
|
|
|
|
|
* @param value value to store
|
|
|
|
|
*/
|
|
|
|
|
export function saveState(name: string, value: string): void {
|
2019-09-19 22:18:51 -04:00
|
|
|
issueCommand('save-state', {name}, value)
|
2019-09-19 22:02:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the value of an state set by this action's main execution.
|
|
|
|
|
*
|
|
|
|
|
* @param name name of the state to get
|
|
|
|
|
* @returns string
|
|
|
|
|
*/
|
|
|
|
|
export function getState(name: string): string {
|
|
|
|
|
return process.env[`STATE_${name}`] || ''
|
|
|
|
|
}
|