branch-to-docker-tag-name/index.js

42 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

2022-09-03 23:48:34 +08:00
const core = require('@actions/core');
2022-09-04 00:06:07 +08:00
/**
https://docs.docker.com/engine/reference/commandline/tag/#description
2022-09-04 00:41:20 +08:00
A tag name must be valid ASCII and may contain lowercase and uppercase
letters, digits, underscores, periods and dashes. A tag name may not
start with a period or a dash and may contain a maximum of 128
characters.
2022-09-04 00:06:07 +08:00
**/
2022-09-03 23:48:34 +08:00
try {
2022-09-04 00:41:20 +08:00
const main = core.getInput('main')
const branch = core.getInput('branch');
2022-09-04 00:06:07 +08:00
const prefix = core.getInput('prefix');
2022-09-04 00:41:20 +08:00
if (main == branch) {
core.setOutput('tag-name', 'latest');
} else {
let result = branch;
2022-09-03 23:48:34 +08:00
2022-09-04 00:41:20 +08:00
// Remove prefix
if (branch.startsWith(prefix)) {
result = result.substr(prefix.length);
}
2022-09-04 00:06:07 +08:00
2022-09-04 00:41:20 +08:00
// Remove invalid characters
result = result.replaceAll(/[^a-zA-Z0-9\._-]/g, '')
2022-09-04 00:06:07 +08:00
// Remove starting end periods and dashes
2022-09-04 00:41:20 +08:00
result = result.replace(/^[-\.]+/, '')
2022-09-03 23:48:34 +08:00
2022-09-04 00:41:20 +08:00
// Maximum of 128 characters
result = result.substr(0, 128)
2022-09-03 23:48:34 +08:00
// Remove ending periods and dashes
result = result.replace(/[-\.]+$/, '')
2022-09-04 00:41:20 +08:00
core.setOutput('tag-name', result);
}
2022-09-03 23:48:34 +08:00
} catch (error) {
core.setFailed(error.message);
}