forked from WyriHaximus/github-action-get-previous-tag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
37 lines (32 loc) · 1.59 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const { exec } = require('child_process');
const fs = require('fs');
const tagPrefix = `${process.env.INPUT_PREFIX || ''}*`;
exec(`git for-each-ref --sort=-creatordate --count 1 --format="%(refname:short)" "refs/tags/${tagPrefix}"`, (err, tag, stderr) => {
tag = tag.trim();
if (err) {
console.log('\x1b[33m%s\x1b[0m', 'Could not find any tags because: ');
console.log('\x1b[31m%s\x1b[0m', stderr);
process.exit(1);
} else if (tag === "") {
let timestamp = Math.floor(new Date().getTime() / 1000);
console.log('\x1b[33m%s\x1b[0m', 'Falling back to default tag');
console.log('\x1b[32m%s\x1b[0m', `Found tag: ${process.env.INPUT_FALLBACK}`);
console.log('\x1b[32m%s\x1b[0m', `Found timestamp: ${timestamp}`);
fs.appendFileSync(process.env.GITHUB_OUTPUT, `tag=${process.env.INPUT_FALLBACK}\n`);
fs.appendFileSync(process.env.GITHUB_OUTPUT, `timestamp=${timestamp}\n`);
process.exit(0);
}
exec(`git log -1 --format=%at ${tag}`, (err, timestamp, stderr) => {
if (err) {
console.log('\x1b[33m%s\x1b[0m', 'Could not find any timestamp because: ');
console.log('\x1b[31m%s\x1b[0m', stderr);
process.exit(1);
}
timestamp = timestamp.trim();
console.log('\x1b[32m%s\x1b[0m', `Found tag: ${tag}`);
console.log('\x1b[32m%s\x1b[0m', `Found timestamp: ${timestamp}`);
fs.appendFileSync(process.env.GITHUB_OUTPUT, `tag=${tag}\n`);
fs.appendFileSync(process.env.GITHUB_OUTPUT, `timestamp=${timestamp}\n`);
process.exit(0);
});
});