Closed kng72 closed 2 years ago
As with the other actions we wrote, we are going to need to setup a few directories and files.
This time we will start with the dependencies for our action. JavaScript projects can be packaged with a package.json
file which contains metadata and configuration information about a project. In our case we will use some pieces of the actions toolkit.
package.json
to issue-makerCreate and add the following contents to the .github/actions/issue-maker/package.json
file:
You can use this link to easily create this file.
{
"name": "issue-maker",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@actions/core": "^1.2.2",
"@actions/github": "^2.1.0"
}
}
Commit the changes to this branch.
Click the green Commit new file
button
I'll respond when you push changes to this pull request.
Like our "hello world" action, this action will require at least one input:
parameter. We need this parameter so that our JavaScript for this action has access to the output:
from the joke action.
If you recall, in the my-workflow.yml
file, we stated this action would take a specific input named catFact:
and we set it's value to the output of the previous action.
- name: create-issue
uses: ./.github/actions/issue-maker
with:
catFact: ${{steps.cat.outputs.fact}}
Because of this, we need to define catFact:
as one of our inputs:
for this action. Remember when we did this with the first action? It looked a little like this:
inputs:
catFact:
description: "the cat fact retreived from a previous action"
required: true
default: "Mona is an Octocat"
We also will also need to authenticate to GitHub with the action so that we can interact with the GitHub API. For that we will use a special token that gets created for us when we use actions called GITHUB_TOKEN
Our action will also need to accept an input so that we can let the user specify a name for the issue that will be created when this action runs.
action.yml
for issue-maker💡All of the following steps take place inside of the .github/actions/issue-maker
directory.
Create and add the following contents to the .github/actions/issue-maker/action.yml
file:
You can use this link to easily create this file.
name: "issue maker"
description: "create and issue with a cat fact as the body"
inputs:
issueTitle:
description: "A name for the cat-fact issue"
required: true
default: "A cat fact for you"
catFact:
description: "the cat fact retreived from a previous action"
required: true
default: "Mona is an Octocat"
repoToken:
description: "Authentication token, use secrets.GITHUB_TOKEN"
required: true
runs:
using: "docker"
image: "Dockerfile"
Commit the changes to this branch.
Click the green Commit new file
button
I'll respond when you push changes to this pull request.
src/index.js
for issue-maker💡All of the following steps take place inside of the .github/actions/issue-maker/src
directory.
Create and add the following contents to the .github/actions/issue-maker/src/index.js
file:
You can use this link to easily create this file.
const core = require("@actions/core");
const github = require("@actions/github");
async function run() {
const issueTitle = core.getInput("issueTitle");
const catFact = core.getInput("catFact");
const token = core.getInput("repoToken");
try {
const octokit = new github.GitHub(token);
const newIssue = await octokit.issues.create({
repo: github.context.repo.repo,
owner: github.context.repo.owner,
title: issueTitle,
body: catFact
});
} catch (error) {
core.setFailed(error.message);
}
}
run();
Commit the changes to this branch.
Click the green Commit new file
button
I'll respond when you push changes to this pull request.
Dockerfile
Dockerfile
for issue-makerOne more piece to add and that is this actions Dockerfile
. Once you complete this you will have all the pieces in place to use your final action!
Create and add the following contents to .github/actions/issue-maker/Dockerfile
:
You can use this link to easily create this file.
FROM node:slim
COPY package*.json ./
RUN npm install
COPY . .
CMD [ "node", "/src/index.js" ]
Commit the changes to this branch
Click the green Commit new file
button
I'll respond when you push changes to this pull request.
Let's trigger your new workflow!
Continue experimenting with this workflow for as long as you'd like.
Try adding another label and see if you get a new fact!
When you have finished experimenting, merge this pull request and meet me in this issue to finish wrapping things up.
Oh no... I found an error ⚠️
Error The title of this pull request isn't what I expected!
Solution Verify the name of your pull request is Use outputs and keep in mind that this is case-sensitive.
Follow these steps to rename your pull request:
I'll respond when I detect this pull request has been renamed.