GitHub Actions Certification

Custom Actions

Create a Docker Action

In this tutorial, learn how to develop a custom Docker-based GitHub Action that automatically posts a random “Thank You” GIF from Giphy whenever someone opens a pull request. This CI/CD integration covers:

  • Triggering on pull_request events
  • Fetching a random GIF via the Giphy REST API
  • Commenting on the pull request with the GitHub REST API

We’ll securely store API keys in GitHub Secrets and encapsulate the entire implementation in a Docker container.

Table of Contents

  1. Giphy REST API
  2. GitHub REST API for Comments
  3. Creating the Repository
  4. Configuring GitHub Secrets
  5. Project Structure Overview
  6. Dockerfile Configuration
  7. Entrypoint Script
  8. Action Metadata (action.yml)
  9. Test Workflow Setup
  10. Opening a Pull Request
  11. Verifying the Workflow
  12. Reviewing the Bot Comment
  13. Links and References

1. Giphy REST API

Use the Giphy Random GIF endpoint to retrieve a random “thank you” GIF. Store your GIPHY API key in GitHub Secrets and reference it in the action:

GET https://api.giphy.com/v1/gifs/random?api_key=<GIPHY_API_KEY>&tag=thank%20you&rating=g

A sample JSON response:

{
  "data": {
    "images": {
      "original": {
        "url": "https://media1.giphy.com/media/l119IDMNbVsKgyf5u/giphy.gif"
      },
      "downsized": {
        "url": "https://media1.giphy.com/media/l119IDMNbVsKgyf5u/200w_d.gif"
      }
    }
  }
}

Extract the GIF URL from .data.images.downsized.url using jq.


2. GitHub REST API for Comments

To comment on a PR, call GitHub’s Create an issue comment endpoint. Issues and pull request comments use the same API.

The image shows a GitHub documentation page for the REST API, specifically focusing on managing issues, with links to various related actions like listing, creating, and updating issues.

HTTP MethodEndpointPurpose
POSThttps://api.github.com/repos/{owner}/{repo}/issues/{issue_number}/commentsCreate a PR comment

Example curl command:

curl -s -X POST \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer <YOUR-TOKEN>" \
  https://api.github.com/repos/OWNER/REPO/issues/ISSUE_NUMBER/comments \
  -d '{"body":"Thank you for this contribution! 🎉"}'

3. Creating the Repository

Initialize a new public repository named docker-action-pr-giphy-comment with a README.md:

The image shows a GitHub page for creating a new repository, with fields for the repository name, description, and options for public or private settings.

After creation, you’ll see the initial commit:

The image shows a GitHub repository page titled "docker-action-pr-giphy-comment" with an initial commit and a README file. The repository has no stars, forks, or releases.


4. Configuring GitHub Secrets

Store sensitive information under Settings > Secrets and variables > Actions:

The image shows a GitHub repository settings page where a new secret is being added under "Actions secrets." The secret is named "GIPHY_API_KEY."

Note

You will add:

  • GIPHY_API_KEY for the Giphy API
  • Use the built-in GITHUB_TOKEN for commenting on PRs

5. Project Structure Overview

Your repository should contain the following files:

The image shows a code editor with a project directory open, displaying files like `action.yml`, `Dockerfile`, and `entrypoint.sh` in a GitHub repository. The `README.md` file is open in the editor pane.

FilePurpose
DockerfileDefines the container environment
entrypoint.shHandles the action logic
action.ymlAction metadata and inputs
.github/workflows/test.ymlWorkflow to test the action on PRs

6. Dockerfile Configuration

Create a lightweight container with required utilities:

FROM alpine:3.10

# Install HTTP client and JSON parser
RUN apk update && apk add --no-cache curl jq

# Copy and set entrypoint script permissions
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]

7. Entrypoint Script

The entrypoint.sh script orchestrates:

  1. Reading the PR number from the GitHub event payload
  2. Fetching a random “thank you” GIF
  3. Parsing the GIF URL
  4. Posting a comment on the PR
#!/bin/sh

GITHUB_TOKEN=$1
GIPHY_API_KEY=$2

# Get PR number
pr_number=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")
echo "PR Number: $pr_number"

# Fetch GIF from Giphy
giphy_response=$(curl -s \
  "https://api.giphy.com/v1/gifs/random?api_key=$GIPHY_API_KEY&tag=thank%20you&rating=g")
echo "Giphy Response: $giphy_response"

# Extract GIF URL
gif_url=$(echo "$giphy_response" | jq --raw-output .data.images.downsized.url)
echo "GIF URL: $gif_url"

# Post comment
response=$(curl -s -X POST \
  -H "Authorization: token $GITHUB_TOKEN" \
  -H "Accept: application/vnd.github.v3+json" \
  -d "{\"body\": \"Thank you for your contribution! 🎉\n![GIF]($gif_url)\"}" \
  "https://api.github.com/repos/$GITHUB_REPOSITORY/issues/$pr_number/comments")
echo "Comment posted: $(echo "$response" | jq --raw-output .html_url)"

8. Action Metadata (action.yml)

Define inputs and Docker run settings:

name: 'Giphy PR Comment'
description: 'Automatically add a thank-you Giphy GIF to pull requests.'
inputs:
  github-token:
    description: 'Token for GitHub API authentication'
    required: true
  giphy-api-key:
    description: 'Secret key for Giphy API'
    required: true
runs:
  using: 'docker'
  image: 'Dockerfile'
  args:
    - ${{ inputs.github-token }}
    - ${{ inputs.giphy-api-key }}

9. Test Workflow Setup

Configure .github/workflows/test.yml to trigger on PR opens:

on:
  pull_request:
    types: [opened]

jobs:
  test-giphy-action:
    runs-on: ubuntu-latest
    permissions:
      issues: write
      pull-requests: write

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Invoke Giphy PR Comment Action
        uses: ./
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          giphy-api-key: ${{ secrets.GIPHY_API_KEY }}

10. Opening a Pull Request

Make a small update (e.g., edit README.md) in a feature branch to trigger the workflow:

The image shows a GitHub repository interface where a user is editing the `README.md` file in a project named "docker-action-pr-giphy-comment."

Create and submit the PR:

The image shows a GitHub pull request page for updating a README.md file. It indicates that some checks are pending, and the branch has no conflicts with the base branch.


11. Verifying the Workflow

Monitor the Action run under Actions. A successful run indicates:

The image shows a GitHub Actions interface with a successful workflow run named "testing-action," detailing steps like setting up a job, checking out a repository, and posting a PR comment.

View detailed logs for build and script outputs:

The image shows a GitHub Actions interface with a successful workflow run for "testing-action," displaying job setup details and steps like "Checkout Repository" and "Post PR Comment."


12. Reviewing the Bot Comment

Your PR will now contain an automated comment with a thank-you message and a Giphy GIF. 🎉


Congratulations! You have built a reusable Docker Action that integrates external APIs to enhance your pull request workflow. Consider publishing this action to the GitHub Marketplace next.

Watch Video

Watch video content

Previous
Using a Composite Action in Workflow