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

40 lines
1.0 KiB
JavaScript
Raw 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
2022-09-04 00:41:20 +08:00
// Remove start and end periods and dashes
result = result.replace(/^[-\.]+/, '')
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
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);
}