-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (83 loc) · 3.26 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
const docker = require('./docker');
const humanitecFactory = require('./humanitec');
const fs = require('fs');
const core = require('@actions/core');
/**
* Performs the GitHub action.
*/
async function runAction() {
// Get GitHub Action inputs
const token = core.getInput('humanitec-token', {required: true});
const orgId = core.getInput('organization', {required: true});
const moduleName = core.getInput('module-name') || process.env.GITHUB_REPOSITORY.replace(/.*\//, '');
const dockerfile = core.getInput('dockerfile') || '.';
const registryHost = core.getInput('humanitec-registry') || 'registry.humanitec.io';
const apiHost = core.getInput('humanitec-api') || 'api.humanitec.io';
if (!fs.existsSync(`${process.env.GITHUB_WORKSPACE}/.git`)) {
core.error('It does not look like anything was checked out.');
core.error('Did you run a checkout step before this step? ' +
'http:/docs.humanitec.com/connecting-your-ci#github-actions');
core.setFailed('No .git directory found in workspace.');
return;
}
// As the user can choose their module name, we need to ensure it is a valid slug (i.e. lowercase kebab case)
if (! moduleName.match(/^[a-z0-9][a-z0-9-]*[a-z0-9]$/)) {
core.error('module-name must be all lowercase letters, numbers and the "-" symbol. ' +
'It cannot start or end with "-".');
core.setFailed('module-name: "${moduleName}" is not valid.');
return;
}
if (!fs.existsSync(dockerfile)) {
core.error(`Cannot find Dockerfile at ${dockerfile}`);
core.setFailed('Cannot find Dockerfile.');
return;
}
const humanitec = humanitecFactory(token, orgId, apiHost);
let registryCreds;
try {
registryCreds = await humanitec.getRegistryCredentials();
} catch (error) {
core.error('Unable to fetch repository credentials.');
core.error('Did you add the token to your Github Secrets? ' +
'http:/docs.humanitec.com/connecting-your-ci#github-actions');
core.setFailed('Unable to access Humanitec.');
return;
}
if (!docker.login(registryCreds.username, registryCreds.password, registryHost)) {
core.setFailed('Unable to connect to the humanitec registry.');
return;
}
process.chdir(process.env.GITHUB_WORKSPACE);
const localTag = `${orgId}/${moduleName}:${process.env.GITHUB_SHA}`;
const imageId = await docker.build(localTag, dockerfile);
if (!imageId) {
core.setFailed('Unable build image from Dockerfile.');
return;
}
const remoteTag = `${registryHost}/${localTag}`;
if (!docker.push(imageId, remoteTag)) {
core.setFailed('Unable to push image to registry');
return;
}
const payload = {
commit: process.env.GITHUB_SHA,
image: remoteTag,
};
if (process.env.GITHUB_REF.includes('\/heads\/')) {
payload.branch = process.env.GITHUB_REF.replace(/.*\/heads\//, '');
payload.tags = [];
} else {
payload.branch = '';
payload.tags = [process.env.GITHUB_REF.replace(/.*\/tags\//, '')];
}
try {
await humanitec.addNewBuild(moduleName, payload);
} catch (error) {
core.error('Unable to notify Humanitec about build.');
core.error('Did you add the token to your Github Secrets? ' +
'http:/docs.humanitec.com/connecting-your-ci#github-actions');
core.setFailed('Unable to access Humanitec.');
return;
}
}
runAction();