Skip to content

Instantly share code, notes, and snippets.

@hariombalhara
Last active June 9, 2022 07:01
Show Gist options
  • Save hariombalhara/969bd98ec14006d5b8b60fb51d018aa2 to your computer and use it in GitHub Desktop.
Save hariombalhara/969bd98ec14006d5b8b60fb51d018aa2 to your computer and use it in GitHub Desktop.
Script Kit: Github Actions
// Menu: Github Actions
// Description: Run Github Workflow configured to run on workflow_dispatch
// Author: Hariom Balhara
// Shortcut: opt g
// Twitter: @hariom_balhara
// Format https://api.github.com/repos/{owner}/{repo}/actions/workflows
let githubWorkflowUrl = await env('WORKFLOWS_PREFIX')
// Obtain from here - https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token
let githubPersonalAccessToken = await env('GITHUB_ACCESS_TOKEN')
let requestOptions = {
headers: {
"Authorization": `token ${githubPersonalAccessToken}`,
"Accept": "application/vnd.github.v3+json"
}
};
let workflows = (await get(
githubWorkflowUrl,
requestOptions
)).data.workflows;
if (!workflows) {
md('Couldn\'t fetch workflows')
process.exit(1);
}
let workflowsMap = {};
workflows.forEach((workflow) => {
workflowsMap[workflow.name] = workflow;
});
let workflowName = await arg("Choose a github workflow", workflows.map((workflow)=>{return workflow.name}))
let chosenWorkflow = workflowsMap[workflowName]
let workflowId = chosenWorkflow.id
let branch = await arg('Branch Name: Default is main')
if (!branch) {
branch = 'main'
}
try {
await post(`${githubWorkflowUrl}/${workflowId}/dispatches`, {ref: branch}, {...requestOptions});
} catch(e) {
await arg('Error Occurred', md(e.response.data.message))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment