forked from CircleCI-Public/trigger-circleci-pipeline-action
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
95 lines (85 loc) · 2.31 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
import {
getInput,
setFailed,
setOutput,
startGroup,
endGroup,
info,
error as coreError,
} from "@actions/core";
import { context } from "@actions/github";
import axios from "axios";
startGroup("Preparing CircleCI Pipeline Trigger");
const repoOrg = context.repo.owner;
const repoName = context.repo.repo;
info(`Org: ${repoOrg}`);
info(`Repo: ${repoName}`);
const ref = context.ref;
const getBranch = () => {
if (ref.startsWith("refs/heads/")) {
return ref.substring(11);
} else if (ref.startsWith("refs/pull/")) {
info(`This is a PR. Using head PR branch`);
const pullRequestNumber = ref.match(/refs\/pull\/([0-9]*)\//)[1];
const newref = `pull/${pullRequestNumber}/head`;
return newref;
}
return ref;
};
const getTag = () => {
if (ref.startsWith("refs/tags/")) {
return ref.substring(10);
}
};
const headers = {
"content-type": "application/json",
"x-attribution-login": context.actor,
"x-attribution-actor-id": context.actor,
"Circle-Token": `${process.env.CCI_TOKEN}`,
};
const parameters = {
GHA_Actor: context.actor,
GHA_Action: context.action,
GHA_Event: context.eventName,
};
const metaData = getInput("GHA_Meta");
if (metaData.length > 0) {
Object.assign(parameters, { GHA_Meta: metaData });
}
const body = {
parameters: parameters,
};
const tag = getTag();
const branch = getBranch();
if (tag) {
Object.assign(body, { tag });
} else {
Object.assign(body, { branch });
}
const url = `https://circleci.com/api/v2/project/gh/${repoOrg}/${repoName}/pipeline`;
info(`Triggering CircleCI Pipeline for ${repoOrg}/${repoName}`);
info(`Triggering URL: ${url}`);
if (tag) {
info(`Triggering tag: ${tag}`);
} else {
info(`Triggering branch: ${branch}`);
}
info(`Parameters:\n${JSON.stringify(parameters)}`);
endGroup();
axios
.post(url, body, { headers: headers })
.then((response) => {
startGroup("Successfully triggered CircleCI Pipeline");
info(`CircleCI API Response: ${JSON.stringify(response.data)}`);
setOutput("id", response.data.id);
setOutput("number", response.data.number);
setOutput("state", response.data.state);
setOutput("created_at", response.data.created_at);
endGroup();
})
.catch((error) => {
startGroup("Failed to trigger CircleCI Pipeline");
coreError(error);
setFailed(error.message);
endGroup();
});