Saturday, April 6, 2024

GitHub Actions Pipeline to Create Docker Image and Push Docker Image into DockerHub | GitHub Actions Integration with DockerHub

 Please find steps for integrating DockerHub with GitHub Actions:


Implementation steps:

  • Create access token in DockerHub
  • Add access token, docker hub user name as secrets in GitHub Actions
  • Create GitHub Actions workflow yaml in your repo
  • Add tasks for Maven build, docker image creation, tagging and docker push 
  • Run the workflow/build in GitHub hosted runner(e.g. Ubuntu)
  • Verify docker image have been uploaded into DockerHub

Pre-requisites:

Steps below to create access token in DockerHub:

Click on new access token:
    Copy on Generate

 

Add Docker Hub user name and token as Secrets in GitHub Actions

Go to your GitHub Repo --> Settings --> 

Click on Secrets and Variables under Security in left nav 
Click new Repository Secret


Enter DOCKERHUB_USERNAME as secret name and 
Enter your docker hub user name


Enter DOCKERHUB_TOKEN as secret name and 
Enter your token as secret



Create GitHub Actions CICD workflow yaml:

Go to GitHub repo where your Java project is, create a new file:

.github/workflows/cicd.yml


The below file have four steps(tasks) 
    - Checkout
    - Install Java on runner
    - Build using Maven
    - Build docker image and tag it
    - Upload docker image into DockerHub

Copy below YAML code:

name: cicd-workflow to create docker image and upload into Dockerhub
on:
  push:
    branches: [ "master" ]
jobs:
  job1:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Set up JDK 17
      uses: actions/setup-java@v2
      with:
        distribution: 'adopt'
        java-version: '17'
    - name: Build with Maven
      run: mvn clean install
    - name: Login to Docker Hub
      uses: docker/login-action@v3
      with:
        username: ${{ secrets.DOCKERHUB_USERNAME }}
        password: ${{ secrets.DOCKERHUB_TOKEN }}
    - name: Build and push Docker image
      env:
        IMAGE_TAG: ${{ github.sha }}
      run: |
        docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/myspringbootapp:${IMAGE_TAG} .
        docker push ${{ secrets.DOCKERHUB_USERNAME }}/myspringbootapp:${IMAGE_TAG}


Commit the file.

As soon as you commit, build will run immediately in GitHub Actions. 
Now you can see the output of build in Actions tab.


Login to DockerHub to verify if docker image have been uploaded successfully.


Watch steps in YouTube channel:

No comments:

Post a Comment

DevOps Bootcamp Oct 2024 Schedule | DevOps & AWS Azure Cloud Coaching by Coach AK | DevOps and Cloud Computing Online Classes

  (Lot of new topics covered like GitHub Actions, Helm, Prometheus and Grafana..) The DevOps requirements in the IT market space is expected...