-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
122 lines (104 loc) · 2.89 KB
/
index.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const core = require("@actions/core");
const github = require("@actions/github");
function stringifyDate(date) {
return date.toISOString().substring(0, 10);
}
function getTagName(appName, environment) {
date = stringifyDate(new Date());
let tag = `${appName}-${date}`;
if (environment) {
tag = `${tag}-${environment}`;
}
return tag;
}
async function run() {
try {
const appName = core.getInput("app-name", {required: true});
if (!appName) {
core.setFailed('Input "app-name" is missing');
}
let environment = "";
const environmentInput = core.getInput("environment", {required: false});
if (environmentInput) {
environment = environmentInput;
}
let githubToken = core.getInput("github-token", {required: false});
if (!githubToken) {
if (process.env.GITHUB_TOKEN) {
githubToken = process.env.GITHUB_TOKEN;
} else {
core.setFailed(
'Input "github-token" is missing, and not provided in environment'
);
}
}
const octokit = github.getOctokit(githubToken);
let { owner, repo } = github.context.repo;
const repoInput = core.getInput("repo", {required: false});
if (repoInput) {
repo = repoInput;
}
const ownerInput = core.getInput("owner", {required: false});
if (ownerInput) {
owner = ownerInput;
}
const tagName = getTagName(appName, environment);
core.setOutput("tag", tagName);
const tagMessage = `${tagName} deployed via GitHub Actions`;
const sha = core.getInput("sha", {required: false}) || process.env.GITHUB_SHA;
if (!sha) {
core.setFailed(
'SHA to tag must be provided as input "sha" or environment variable "GITHUB_SHA"'
);
}
console.log(`Repo: ${owner}/${repo}, Tag: ${tagName}, SHA: ${sha}`);
const createdTag = await octokit.git.createTag({
owner,
repo,
tag: tagName,
message: tagMessage,
object: sha,
type: "commit",
});
console.log("Tag created successfully.");
const refName = `tags/${tagName}`;
let refExists = false;
try {
const existingRef = await octokit.git.getRef({
owner,
repo,
ref: refName,
});
refExists = true;
console.log("Existing ref found. Will update.");
} catch (error) {
if (error.status != 404) {
throw error;
}
}
let newRef;
if (refExists) {
// Update ref to point to current commit
newRef = await octokit.git.updateRef({
owner,
repo,
ref: refName,
sha,
});
console.log("Ref updated.");
} else {
// Create new ref
const fullRefName = `refs/${refName}`;
newRef = await octokit.git.createRef({
owner,
repo,
ref: fullRefName,
sha,
});
console.log("Ref created.");
}
} catch (error) {
core.setFailed(error.message);
}
}
run();