mirror of
https://github.com/rlespinasse/github-slug-action.git
synced 2026-06-13 01:51:09 +00:00
feat: add support for windows and macos jobs
BREAKING CHANGE: The action implementation move from container action to node.js action Co-authored-by: Romain Lespinasse <romain.lespinasse@gmail.com>
This commit is contained in:
committed by
GitHub
parent
6efa53ebca
commit
13c2f38dad
97
src/main.ts
Normal file
97
src/main.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
import * as core from '@actions/core'
|
||||
import {slugref, slug, slugurl, slugurlref, shortsha} from './slug'
|
||||
|
||||
/**
|
||||
* Inputs environments variables keys from Github actions job
|
||||
* see https://docs.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables
|
||||
*/
|
||||
const GITHUB_REPOSITORY = 'GITHUB_REPOSITORY'
|
||||
const GITHUB_REF = 'GITHUB_REF'
|
||||
const GITHUB_HEAD_REF = 'GITHUB_HEAD_REF'
|
||||
const GITHUB_BASE_REF = 'GITHUB_BASE_REF'
|
||||
const GITHUB_SHA = 'GITHUB_SHA'
|
||||
const GITHUB_EVENT_PATH = 'GITHUB_EVENT_PATH'
|
||||
|
||||
/**
|
||||
* Slugged outputs environments variables keys
|
||||
*/
|
||||
const GITHUB_REPOSITORY_SLUG = 'GITHUB_REPOSITORY_SLUG'
|
||||
const GITHUB_REPOSITORY_SLUG_URL = 'GITHUB_REPOSITORY_SLUG_URL'
|
||||
const GITHUB_REF_SLUG = 'GITHUB_REF_SLUG'
|
||||
const GITHUB_HEAD_REF_SLUG = 'GITHUB_HEAD_REF_SLUG'
|
||||
const GITHUB_BASE_REF_SLUG = 'GITHUB_BASE_REF_SLUG'
|
||||
const GITHUB_REF_SLUG_URL = 'GITHUB_REF_SLUG_URL'
|
||||
const GITHUB_HEAD_REF_SLUG_URL = 'GITHUB_HEAD_REF_SLUG_URL'
|
||||
const GITHUB_BASE_REF_SLUG_URL = 'GITHUB_BASE_REF_SLUG_URL'
|
||||
const GITHUB_SHA_SHORT = 'GITHUB_SHA_SHORT'
|
||||
const GITHUB_EVENT_REF_SLUG = 'GITHUB_EVENT_REF_SLUG'
|
||||
const GITHUB_EVENT_REF_SLUG_URL = 'GITHUB_EVENT_REF_SLUG_URL'
|
||||
|
||||
async function run(): Promise<void> {
|
||||
try {
|
||||
const eventPath = process.env[GITHUB_EVENT_PATH]
|
||||
if (eventPath) {
|
||||
const eventData = await import(eventPath)
|
||||
if (eventData.hasOwnProperty('ref')) {
|
||||
core.exportVariable(GITHUB_EVENT_REF_SLUG, slugref(eventData.ref))
|
||||
core.exportVariable(
|
||||
GITHUB_EVENT_REF_SLUG_URL,
|
||||
slugurlref(eventData.ref)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
exportSlug(GITHUB_REPOSITORY, GITHUB_REPOSITORY_SLUG)
|
||||
|
||||
exportSlugUrl(GITHUB_REPOSITORY, GITHUB_REPOSITORY_SLUG_URL)
|
||||
|
||||
exportSlugRef(GITHUB_REF, GITHUB_REF_SLUG)
|
||||
exportSlugRef(GITHUB_HEAD_REF, GITHUB_HEAD_REF_SLUG)
|
||||
exportSlugRef(GITHUB_BASE_REF, GITHUB_BASE_REF_SLUG)
|
||||
|
||||
exportSlugUrlRef(GITHUB_REF, GITHUB_REF_SLUG_URL)
|
||||
exportSlugUrlRef(GITHUB_HEAD_REF, GITHUB_HEAD_REF_SLUG_URL)
|
||||
exportSlugUrlRef(GITHUB_BASE_REF, GITHUB_BASE_REF_SLUG_URL)
|
||||
|
||||
exportShortSha(GITHUB_SHA, GITHUB_SHA_SHORT)
|
||||
} catch (error) {
|
||||
core.setFailed(error.message)
|
||||
}
|
||||
}
|
||||
|
||||
function exportSlug(inputKey: string, outputKey: string): void {
|
||||
const envVar = process.env[inputKey]
|
||||
if (envVar) {
|
||||
core.exportVariable(outputKey, slug(envVar))
|
||||
}
|
||||
}
|
||||
|
||||
function exportSlugRef(inputKey: string, outputKey: string): void {
|
||||
const envVar = process.env[inputKey]
|
||||
if (envVar) {
|
||||
core.exportVariable(outputKey, slugref(envVar))
|
||||
}
|
||||
}
|
||||
|
||||
function exportSlugUrl(inputKey: string, outputKey: string): void {
|
||||
const envVar = process.env[inputKey]
|
||||
if (envVar) {
|
||||
core.exportVariable(outputKey, slugurl(envVar))
|
||||
}
|
||||
}
|
||||
|
||||
function exportSlugUrlRef(inputKey: string, outputKey: string): void {
|
||||
const envVar = process.env[inputKey]
|
||||
if (envVar) {
|
||||
core.exportVariable(outputKey, slugurlref(envVar))
|
||||
}
|
||||
}
|
||||
|
||||
function exportShortSha(inputKey: string, outputKey: string): void {
|
||||
const envVar = process.env[inputKey]
|
||||
if (envVar) {
|
||||
core.exportVariable(outputKey, shortsha(envVar))
|
||||
}
|
||||
}
|
||||
|
||||
run()
|
||||
79
src/slug.ts
Normal file
79
src/slug.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
const MAX_SLUG_STRING_SIZE = 63
|
||||
const SHORT_SHA_SIZE = 8
|
||||
|
||||
/**
|
||||
* slug will take envVar and then :
|
||||
* - put the variable content in lower case
|
||||
* - replace any character by `-` except `0-9`, `a-z`, and `.`
|
||||
* - remove leading and trailing `-` character
|
||||
* - limit the string size to 63 characters
|
||||
* @param envVar to be slugged
|
||||
*/
|
||||
export function slug(envVar: string): string {
|
||||
return trailHyphen(
|
||||
replaceAnyNonAlphanumericCaracter(envVar.toLowerCase())
|
||||
).substring(0, MAX_SLUG_STRING_SIZE)
|
||||
}
|
||||
|
||||
/**
|
||||
* slug will take envVar and then :
|
||||
* - remove refs/(heads|tags)/
|
||||
* - put the variable content in lower case
|
||||
* - replace any character by `-` except `0-9`, `a-z`, and `.`
|
||||
* - remove leading and trailing `-` character
|
||||
* - limit the string size to 63 characters
|
||||
* @param envVar to be slugged
|
||||
*/
|
||||
export function slugref(envVar: string): string {
|
||||
return slug(removeRef(envVar.toLowerCase()))
|
||||
}
|
||||
|
||||
/**
|
||||
* slug will take envVar and then :
|
||||
* - put the variable content in lower case
|
||||
* - replace any character by `-` except `0-9`, `a-z`
|
||||
* - remove leading and trailing `-` character
|
||||
* - limit the string size to 63 characters
|
||||
* @param envVar to be slugged
|
||||
*/
|
||||
export function slugurl(envVar: string): string {
|
||||
return slug(replaceAnyDotToHyphen(envVar))
|
||||
}
|
||||
|
||||
/**
|
||||
* slug will take envVar and then :
|
||||
* - remove refs/(heads|tags)/
|
||||
* - put the variable content in lower case
|
||||
* - replace any character by `-` except `0-9`, `a-z`
|
||||
* - remove leading and trailing `-` character
|
||||
* - limit the string size to 63 characters
|
||||
* @param envVar to be slugged
|
||||
*/
|
||||
export function slugurlref(envVar: string): string {
|
||||
return slugurl(slugref(envVar))
|
||||
}
|
||||
|
||||
/**
|
||||
* slug will take envVar and then :
|
||||
* - limit the string size to 8 characters
|
||||
* @param envVar to be slugged
|
||||
*/
|
||||
export function shortsha(envVar: string): string {
|
||||
return envVar.substring(0, SHORT_SHA_SIZE)
|
||||
}
|
||||
|
||||
function trailHyphen(envVar: string): string {
|
||||
return envVar.replace(RegExp('^-*', 'g'), '').replace(RegExp('-*$', 'g'), '')
|
||||
}
|
||||
|
||||
function replaceAnyNonAlphanumericCaracter(envVar: string): string {
|
||||
return envVar.replace(RegExp('[^a-z0-9.]', 'g'), '-')
|
||||
}
|
||||
|
||||
function replaceAnyDotToHyphen(envVar: string): string {
|
||||
return envVar.replace(RegExp('[.]', 'g'), '-')
|
||||
}
|
||||
|
||||
function removeRef(envVar: string): string {
|
||||
return envVar.replace(RegExp('^refs/(heads|tags)/'), '')
|
||||
}
|
||||
Reference in New Issue
Block a user