GitHub Actions @ work

Aman Saeed
4 min readApr 6, 2021
source

Being a collaborative programmer, have you ever been in a situation where you were stuck with reviewing several pull requests in GitHub or you wanted to quickly trigger a build or deployment post making some changes to your code? Well then, GitHub Actions is for you.

GitHub Actions was introduced in the late 2018. Since it’s launch, it has gained a lot of popularity in the community. It was launched with an intent to provide a new way for developers to automate the software development workflows directly from their GitHub repository, though it is not limited to that.

For any event within your code repository, you can set-up an action. And this can be done through a simple YAML file that resides inside the .github/workflows folder of the repository.

An event triggers an action

So, let’s get started.

  • For the demonstration, we would be trying to build a docker image using the docker file and push it to a docker repository, every time there is a Push or a Pull event on the main branch, in my repository. You can create a Docker Hub account, in case you don’t have.
  • Assuming that you have created a code repository on GitHub, add two files, index.html and Dockerfile to the repository.
  • Navigate to the Actions tab and select a desired template as per your workflow automation requirement.
Actions Tab under the GitHub repository
  • GitHub makes it really simple, by providing an initial Actions template to do this. Select the template named, Docker Image
  • Update the the default template YML file with below content.
  • Various tags in the above file:
Name: User-defined action name
on: Specifies the events on which the job is triggered.
runs-on: Specifies the virtual environment(linux/windows/mac) on which the job runs.
uses: Specifies an action to run as part of a step in your job.
Note: Update the value for jobs.steps.name[2].with.tags with your respective docker hub account name
  • Inside the code repository, navigate to Settings -> Secrets -> New repository secret, and create repo secrets for username & password respectively. This saves the credentials in encrypted format.
Secrets for Username And Password
Secrets Tab for adding Username and Password
  • Now on any Push or Pull event in this repo, the above GitHub Actions would get triggered automatically.
GitHub Actions triggered after a Push Event
  • The workflow executes on a new ubuntu virtual environment managed by GitHub, builds the docker image, myfirstimage:1.0.0 and pushes the same to your docker hub repository.
GitHub Actions executed successfully
  • That’s it!!!

It’s time to verify the workflow execution

To verify the continuous deployment workflow, login to your Docker Hub account and you can find the newly pushed docker image, myfirstimage:1.0.0.

DockerHub Repository

Pull the above docker image and run it as a docker container using the below command.

docker run -d -p <desired-port>:80 <your-dockerhub-username>/myfirstimage:1.0.0
Static website running in a docker container

And here is our static HTML page…!!!

You can download this project from GitHub :)

I hope this gives you a fair bit of idea on what GitHub Actions is and lets you get started with setting up your own workflows. If you have liked this article or it did help you, feel free to share and/or leave a comment…

Happy learning!!!

--

--