forked from kriasoft/create-dns-record
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.js
110 lines (91 loc) · 3.12 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
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
/**
* Create CloudFlare DNS Record Action for GitHub
* https://github.com/marketplace/actions/cloudflare-create-dns-record
*/
const path = require("path");
const cp = require("child_process");
const getCurrentRecordId = () => {
//https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records
const { status, stdout } = cp.spawnSync("curl", [
...["--header", `Authorization: Bearer ${process.env.INPUT_TOKEN}`],
...["--header", "Content-Type: application/json"],
`https://api.cloudflare.com/client/v4/zones/${process.env.INPUT_ZONE}/dns_records`,
]);
if (status !== 0) {
process.exit(status);
}
const { success, result, errors } = JSON.parse(stdout.toString());
if (!success) {
console.log(`::error ::${errors[0].message}`);
process.exit(1);
}
const name = process.env.INPUT_NAME;
const record = result.find((x) => x.name === name);
if (!record) {
return null
}
return record.id;
};
const createRecord = () => {
// https://api.cloudflare.com/#dns-records-for-a-zone-create-dns-record
const { status, stdout } = cp.spawnSync("curl", [
...["--request", "POST"],
...["--header", `Authorization: Bearer ${process.env.INPUT_TOKEN}`],
...["--header", "Content-Type: application/json"],
...["--silent", "--data"],
JSON.stringify({
type: process.env.INPUT_TYPE,
name: process.env.INPUT_NAME,
content: process.env.INPUT_CONTENT,
ttl: Number(process.env.INPUT_TTL),
proxied: process.env.INPUT_PROXIED == "true",
}),
`https://api.cloudflare.com/client/v4/zones/${process.env.INPUT_ZONE}/dns_records`,
]);
if (status !== 0) {
process.exit(status);
}
const { success, result, errors } = JSON.parse(stdout.toString());
if (!success) {
console.dir(errors[0]);
console.log(`::error ::${errors[0].message}`);
process.exit(1);
}
console.log(`::set-output name=id::${result.id}`);
console.log(`::set-output name=name::${result.name}`);
};
const updateRecord = (id) => {
console.log(`Record exists with ${id}, updating...`);
// https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record
const { status, stdout } = cp.spawnSync("curl", [
...["--request", "PUT"],
...["--header", `Authorization: Bearer ${process.env.INPUT_TOKEN}`],
...["--header", "Content-Type: application/json"],
...["--silent", "--data"],
JSON.stringify({
type: process.env.INPUT_TYPE,
name: process.env.INPUT_NAME,
content: process.env.INPUT_CONTENT,
ttl: Number(process.env.INPUT_TTL),
proxied: process.env.INPUT_PROXIED == "true",
}),
`https://api.cloudflare.com/client/v4/zones/${process.env.INPUT_ZONE}/dns_records/${id}`,
]);
if (status !== 0) {
process.exit(status);
}
const { success, result, errors } = JSON.parse(stdout.toString());
if (!success) {
console.dir(errors[0]);
console.log(`::error ::${errors[0].message}`);
process.exit(1);
}
console.log(`::set-output name=record_id::${result.id}`);
console.log(`::set-output name=name::${result.name}`);
}
const id = getCurrentRecordId();
if (id) {
updateRecord(id);
process.exit(0);
}
createRecord();