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
- Giphy REST API
- GitHub REST API for Comments
- Creating the Repository
- Configuring GitHub Secrets
- Project Structure Overview
- Dockerfile Configuration
- Entrypoint Script
- Action Metadata (
action.yml
) - Test Workflow Setup
- Opening a Pull Request
- Verifying the Workflow
- Reviewing the Bot Comment
- 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.
HTTP Method | Endpoint | Purpose |
---|---|---|
POST | https://api.github.com/repos/{owner}/{repo}/issues/{issue_number}/comments | Create 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
:
After creation, you’ll see the initial commit:
4. Configuring GitHub Secrets
Store sensitive information under Settings > Secrets and variables > Actions:
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:
File | Purpose |
---|---|
Dockerfile | Defines the container environment |
entrypoint.sh | Handles the action logic |
action.yml | Action metadata and inputs |
.github/workflows/test.yml | Workflow 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:
- Reading the PR number from the GitHub event payload
- Fetching a random “thank you” GIF
- Parsing the GIF URL
- 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\"}" \
"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:
Create and submit the PR:
11. Verifying the Workflow
Monitor the Action run under Actions. A successful run indicates:
View detailed logs for build and script outputs:
12. Reviewing the Bot Comment
Your PR will now contain an automated comment with a thank-you message and a Giphy GIF. 🎉
13. Links and References
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