Step-by-step instructions for using Docker with GitHub Actions
Step 1: Create the repository.
Step 2: Here is my repository maven web app.
Here is the code i used and kept in the workflow file.
name: Java CI with Maven, Docker, and Cloud Run
on:
workflow_dispatch:
# push:
# branches: [ master ]
# pull_request:
# branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v4
with:
java-version: '11'
distribution: 'adopt'
cache: maven
- name: Build with Maven
run: mvn -B package --file pom.xml
- name: Archive Artifact
uses: actions/upload-artifact@v2
with:
name: maven-web-application
path: /home/runner/work/maven-web-application/maven-web-application/target/maven-web-application.war
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push Docker image to DockerHub
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
push: true
tags: larasai/maven-web-app:v1
- name: install the gcloud cli
uses: google-github-actions/setup-gcloud@v0
with:
project_id: ${{ secrets.GCLOUD_PROJECT_ID }}
service_account_key: ${{ secrets.GCLOUD_SA_KEY }}
export_default_credentials: true
- name: Build and push Docker image to Google Artifact Registry
run: |
gcloud auth configure-docker us-central1-docker.pkg.dev
docker pull larasai/maven-web-app:v1
docker tag larasai/maven-web-app:v1 us-central1-docker.pkg.dev/stone-trees-422304-p7/demo-app/maven-web-app:v1
docker push us-central1-docker.pkg.dev/stone-trees-422304-p7/demo-app/maven-web-app:v1
- name: Deploy to Cloud Run
uses: google-github-actions/deploy-cloudrun@v2
with:
service: maven-web-app
image: us-central1-docker.pkg.dev/stone-trees-422304-p7/demo-app/maven-web-app:v1
platform: managed
region: us-central1
project_id: ${{ secrets.GCLOUD_PROJECT_ID }}
service_account_key: ${{ secrets.GCLOUD_SA_KEY }}
flags: '--allow-unauthenticated'
Here is the detail explanation about the code.
- on:
- This section defines when the workflow should run. In this case, it’s configured to run manually (
workflow_dispatch
), meaning someone needs to trigger it explicitly. The commented-out lines indicate configurations for running the workflow automatically on specific events like pushes to the master branch or pull requests.
- This section defines when the workflow should run. In this case, it’s configured to run manually (
- jobs:
- This workflow contains one job named “build”, which defines a series of steps to execute.
- runs-on:
- Specifies the type of virtual machine to run the job on. In this case, it’s set to
ubuntu-latest
.
- Specifies the type of virtual machine to run the job on. In this case, it’s set to
- steps:
- This section contains the individual tasks to be executed as part of the job.
- actions/checkout@v2:
- Checks out the code repository into the runner’s workspace.
- actions/setup-java@v4:
- Sets up Java 11 for the job using AdoptOpenJDK, and configures Maven caching for faster builds.
- Build with Maven:
- Runs the Maven command to build the Java web application using the specified
pom.xml
file.
- Runs the Maven command to build the Java web application using the specified
- Archive Artifact:
- Uploads the built artifact (in this case, a
.war
file) to GitHub as an artifact, making it available for download in subsequent steps or workflows.
- Uploads the built artifact (in this case, a
- docker/setup-buildx-action@v1:
- Sets up Docker Buildx, a Docker CLI plugin for extended build capabilities.
- docker/login-action@v2:
- Logs in to DockerHub using provided credentials stored in GitHub secrets.
- docker/build-push-action@v2:
- Builds the Docker image using the specified Dockerfile, tags it with a version (
larasai/maven-web-app:v1
), and pushes it to DockerHub.
- Builds the Docker image using the specified Dockerfile, tags it with a version (
- google-github-actions/setup-gcloud@v0:
- Sets up the Google Cloud SDK (gcloud) CLI for interacting with Google Cloud services.
- Build and push Docker image to Google Artifact Registry:
- Configures Docker to authenticate with Google Artifact Registry, pulls the Docker image from DockerHub, retags it, and pushes it to Google Artifact Registry.
- Deploy to Cloud Run:
- Deploys the Docker image to Google Cloud Run, a managed serverless platform, using the specified image from Google Artifact Registry. It configures the deployment to be accessible without authentication, using the provided Google Cloud Project ID and service account key.
Step 3: CLick on actions and click on the java ci with maven on left sisde and trigger the pipeline.
Step 4: The process was successfully executed here is the build.
Step 5: Refer the below image and the all steps was completed successfully.
Step 6: Here the image was successfully build and pushed to the docker hub refer the below image.
Step 7: We have stored the all secrets in the repository secrets.
Step 8: The docker build image was successfully pushed in to the google cloud artifact repository refer the below image. In service account we need to provide the sufficient permissions while pushing the image inro the artifact repository.
Step 9: The application was successfully deployed into the cloudrun service for your reference refer the below image.
Step 10: The revision was successfully created, here are the revision details and that is serving the traffic successfully.
Step 11: Here is the application tomcat web page.
How do I use Docker with GitHub Actions?
Docker packages all the components required for software into containers, much like a recipe box. By automating chores in your development workflow, GitHub Actions serves as your personal chef. When combined, they optimize software development by effectively packaging applications and smoothly automating monotonous operations. While GitHub Actions automates tasks like testing and deployment, Docker guarantees consistency in application deployment. Docker integration with GitHub Actions produces an effective workflow for easily developing, testing, and deploying apps. This combination makes it easier for teams to produce high-quality software quickly and improves communication across the software development lifecycle.