-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
159 lines (129 loc) · 4.81 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
152
153
154
155
156
157
158
159
const core = require('@actions/core');
const github = require('@actions/github');
const exec = require('@actions/exec');
const glob = require('@actions/glob');
const artifact = require('@actions/artifact');
const fs = require('fs');
const path = require('path');
const os = require('os');
// Input variable names
const inputAppName = 'app-name';
const buildConfigurationName = 'build-configuration';
let dockerImage = '';
let tag = '';
let packageVersion = '';
async function run() {
await runStep(getPackageVersion, 'Loading package version');
await runStep(setUpVersion, 'Prepare docker version.');
await runStep(buildAndPush, 'Build and push docker container');
await runStep(extractBuildResult, 'Extract build result');
await runStep(uploadArtifacts, 'Upload artifacts');
}
async function runStep(step, displayText) {
try {
console.log(`${displayText} started.`);
await step();
console.log(`${displayText} finished.`)
} catch (error) {
core.setFailed(`Step "${displayText}" failed. Error: ${error.message}`);
throw error;
}
}
async function getPackageVersion() {
try {
console.log("installing nbgv via dotnet tool");
await exec.exec("dotnet tool install -g nbgv");
console.log("done installing nbgv via dotnet tool");
} catch(err) {
console.log(`Error while installing nbgv: ${err}. We try to proceed anyway.`)
}
core.addPath(path.join(os.homedir(), ".dotnet", "tools"));
let versionJsonPath = undefined;
let versionJsonDirectory = undefined;
let versionJson = "";
console.log("find version.json");
await exec.exec('find . -name "version.json"', [], {
listeners: {
stdout: (data) => {
versionJsonPath = data.toString();
},
},
});
if (!versionJsonPath) {
console.error("Version Json not found.");
}
console.log("find directory of version.json");
await exec.exec(`dirname ${versionJsonPath}`, [], {
listeners: {
stdout: (data) => {
versionJsonDirectory = `${data.toString()}`;
},
},
});
versionJsonDirectory = versionJsonDirectory.replace(/(\r\n|\n|\r)/gm, "") + "/";
console.log(`run nbgv get-version -p ${versionJsonDirectory}`);
await exec.exec(`nbgv get-version -p ${versionJsonDirectory}`);
console.log(`run nbgv get-version -f json -p ${versionJsonDirectory}`);
let myError = '';
await exec.exec(`nbgv get-version -f json -p ${versionJsonDirectory}`, [], {
listeners: {
stdout: (data) => {
versionJson += data.toString();
},
stderr: (data) => {
myError += data.toString().trim();
}
},
});
if (myError) {
throw new Error(myError);
}
console.log(`set version in output`);
packageVersion = JSON.parse(versionJson)["CloudBuildAllVars"]["NBGV_NuGetPackageVersion"];
core.setOutput("version", packageVersion);
let isPreRelease = false;
if (packageVersion.includes("-")) {
isPreRelease = true;
}
core.setOutput("is-pre-release", isPreRelease);
}
async function buildAndPush() {
let dockerFile = undefined;
let buildConfiguration = core.getInput(buildConfigurationName).toLowerCase() || '';
await exec.exec('find . -name "Dockerfile"', [], { listeners: { stdout: (data) => { dockerFile = data.toString() } } });
if (!dockerFile) {
console.error('Dockerfile not found');
}
dockerFile = dockerFile.replace(/(\r\n|\n|\r)/gm, '');
await exec.exec(`docker build . -f ${dockerFile} -t ${tag} -t ${dockerImage}:${packageVersion} --build-arg BUILD_CONFIGURATION=${buildConfiguration}`);
}
async function extractBuildResult() {
await exec.exec(`docker create --name extract "${tag}"`);
await exec.exec('mkdir extracted-app');
await exec.exec('docker cp extract:/app/dist ./extracted-app');
await exec.exec('docker rm extract');
}
async function setUpVersion() {
let repositoryName = core.getInput(inputAppName).toLowerCase();
let version = `edge`;
dockerImage = `${repositoryName}`;
if (github.context.ref.startsWith('refs/tags')) {
version = github.context.ref.replace('refs/tags/', '');
} else if (github.context.ref.startsWith('refs/heads/')) {
version = github.context.ref.replace('refs/heads/', '').replace('/', '-');
} else if (github.context.ref.startsWith('refs/pull/')) {
const ev = JSON.parse(
fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8')
);
version = `pr-${ev.pull_request.number}`;
}
tag = `${dockerImage}:${version}`;
}
async function uploadArtifacts() {
const globber = await glob.create('./extracted-app/**');
const files = await globber.glob();
const name = `${core.getInput(inputAppName)}-${packageVersion}`;
await artifact.create().uploadArtifact(name, files, './extracted-app');
core.setOutput("artifact-name", name);
}
run().then(_ => {});