forked from petehouston/github-actions-query-branch-name
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (31 loc) · 1.24 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
const core = require('@actions/core');
async function run() {
try {
// output name
const OutputGitBranchName = 'git_branch_name';
// get input
const name = core.getInput('name', { required: true })
// current branch
const branchName = process.env.GITHUB_REF.split('/').slice(2).join('/');
// export to variable
core.exportVariable(name, branchName);
// set output
core.setOutput(OutputGitBranchName, branchName);
// check if this is a pull request
if (process.env.GITHUB_EVENT_NAME === 'pull_request') {
const baseBranchName = process.env.GITHUB_BASE_REF;
core.exportVariable(`${name}_BASE`, baseBranchName);
core.setOutput(`${OutputGitBranchName}_base`, baseBranchName);
const headBranchName = process.env.GITHUB_HEAD_REF;
core.exportVariable(`${name}_HEAD`, headBranchName);
core.setOutput(`${OutputGitBranchName}_head`, headBranchName);
// set default branch name to head
core.exportVariable(name, headBranchName);
core.setOutput(OutputGitBranchName, headBranchName);
}
}
catch (error) {
core.setFailed(error.message);
}
}
run()