Closed geored closed 3 years ago
Just like the first action you wrote, you are going to need to setup a few directories and files.
Now that you have all the necessary tools installed locally, follow these steps to ensure your environment is configured and ready for actions.
.github/
directory.master
branch
git checkout master
git pull
action-two
branch you created for this pull request.
git checkout action-two
.github/actions/joke-action
.
mkdir actions/joke-action
joke-action
folder you just created:
cd actions/joke-action
npm init -y
npm
:
npm install --save request request-promise @actions/core
git add .
git commit -m 'add joke action dependencies'
git push
I will respond once you push a new commit to the action-two
branch.
💡All of the following steps take place inside of the .github/actions/joke-action
directory.
Our action does not require much metadata for it to run correctly. We will not be accepting any inputs, we will however be setting a single output this time.
We will not use the joke-output
in in this portion of the course. There will be a later step that will rely on this actions output.
Create the action metadata file .github/actions/joke-action/action.yml
with the following content:
name: "external API action"
description: "use an external API to retrieve and display a joke"
outputs:
joke-output:
description: The resulting joke from the icanhazdadjokes API
runs:
using: "node12"
main: "main.js"
action.yml
filegit add action.yml
git commit -m 'add metadata for the joke action'
git push
I'll respond when you push to this branch.
Yes... files... plural. As you probably know, in JavaScript and other programming languages it is common to break your code into modules so that it is easier to read and maintain going forward. Since JavaScript actions are just programs written in JavaScript that run based on a specific trigger we are able to make our action code modular as well.
To do so we will create two files. One of them will contain the logic to reach out to an external API and retrieve a joke for us, the other will call that module and print the joke to the actions console for us. We will be extending this functionality in our third and final action.
Joke API
The first file will be joke.js
and it will fetch our joke for us. We will be using the icanhazdadjoke API for our action. This API does not require any authentication, but it does however that we set a few parameters in the HTTP headers. I'll point out what those are when we get to the code, however it is outside of the scope of this course to cover HTTP in any depth.
When we make our request to this API we will get back a JSON Object in the response. That Object looks like this:
{
id: '0LuXvkq4Muc',
joke: "I knew I shouldn't steal a mixer from work, but it was a whisk I was willing to take.",
status: 200
}
It contains 3 key:value pairs of data that we can use in our own program or service. In our case, we are only interested in the joke
field.
Joke Module
We will create a file named joke.js
and it will reside in the .github/action/joke-action
directory.
The joke module will look like this:
const request = require("request-promise");
const options = {
method: "GET",
uri: "https://icanhazdadjoke.com/",
headers: {
Accept: "application/json",
"User-Agent":
"Writing JavaScript action GitHub Learning Lab course. Visit lab.github.com or to contact us."
},
json: true
};
async function getJoke() {
const res = await request(options);
return res.joke;
}
module.exports = getJoke;
joke.js
source code?Main Module
We will also create a file named main.js
that resides inside of the .github/actions/joke-action
directory.
That file will look like this:
const getJoke = require("./joke");
const core = require("@actions/core");
async function run() {
const joke = await getJoke();
console.log(joke);
core.setOutput("joke-output", joke);
}
run();
main.js
source code?Create and add the following contents to the .github/actions/joke-action/joke.js
file:
const request = require("request-promise");
const options = {
method: "GET",
uri: "https://icanhazdadjoke.com/",
headers: {
Accept: "application/json",
"User-Agent":
"Writing JavaScript action GitHub Learning Lab course. Visit lab.github.com or to contact us."
},
json: true
};
async function getJoke() {
const res = await request(options);
return res.joke;
}
module.exports = getJoke;
Save the joke.js
file.
Create and add the following contents to the .github/actions/joke-action/main.js
file:
const getJoke = require("./joke");
const core = require("@actions/core");
async function run() {
const joke = await getJoke();
console.log(joke);
core.setOutput("joke-output", joke);
}
run();
Save the main.js
file.
Commit the changes to this branch and push them to GitHub:
git add joke.js main.js
git commit -m 'creating joke.js and main.js'
git push
I'll respond in this pull request when you have pushed to this branch.
At this point we can't expect much from our workflow, if you remember all of its contents are commented out. Let's go ahead and fix that now so that we can see our action fetch us a joke.
We are going to add a new trigger to our workflow to make it easier for us to trigger it without the need to push changes to the repository. Remember that every time our workflow runs this action we should see a new joke which means we need a good way to make it run a few times. If you recall there are many events that trigger a workflow run.
We will use the pull_request
event and specify the activity type to be when an issue get's labeled
. This will allow us to trigger our workflow by simply placing a label on this pull request.
Let's change the tigger and add the joke action
Edit your current workflow file to contain the following:
name: JS Actions
on:
pull_request:
types: [labeled]
jobs:
action:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: hello-action
uses: ./.github/actions/hello-world
- name: ha-ha
uses: ./.github/actions/joke-action
action-two
branchI'll respond once you commit your changes
Great job! Everything is all set up and now we are ready to start laughing 🤣. You will find you have some joke related labels available to you in this repository. You don't have to use them, any label will trigger our workflow, but it might be easier to follow along with me if you use the labels I suggest.
first-joke
label to this pull requestsecond-joke
label to this pull requestFeel free to continue adding labels to this pull request if you want to see more jokes.
When you are ready to move forward in the lesson merge this pull request into the master
branch.
Congratulations geored you have officially written two GitHub JavaScript actions!!!
Next, you'll write your final action of this course, so let's head over to the new issue I've opened to continue.
External APIs PR's