Closed rabicloud closed 3 years ago
💡Did you know that GitHub Script also grants you access to a full Node.js environment?
Although we wouldn't recommend using GitHub Script to write the logic for complex actions, there are use cases where you may want to leverage using a little more than just the octokit/rest.js API.
One such use case is our issue comment. Right now it is pretty hard coded the way it is making it less than ideal. What if we wanted to display our contribution guide every time an issue was opened?
Instead of writing the guide directly into our workflow, we can use the Node.js File System module to read a file and use it as the body of our issue comment.
If we want access to the files within our repository, we need to make sure we include the actions/checkout
action in our workflow as the first step.
We will make the following changes to the current workflow file:
actions/checkout
action so we can read the templated response file located at .github/ISSUE_RESPONSES/comment.md
Edit the current workflow to have the following contents:
name: Learning GitHub Script
on:
issues:
types: [opened]
jobs:
comment:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v2
- name: Comment on new issue
uses: actions/github-script@0.8.0
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const fs = require('fs')
const issueBody = fs.readFileSync(".github/ISSUE_RESPONSES/comment.md", "utf8")
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: issueBody
})
- name: Add issue to project board
if: contains(github.event.issue.labels.*.name, 'bug')
uses: actions/github-script@0.8.0
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.projects.createCard({
column_id: 12969373,
content_id: context.payload.issue.id,
content_type: "Issue"
});
Commit the workflow changes to this branch.
I am waiting for you to commit the desired changes to this branch before moving on.
I'll respond once you've committed the changes to this branch
I have created a new issue where we will continue this lesson. Click the link to meet me over there.
Let's improve the workflow
@rabicloud it looks like you workflow has completed! Let's take a look at the results!
We should be pretty familiar with the first portion of the workflow, it's the same as the first time it ran, and it just creates a comment whenever a new issue has been created.
The second portion may exactly be clear to you, but this issue was added to a project board that is present in this repository. Checkout the projects tab if you want to see that this issue has been added! Multiple steps
One benefit of using actions is the ability to separate jobs into smaller units of work called steps. If we think about what our workflow is doing we can see that it makes more sense to have these two tasks take place across two steps.
As an added advantage, once we break the current workflow into multiple steps we can apply logic through expressions to them. This will let us create rules around when steps are allowed to run. Ultimately this allows us to optimize our workflow run!
Since GitHub Script is simply an action, breaking each unique task into a new step works just fine! We will do this in our next activity!