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

44 lines
1.1 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
Name components may contain lowercase letters, digits and separators.
A separator is defined as a period, one or two underscores, or one or
more dashes. A name component may not start or end with a separator.
**/
2022-09-03 23:48:34 +08:00
try {
const path = core.getInput('path');
2022-09-04 00:06:07 +08:00
const prefix = core.getInput('prefix');
let result = path;
2022-09-03 23:48:34 +08:00
// Remove prefix
if (path.startsWith(prefix)) {
2022-09-04 00:06:07 +08:00
result = result.substr(prefix.length);
2022-09-03 23:48:34 +08:00
}
2022-09-04 00:06:07 +08:00
// Remove start
result = result.replace(/^\./, '');
// Replace '/' (we expect lots of / in paths)
result = result.replaceAll('/', '-');
// Replace excessive underscores
2022-09-04 00:13:37 +08:00
result = result.replaceAll(/___+/g, '__');
2022-09-04 00:06:07 +08:00
// Lower case
result = result.toLowerCase();
2022-09-03 23:48:34 +08:00
2022-09-04 00:06:07 +08:00
// Remove invalid characters
2022-09-04 00:13:37 +08:00
result = result.replaceAll(/[^a-z0-9\._-]/g, '')
2022-09-03 23:48:34 +08:00
2022-09-04 00:06:07 +08:00
// Remove start and end seperators
result = result.replace(/^[-\._]+/, '')
result = result.replace(/[-\._]+$/, '')
2022-09-03 23:48:34 +08:00
core.setOutput('image-name', result);
} catch (error) {
core.setFailed(error.message);
}