DEV Community

Cover image for Creating a GitHub Action for a Docker Image
blugreenspace
blugreenspace

Posted on • Originally published at Medium

2 1

Creating a GitHub Action for a Docker Image

What are GitHub Actions anyway?

Actions are one of the the smallest executable steps of a job, or you can say a job is composed of steps which can be actions or commands. You can create GitHub Actions based on docker images. More details.

Creating a Github Action for an existing Dockerfile

Mainly three files are required, Dockerfile, entrypoint.sh, and actions.yml.

Let's start with creating a Dockerfile for our action, I'll use my Android CI Dockerfile. It's a simple Dockerfile, which includes all Android build-related tools, recent SDKs, Java, etc.

The Dockerfile for action uses the existing Docker image and extends it to include entrypoint script. The idea for the script is basically to pass the input from a step in workflow to runtime of the Docker in the Github environment.

Dockerfile

# Container image that runs your code
FROM code0987/android-ci:latest
# Copy your code to the filesystem path `/` of the container
COPY entrypoint.sh /
# Grant executable permission to entrypoint file
RUN ["chmod", "+x", "/entrypoint.sh"]
# Code file to execute when the docker container starts up (`entrypoint.sh`)
ENTRYPOINT ["/entrypoint.sh"]
Enter fullscreen mode Exit fullscreen mode

entrypoint.sh

This script runs all the input passed from GitHub Workflow, as a set of commands in non-interactive sh spawned inside Docker runtime.

#!/bin/sh
set -eu
sh -c "$*"
Enter fullscreen mode Exit fullscreen mode

Now let's define the action metadata so that GitHub can recognize this action. The specifics for Dockerfile based actions are, defining -

runs:
 using: 'docker'
 image: 'Dockerfile'
 args: 
Enter fullscreen mode Exit fullscreen mode

actions.yml

name: 'Hello World'
description: 'Greet someone and record the time'
runs:
  using: 'docker'
  image: 'Dockerfile'
Enter fullscreen mode Exit fullscreen mode

Using above action in a GitHub Workflow

name: Android CI
on: [push]
jobs:
  android-ci:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - name: "Android CI Github Action"
      uses: code0987/android-ci-github-action@master
      with:
        args: |
          chmod 755 gradlew 
          gradlew check
Enter fullscreen mode Exit fullscreen mode

Example


References

GitHub repo for action used as example, android-ci-github-action.

https://help.github.com/en/actions/building-actions/creating-a-docker-container-action

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay