darkknight1947 / write-docker-actions

https://lab.github.com/githubtraining/github-actions:-write-docker-container-actions
MIT License
0 stars 0 forks source link

External APIs #6

Closed darkknight1947 closed 3 years ago

github-learning-lab[bot] commented 3 years ago

Add metadata to the cat-fats action

:keyboard: Activity: Create the cat-facts metadata file

πŸ’‘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.

  1. 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"
  2. Commit the changes to the branch named action-two


I'll respond when you push changes to this pull request.

github-learning-lab[bot] commented 3 years ago

Fetch a cat fact

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.

:keyboard: Activity: Create the Python source code for the cat-fats action

  1. 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}")
  2. Commit the changes to this branch:

πŸ“– Learn Python programming


I'll respond when you push changes to this pull request.

github-learning-lab[bot] commented 3 years ago

Add Python dependencies

:keyboard: Activity: Create 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

  1. 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
  2. Commit the changes to this branch.

πŸ“–Learn PIP


I'll respond when you push changes to this pull request.

github-learning-lab[bot] commented 3 years ago

Add the cat-fact Dockerfile

Awesome πŸŽ‰

This action now has three of the four key files it needs to run:

:keyboard: Activity: Create Dockerfile for the cat-fat action

Lastly we will create the Dockerfile, just like we did with our first action.

  1. 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" ]
  2. Commit the changes to this branch

  3. Click the green Commit new file button

πŸ“– Become a Dockerfile guru


I'll respond when you push changes to this pull request.

github-learning-lab[bot] commented 3 years ago

Use the cat-fat action

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.

:keyboard: Activity: Restore the workflow file

Let's change the tigger and add the cat fact action

  1. 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
  2. Commit the changes to this branch


I'll respond when you push changes to this pull request.

github-learning-lab[bot] commented 3 years ago

darkknight1947 get ready to purr

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.

:keyboard: Trigger a cat fact

  1. Apply the first-cat-fact label to this pull request
  2. Wait a few seconds and then apply the second-cat-fact label to this pull request
  3. Check the workflow results on the Actions tab

Feel 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.

github-learning-lab[bot] commented 3 years ago

Congrats on your second action πŸŽ‰

Congratulations darkknight1947 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.