Pre-requisites: GitHub Actions, Docker Hub
Publishing Docker images to Docker Hub using GitHub Actions is a simple and efficient process that can automate your image-building and publishing process.
What are Github Actions?
GitHub Actions is a CI/CD (Continuous Integration/Continuous Deployment) platform integrated with GitHub. It enables developers to automate software workflows, such as building, testing, and deploying code, directly from their GitHub repositories. With GitHub Actions, developers can write custom scripts, called “Actions,” that run on pre-configured virtual environments, known as “runners,” in response to specified events, such as pushes to a branch or pull requests. The Actions can be run on the GitHub platform, on the developer’s servers, or third-party cloud providers, providing a flexible and scalable CI/CD solution for software development. GitHub Actions is a paid feature, but free tiers are available for open-source projects. The following are required to set up GitHub actions.
- Please note you should already have a functioning application. The application can be written in any language.
- The Dockerfile of the application should be created already. The Dockerfile should be correct.
- You should have a GitHub account.
- You should have your docker hub account and docker hub repository.
- You should have the credentials that we use to login into Docker Hub.
- The Dockerfile along with the application should be present in the GitHub repository.
For this demonstration, we have added the source codes here.
In the demonstration, we considered a node application and created a Dockerfile for the same.
The Dockerfile that we have used for our application looks something like this:
If you do not have a docker hub repository you can create one by following these steps.
- Go to your docker hub account and select create. Add a name for the app e.g node-app
Step 1: Click on the Actions tab at the top of the GitHub repository.
Step 2: Click on the setup workflow yourself to create a new pipeline.
Step 3: Add the following code to the workflow.
Please note that the path to the repository should be changed before saving. The code rishaw00/node-app should be changed to <your-docker-username>/<your-repo-name> in the workflow.
Step 4: Before saving the workflow, Add Dockerhub credentials in the secrets:
Set up the GitHub Secrets: To authenticate with Docker Hub, you’ll need to provide your Docker Hub username and password. You can store them as secrets in GitHub. Go to the repository’s Settings > Secrets and add two secrets: `DOCKER_USERNAME` and `DOCKER_PASSWORD`.
Once this is done we can save the workflow.
Step 5: Trigger the Workflow:
Once you’ve created the workflow and added the secrets, you can trigger it by pushing code to the main branch of your repository.
Now, to check the workflow we can go to the actions tab and see the workflow in the process.
Step 6: Verify the Docker Image:
You can verify that the Docker image has been published to Docker Hub by logging into Docker Hub and checking the Tags section of your repository.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!