Master GitHub Actions with hands-on labs and exercises. Learn how to automate workflows, run tests, deploy applications, and more using GitHub's powerful automation platform. This repository has everything you need to get started with continuous integration and continuous deployment.
In this lab, you will create a workflow to automatically comment on new issues and add a label. This demonstrates how to use the github-script
action for interacting with GitHub’s REST API to enhance automation.
Estimated Duration: 20-30 minutes
.github/workflows/
named auto-comment-on-issues.yml
.Copy the following content into the workflow file:
name: Misc - Auto Comment on New Issue
on:
issues:
types: opened
workflow_dispatch:
jobs:
thanks:
runs-on: ubuntu-latest
permissions:
issues: write # Required to create comments
steps:
- uses: actions/github-script@v7
id: issue_script
with:
github-token: $
script: |
const issue_number = context.issue.number;
console.log(`issue_number: ${issue_number}`);
const owner = context.repo.owner;
const repo = context.repo.repo;
// Lookup issue info
const issue = await github.rest.issues.get({
repo, owner, issue_number
});
console.log(`issue: ${issue}`);
// Create comment thanking contributor
const comment = await github.rest.issues.createComment({
repo, owner, issue_number,
body: "Thanks for your contribution!"
});
console.log(`comment id: ${comment.data.id}`);
console.log(comment.data);
// Auto-label the issue
github.rest.issues.addLabels({
repo, owner, issue_number,
labels: ['todo-review']
});
// Make comment id available to subsequent steps
return comment.data.id;
- run: echo 'comment id=$'
main
branch.todo-review
.on
)issues
:
opened
event when a new issue is created.workflow_dispatch
:
jobs
)thanks
:
ubuntu-latest
.actions/github-script
action to interact with GitHub’s REST API.context.issue
object.todo-review
) to the issue using the GitHub REST API.Change the Comment Text:
body
field in the script to customize the comment text.body: 'We appreciate your input! Stay tuned for a response.';
Add Multiple Labels:
labels
array.labels: ['todo-review', 'needs-feedback'];
Trigger on Additional Events:
on
section to include other issue-related events, such as reopened
.on:
issues:
types: [opened, reopened]
If you want to test the workflow manually without creating a new issue:
In this lab, you:
github-script
action to comment on and label new issues.This workflow is a practical example of automating repository management tasks with GitHub Actions.