Create Own GitHub Action

Arnob
3 min readDec 20, 2022

Here I told you how you create an action package and publish that to the marketplace.

do action docker

What is GitHub Action

GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline.

I hope it will help you understand action well

How to create a GitHub Action!

At the starting, you need to create an action.yaml file. And write there the details of the action

action.yaml

name: 'DigitalOcean kubectl Action'
author: Arnob Almazee
description: DigitalOcean kubectl image Update by using Github Action
branding:
color: 'blue'
icon: 'command'
runs:
using: 'docker'
image: 'docker://ghcr.io/arn-ob/do-kubectl-action:latest'
inputs:
do_access_token:
description: Access token for accessing the doctl.
required: true
do_cluster_certificate:
description: Cluster certificate for accessing the kubectl.
required: true
do_deployment_name:
description: App deployment name.
required: true
do_container_name:
description: App container name.
required: true
do_image_tag:
description: App build image and tag.
required: true
stdin:
description: File to read and pass as stdin to kubectl
required: false
args:
description: The arguments that you want to pass through to the kubectl command
required: true
outputs:
kubectl-out:
description: The output of the kubectl command

This is the action config file. Here the action knows what you wanted to do and which value will get input and output. Now you have to give the Dockerfile which is initing run

FROM alpine:latest

LABEL maintainer="Arnob Almazee <github.com@arn-ob>"

RUN apk add --no-cache curl

RUN wget https://github.com/digitalocean/doctl/releases/download/v1.84.0/doctl-1.84.0-linux-amd64.tar.gz

RUN tar -xf doctl-1.84.0-linux-amd64.tar.gz -C ~/

RUN chmod +x ~/doctl

RUN mv ~/doctl /usr/local/bin

RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/` curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl

RUN chmod +x ./kubectl

RUN mv ./kubectl /usr/local/bin/kubectl

ADD entrypoint.sh /entrypoint.sh

RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]

This Dockerfile describes the process and the steps which I need to run the entrypoint.sh.

Short Details:

1st Dockerfile run from alpine:latest image. it will wget the doctl release then extract and save it in the bin file. Then install kubectl also it save to the bin. then make kubectl and doctl exec files.

After Dockerfile build then it runs entrypoint.sh

#!/bin/sh

debug() {
if [ "${ACTIONS_RUNNER_DEBUG:-}" = "true" ]; then
echo "DEBUG: :: $*" >&2
fi
}

echo "DigitalOcean version"

doctl version

echo "DO Token Init"

doctl auth init -t "${INPUT_DO_ACCESS_TOKEN}"

echo "DigitalOcean add the cluster kubeconfig"

doctl kubernetes cluster kubeconfig save ${INPUT_DO_CLUSTER_CERTIFICATE}

echo "Kubectl deployment"

kubectl set image deployment/${INPUT_DO_DEPLOYMENT_NAME} ${INPUT_DO_CONTAINER_NAME}=${INPUT_DO_IMAGE_TAG}

This entrypoint.sh file work is the main process of GitHub action. It checks the doctl version then it gets the token, then saves the cluster certificate number. Then deployment the image from the deployment name, container name, and image tag.

Those are found at Repository secrets (Repo Setting > Secrets > Action)

So the GitHub workflow YAML for that repository is like

name: Production Hattimatim

on:
push:
branches:
- main

jobs:
Deploy:
name: Hattimatim FrontEnd Deploy Process

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Get brandh tag
id: vars
shell: bash
run: |
echo "::set-output name=tag::${GITHUB_REF#refs/heads/}-$(git rev-parse --short HEAD)"

- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
username: ${{ secrets.GIT_USERNAME }}
password: ${{ secrets.GIT_PASSWORD }}

- name: Build and push image to Docker Hub
run: |
docker build . -t arnobdev/hattimatim-frontend:${{ steps.vars.outputs.tag }}
docker push arnobdev/hattimatim-frontend:${{ steps.vars.outputs.tag }}

- name: Push the build image to the DO kubectl
uses: arn-ob/do-kubectl-action@main
with:
do_access_token: ${{ secrets.DO_ACCESS_TOKEN }}
do_cluster_certificate: ${{ secrets.DO_CLUSTER_CERTIFICATE }}
do_deployment_name: hattimatim-frontend-app
do_container_name: hattimatim-frontend-app
do_image_tag: arnobdev/hattimatim-frontend:${{ steps.vars.outputs.tag }}

Short Details:

Get the new commit tag, then it login into the docker hub. Then build the image from the repository. When the image ready then the image push to the kubectl by using arn-ob/do-kubectl-action.

Thank you.

Happy Learning…

--

--