2020-04-02 00:27:19 +07:00
import core from '@actions/core'
import os from 'os'
import Setup from './lib/setup.js'
import Package from './lib/package.js'
import Tag from './lib/tag.js'
import Regex from './lib/regex.js'
2020-04-03 01:58:51 +07:00
import Dockerfile from './lib/docker.js'
2020-04-02 00:27:19 +07:00
async function run ( ) {
try {
Setup . debug ( )
Setup . requireAnyEnv ( 'GITHUB_TOKEN' , 'INPUT_GITHUB_TOKEN' )
2020-04-02 01:41:56 +07:00
// Configure the default output
core . setOutput ( 'tagcreated' , 'no' )
2020-04-02 00:27:19 +07:00
// Identify the tag parsing strategy
const root = core . getInput ( 'root' , { required : false } ) || core . getInput ( 'package_root' , { required : false } ) || './'
2020-04-02 01:51:57 +07:00
const strategy = ( core . getInput ( 'regex_pattern' , { required : false } ) || '' ) . trim ( ) . length > 0 ? 'regex' : ( ( core . getInput ( 'strategy' , { required : false } ) || 'package' ) . trim ( ) . toLowerCase ( ) )
2020-04-02 02:09:24 +07:00
core . warning ( ` Attempting to use ${ strategy } version extraction strategy. ` )
2020-04-02 00:27:19 +07:00
// Extract the version number using the supplied strategy
let version = core . getInput ( 'root' , { required : false } )
version = version === null || version . trim ( ) . length === 0 ? null : version
switch ( strategy ) {
case 'docker' :
version = ( new Dockerfile ( root ) ) . version
break
case 'package' :
// Extract using the package strategy (this is the default strategy)
version = ( new Package ( root ) ) . version
break
case 'regex' :
2020-04-02 01:49:56 +07:00
version = ( new Regex ( root , new RegExp ( core . getInput ( 'regex_pattern' , { required : false } ) , 'i' ) ) ) . version
2020-04-02 00:27:19 +07:00
break
default :
core . setFailed ( ` " ${ strategy } " is not a recognized tagging strategy. Choose from: 'package' (package.json), 'docker' (uses Dockerfile), or 'regex' (JS-based RegExp). ` )
return
}
core . setOutput ( 'version' , version )
core . debug ( ` Detected version ${ version } ` )
// Configure a tag using the identified version
const tag = new Tag (
core . getInput ( 'tag_prefix' , { required : false } ) ,
version ,
core . getInput ( 'tag_suffix' , { required : false } )
)
2020-04-02 02:28:38 +07:00
core . warning ( ` Attempting to create ${ tag . name } tag. ` )
2020-04-02 10:19:36 +07:00
core . setOutput ( 'tagrequested' , tag . name )
2020-04-05 06:28:01 +07:00
core . setOutput ( 'prerelease' , tag . prerelease ? 'yes' : 'no' )
core . setOutput ( 'build' , tag . build ? 'yes' : 'no' )
2020-04-02 02:28:38 +07:00
2020-04-02 00:27:19 +07:00
// Check for existance of tag and abort (short circuit) if it already exists.
if ( await tag . exists ( ) ) {
core . warning ( ` " ${ tag . name } " tag already exists. ` + os . EOL )
core . setOutput ( 'tagname' , '' )
return
}
// The tag setter will autocorrect the message if necessary.
tag . message = core . getInput ( 'tag_message' , { required : false } ) . trim ( )
await tag . push ( )
2020-04-02 01:41:56 +07:00
2020-04-02 02:24:38 +07:00
core . setOutput ( 'tagname' , tag . name )
core . setOutput ( 'tagsha' , tag . sha )
core . setOutput ( 'taguri' , tag . uri )
core . setOutput ( 'tagmessage' , tag . message )
core . setOutput ( 'tagref' , tag . ref )
2020-04-02 01:41:56 +07:00
core . setOutput ( 'tagcreated' , 'yes' )
2020-04-02 00:27:19 +07:00
} catch ( error ) {
core . warning ( error . message )
2020-04-02 02:09:24 +07:00
core . warning ( error . stack )
2020-04-02 00:27:19 +07:00
core . setOutput ( 'tagname' , '' )
core . setOutput ( 'tagsha' , '' )
core . setOutput ( 'taguri' , '' )
core . setOutput ( 'tagmessage' , '' )
core . setOutput ( 'tagref' , '' )
core . setOutput ( 'tagcreated' , 'no' )
}
}
run ( )