GitHub Actions Certification

Custom Actions

Using a Docker Action in Workflow

Learn how to integrate a reusable custom Docker container action across repositories. In this guide, we’ll invoke the docker-action-pr-giphy-comment action from the solar-system project to post a thank-you GIF on pull requests.

The image shows a GitHub repository page for "docker-action-pr-giphy-comment," displaying files and recent commits.

1. Open the Target Repository

Navigate to your solar-system repository on GitHub:

This image shows a GitHub repository page named "solar-system" with various files and folders listed, including workflows, images, and JavaScript files. The repository has no stars or forks and includes a recent pull request merge.

2. Add the Giphy API Token

Your custom action fetches GIFs using the Giphy API. To configure:

  1. Get an API key from the Giphy Developer Portal.
  2. In your repo, go to Settings > Actions > Secrets and variables > Actions.
  3. Click New repository secret.
  4. Name it GIPHY_API and paste the API key.
  5. Save.

The image shows a GitHub repository settings page focused on "Actions secrets and variables," displaying options for managing environment and repository secrets.

The image shows a GitHub repository settings page where a new secret is being added under "Actions secrets," with the name "GIPHY_API" and a secret key entered.

Note

Never expose your API keys in plain text. Always use GitHub Secrets for sensitive values.

3. Create the Workflow File

Checkout a branch (e.g., main or a feature branch) and add .github/workflows/pr-thank-you.yml:

The image shows a GitHub interface where a user is creating or editing a file named "pr-thank" in the ".github/workflows" directory of a repository. The file content area is currently empty.

Workflow Configuration

# .github/workflows/pr-thank-you.yml
name: PR Thank You Comment

on:
  pull_request:
    types: [opened]

jobs:
  pr-action:
    runs-on: ubuntu-latest
    permissions:
      issues: write
      pull-requests: write
    steps:
      - name: Post PR Comment with Giphy
        uses: siddharth-7/docker-action-pr-giphy-comment@main
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          giphy-api-key: ${{ secrets.GIPHY_API }}

Permissions Reference

PermissionPurpose
issues: writePost comments on issues
pull-requests: writeManage PR comments

Commit and push your branch, then open a pull request:

The image shows a GitHub interface for creating a pull request, with options to leave a comment and assign reviewers. The "Create pull request" button is highlighted at the bottom.

4. Observe the Workflow Run

Once the PR is created, GitHub Actions will trigger the workflow automatically:

The image shows a GitHub pull request page for creating a file named "pr-thank-you.yml" with details about the branch, checks, and merge status.

Monitor progress under the Actions tab:

The image shows a GitHub Actions page with a workflow named "pr-thank-you.yml" in progress. The sidebar lists options like Caches, Deployments, and Runners.

After success, the action posts a thank-you GIF comment on the pull request.

5. Behind the Scenes: Docker Build Logs

Each run of a container action rebuilds the Docker image. Example build output:

#1 [internal] load .dockerignore
#1 DONE 0.0s
#2 [internal] load build definition from Dockerfile
#2 DONE 0.0s
#3 [internal] load metadata for docker.io/library/alpine:3.10
#3 DONE 0.0s
#6 [1/4] FROM docker.io/library/alpine:3.10
#6 DONE 0.1s
#9 [4/4] RUN chmod +x /entrypoint.sh
#9 DONE 0.3s

And the execution command:

/usr/bin/docker run --name 2c046f464a14829b2e7791b519b_32bdb \
  --label 461ce --workdir /github/workspace -m -1 \
  --input_github_token="INPUT_GITHUB_TOKEN" \
  -e "GIPHY_API" -e "GITHUB_SHA"="GITHUB_SHA" \
  -v "/var/run/docker.sock":"/var/run/docker.sock" \
  --rm docker-action-pr-giphy-comment:main

Performance Tip

Rebuilding the Docker image each run adds latency. For faster CI workflows, consider authoring a JavaScript action that executes without a container build.

References

Watch Video

Watch video content

Previous
Using a Javascript Action in Workflow