Converting CLI tool to Github Action

In this article, I will share my journey of creating Docker container action for one of the CLI tools I use.

What is “Github Actions”?

GitHub Actions allows you to build end-to-end continuous integration (CI) and continuous deployment (CD) capabilities directly in your repository. If you are not familiar with Github Actions, you can read more about it here.

Here are the steps to build and publish Docker container action:

Step 1: Learning the CLI tool

First of all, we need to decide the tool which we want to create Github Action for and research what kind of setup we need in order to run it in our Docker image. In my case, using openjdk:jre-slim was enough to run detekt.

In this step, one suggestion is that we should try to find lightweight image so that docker pull is faster.

So, let’s create file called Dockerfile in our repository and add following:

FROM openjdk:jre-slim

Step 2: Download the CLI tool to the Docker image

In this step, we need to download the tool to our Docker image. For this, we will use ADD command.

The ADD command requires a source and a destination:

ADD source destination

Now we can add the CLI tool to the docker image:

ADD https://github.com/detekt/detekt/releases/download/v1.10.0-RC1/detekt /usr/local/bin/detekt

After downloading, we need make our file executable:

...
RUN chmod +x /usr/local/bin/detekt

Step 3: Adding entry point to the Docker image

Now we need to finalize the Docker image by adding ENTRYPOINT:

...

RUN cd $GITHUB_WORKSPACE

ENTRYPOINT ["detekt"]

Notice that, I added cd $GITHUB_WORKSPACE as well. GITHUB_WORKSPACE is an environment variable which tells us working directory so that we can run script in correct directory.

Now we finalized the Dockerfile changes, we can move to next steps. You can find full Dockerfile here.

Step 4: Publishing

As we finalized the Dockerfile, we can publish our action. For this, we need to define action.yml and add name, description, author and other information. You can check action.yml I created here. After you add action.yml, you need to create README.md which gives some information about you action, usage samples, etc. Following step is that after we push our changes to the repository, on GitHub, we need to navigate to the main page of the repository and draft a new release. You can read more here.

🎉Congratulations!🎉

If you followed the steps correctly, the action you created should be published on GitHub Marketplace. You can find the sample repository here.

  YouTube: Coding with Natig

Share on: