This repository has been archived by the owner on Aug 30, 2021. It is now read-only.
generated from actions/javascript-action
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
151 lines (125 loc) · 4.41 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const path = require("path");
const core = require("@actions/core");
const exec = require("@actions/exec");
async function run() {
try {
/* =========================================================================
GRAB ALL RAW INPUT
========================================================================= */
const accessTokenRaw = core.getInput("accessToken", { required: true });
const imageNameRaw = core.getInput("imageName", { required: false });
const tagRaw = core.getInput("tag", { required: false });
const buildArgsRaw = core.getInput("buildArgs", { required: false });
const contextRaw = core.getInput("context", { required: true });
const contextNameRaw = core.getInput("contextName", { required: false });
const crRaw = core.getInput("containerRegistry", { required: true });
const repositoryRaw = core.getInput("repository", { required: false });
/* =========================================================================
SET ALL DEFAULTS
========================================================================= */
// String together the context.
const context = path.join(
process.env.GITHUB_WORKSPACE,
contextRaw
);
// String together the context name from the context.
const contextName = path.join(
context,
contextNameRaw
);
const username = process.env.GITHUB_ACTOR;
// The access token is as-is.
const accessToken = accessTokenRaw;
// Get the optional repository name.
let repository = repositoryRaw
if (!repository) {
repository = process.env.GITHUB_REPOSITORY;
}
repository = repository.toLowerCase();
// Decide whether or not we're pushing to container registry.
const containerRegistryEnabled = (crRaw == "true");
// Set some throwaway values.
const repoArray = repository.split("/");
// Process the image name.
let imageName = imageNameRaw;
if (!imageName) {
imageName = repoArray[repoArray.length - 1];
}
imageName = imageName.toLowerCase();
// Process the owner name.
const ownerName = repoArray[0];
// Process the build arguments.
let buildArgs = [];
if (buildArgsRaw) buildArgs = buildArgsRaw.match(/\w+=\S+/g).flatMap(str => ["--build-arg", str]);
// Generate a base image URL.
let imageURL;
if (containerRegistryEnabled) {
imageURL = `ghcr.io/${ownerName}/${imageName}`;
} else {
imageURL = `docker.pkg.github.com/${repository}/${imageName}`;
}
// Process the image tags.
let tags = ["--tag", "latest"];
if (tagRaw) tags = tagRaw.match(/\w\S+/g).flatMap(str => ["--tag", `${imageURL}:${str}`]);
/* =========================================================================
LOG IN TO DOCKER
========================================================================= */
// Log in.
if (containerRegistryEnabled) {
await exec.exec(
"docker",
[
"login",
"ghcr.io",
"--username",
ownerName,
"--password",
accessToken
]
);
} else {
await exec.exec(
"docker",
[
"login",
"docker.pkg.github.com",
"--username",
username,
"--password",
accessToken
]
);
}
/* =========================================================================
BUILD THE DOCKER IMAGES
========================================================================= */
// Build the Docker image(s).
await exec.exec(
"docker",
[
"build",
...tags,
...buildArgs,
"--file",
contextName,
context
]
);
/* =========================================================================
PUSH THE DOCKER IMAGES
========================================================================= */
// Push the Docker image(s).
let pushTags = tags.filter((item, index) => index % 2 === 1);
for (let pushImage of pushTags) {
await exec.exec("docker", [ "push", pushImage ]);
}
/* =========================================================================
OUTPUT
========================================================================= */
// Output the image URL.
core.setOutput("imageURL", imageURL);
} catch (error) {
core.setFailed(error.message);
}
}
run();