Closed Cemberk closed 2 years ago
π‘All of the following steps take place inside of the .github/actions/cat-facts
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 fact
in in this portion of the course. There will be a later step that will rely on this actions output.
Create and add the following contents to the .github/actions/cat-facts/action.yml
file:
You can use this link to easily create this file.
name: "my cat fact action"
description: "Get external data with GitHub Actions"
outputs:
fact:
description: Resulting cat fact from the https://cat-fact.herokuapp.com/facts api
runs:
using: "docker"
image: "Dockerfile"
Commit the changes to the branch named action-two
I'll respond when you push changes to this pull request.
Cat Fact API
We will be using the cat-fact API for our action. This API does not require any authentication making it an ideal teaching tool.
When we make our request to this API we will get back an array JSON Objects in the response. An individual Object looks like this:
{
"_id": "58e008ad0aac31001185ed0c",
"text": "The frequency of a domestic cat;s purr is the same at which muscles and bones repair themselves.",
"type": "cat",
"user": {
"_id": "58e007480aac31001185ecef",
"name": {
"first": "Kasimir",
"last": "Schulz"
}
},
"upvotes": 6,
"userUpvoted": "None"
}
It contains many key:value pairs of data that we can use in our own program or service. In our case, we are only interested in the text
field.
Our action will extract the text
field in a new array, then select a random index to display in the console as our random cat fact.
To further demonstrate the flexibility of Docker actions we will create this one using Python.
If Python is a new programming language to you, like always don't worry. You are here to learn about actions and we will supply all the necessary source code for you.
Create and add the following contents to the .github/actions/cat-facts/src/main.py
file:
You can use this link to easily create this file.
import requests
import random
import sys
# Make an HTTP GET request to the cat-fact API
cat_url = "https://cat-fact.herokuapp.com/facts"
r = requests.get(cat_url)
r_obj_list = r.json()
# Create an empty list to store individual facts in
# This will make it easy to select a random one later
fact_list = []
# Add the "text" of every object into the fact_list list
for fact in r_obj_list:
fact_list.append(fact["text"])
# Select a random fact from the fact_list and return it
# into a variable named random_fact so we can use it
def select_random_fact(fact_arr):
return fact_arr[random.randint(0, len(fact_list)+1)]
random_fact = select_random_fact(fact_list)
# Print the individual randomly returned cat-fact
print(random_fact)
# Set the fact-output of the action as the value of random_fact
print(f"::set-output name=fact::{random_fact}")
Commit the changes to this branch:
I'll respond when you push changes to this pull request.
requirements.txt
with your Python dependenciesπ‘All of the following steps take place inside of the .github/actions/cat-facts
directory.
A requirements.txt
file is required so that the Python package manger, PIP, knows which dependancies to install when our Docker image get's built. In our case we will only need to add the requests
package to our requirements.txt
Create and add the following contents to the .github/actions/cat-facts/requirements.txt
file:
You can use this link to easily create this file.
requests
Commit the changes to this branch.
πLearn PIP
I'll respond when you push changes to this pull request.
Dockerfile
Awesome π
This action now has three of the four key files it needs to run:
Dockerfile
for the cat-fat actionLastly we will create the Dockerfile
, just like we did with our first action.
Create and add the following contents to the .github/actions/cat-facts/Dockerfile
file:
You can use this link to easily create this file.
FROM python:3
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD [ "python", "/src/main.py" ]
Commit the changes to this branch
Click the green Commit new file
button
I'll respond when you push changes to this pull request.
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 cat fact.
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 cat fact 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 cat fact action
Edit your current workflow file. It should have the following contents:
name: Docker 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: meow
uses: ./.github/actions/cat-facts
Commit the changes to this branch
I'll respond when you push changes to this pull request.
Great job! Everything is all set up and now we are ready to start learning about cats π. You will find you have some cat fact 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-cat-fact
label to this pull requestsecond-cat-fact
label to this pull requestFeel free to continue adding labels to this pull request if you want to see more facts.
When you are ready to move forward in the lesson merge this pull request into the main
branch. I will respond when you've merged this pull request.
Congratulations Cemberk you have officially written two GitHub Docker based 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.
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 External APIs 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.