This project is a sample Spring Boot application that demonstrates a complete CI/CD pipeline, from building a Docker image to deploying it on Google Kubernetes Engine (GKE). It includes a multi-stage Dockerfile for an optimized image and GitHub Actions for automated build, push, and deployment.
pom.xml
: A Maven configuration file for a Spring Boot application. It includes dependencies forspring-boot-starter-web
for creating a REST API andspring-boot-starter-actuator
for production-ready features like health checks. It's configured to use Java 17.Dockerfile
: A multi-stage Dockerfile that first builds the application into a JAR file using a Maven base image and then creates a final, lightweight image with only the JRE to run the application..github/workflows/docker-build.yml
: A GitHub Actions workflow that automates the following steps:- Checks out the code.
- Builds the Docker image from the
Dockerfile
. - Pushes the image to GitHub Container Registry (GHCR).
- Authenticates with Google Cloud using a Workload Identity Provider.
- Obtains GKE cluster credentials.
- Deploys the application to the GKE cluster by applying the
deployment.yml
andservice.yml
manifests.
k8s/deployment.yml
: A Kubernetes Deployment manifest that defines the desired state for your application's pods. It configures the container to use the image from GHCR, sets up port mapping, and includes liveness and readiness probes that check the/actuator/health
endpoint to ensure the application is running and ready to serve traffic.k8s/service.yml
: A Kubernetes Service manifest that exposes the application to external traffic. It uses aLoadBalancer
type to provision a public IP address, routing traffic from port80
to the application's container port8080
.
- Java 17: Required to build and run the application locally.
- Maven: Used for dependency management and building the project.
- Docker: For building and running the Docker image locally.
- A GitHub repository: To host the project and enable the GitHub Actions workflow.
- A Google Cloud project: With a GKE cluster configured for the CI/CD pipeline.
- GitHub PAT: A Personal Access Token with
write:packages
permission is required for the GitHub Actions workflow to push images to GHCR.
- Clone the repository:
git clone https://github.com/<your-username>/sample-test-api.git cd sample-test-api
- Build the project:
mvn clean install
- Run the application:
java -jar target/sample-test-api-0.0.2.jar
- Access the API:
The application will be accessible at
http://localhost:8080
. The/actuator/health
endpoint is used for health checks.
- Build the Docker image:
docker build -t sample-test-api .
- Run the Docker container:
docker run -p 8080:8080 sample-test-api
This project uses GitHub Actions to automate the build, push, and deployment process.
- A push to the
main
branch triggers thedocker-build.yml
workflow. - The workflow builds the Docker image and tags it with the commit SHA.
- It then pushes the image to GHCR.
- Finally, it connects to your GKE cluster and updates the deployment to use the newly pushed image.
Based on the project configuration files, here are the step-by-step instructions for setting up both the GitHub and Google Cloud Platform (GCP) environments to support the CI/CD pipeline.
-
Create a New GitHub Repository:
- Initialize a new, empty GitHub repository for your project.
- Clone this repository to your local machine.
- Copy the project files (
Dockerfile
,pom.xml
,docker-build.yml
,deployment.yml
,service.yml
) into the repository directory. - Commit and push these files to the
main
branch of your new repository. This will trigger the GitHub Actions workflow once the GCP setup is complete.
-
Configure GitHub Secrets: The
docker-build.yml
file references a GitHub Secret namedPAT
for authenticating to the GitHub Container Registry. You will need to create a Personal Access Token (PAT) and add it as a secret to your repository.- Go to your GitHub profile settings.
- Navigate to Developer settings > Personal access tokens > Tokens (classic).
- Click Generate new token (classic).
- Give it a descriptive name (e.g., "GHCR_ACCESS").
- Select the
write:packages
permission under the packages scope. - Generate the token and copy the value.
- In your repository, go to Settings > Secrets and variables > Actions.
- Click New repository secret.
- Name the secret
PAT
and paste the token value you copied.
The docker-build.yml
workflow uses Workload Identity Federation to authenticate with GCP, which is the most secure method as it avoids using long-lived service account keys.
-
Enable Required APIs:
- In your GCP project, enable the following APIs from the Google Cloud Console or using the
gcloud
CLI:gke-gcloud-auth-plugin
Google Kubernetes Engine API
Artifact Registry API
Cloud Build API
Cloud Resource Manager API
IAM API
gcloud services enable container.googleapis.com
artifactregistry.googleapis.com
cloudbuild.googleapis.com
cloudresourcemanager.googleapis.com
iam.googleapis.com
--project=for-tech-practice - In your GCP project, enable the following APIs from the Google Cloud Console or using the
-
Create a GKE Cluster:
- Create a GKE cluster with Workload Identity enabled. The
docker-build.yml
file specifies a cluster namedsample-test
in theasia-east1
region and a project namedfor-tech-practice
. You can create a new cluster with these settings. - Note: The deployment configuration requires a cluster that supports LoadBalancer services for public access.
- Create a GKE cluster with Workload Identity enabled. The
-
Create a Dedicated Service Account:
- Create a new service account that the GitHub Actions workflow will impersonate. The
docker-build.yml
file references a service account namedgithub-actions-sa
within thefor-tech-practice
project. - Assign the necessary IAM roles to this service account to allow it to deploy to your GKE cluster. At a minimum, it will need:
Kubernetes Engine Admin
(roles/container.admin
)Workload Identity User
(roles/iam.workloadIdentityUser
)
- Create a new service account that the GitHub Actions workflow will impersonate. The
-
Configure Workload Identity Federation: This step establishes a trust relationship between your GitHub repository and the service account.
- Go to IAM & Admin > Workload Identity Federation in the Google Cloud Console.
- Create a new pool (e.g.,
github-pool
). - Add a new provider to the pool, selecting
GitHub
as the identity provider. - Configure the provider to trust tokens from your specific GitHub repository.
- The
docker-build.yml
file uses the following settings, which you'll need to match:- Workload Identity Provider:
projects/183944825075/locations/global/workloadIdentityPools/github-p/providers/github
- Service Account:
[email protected]
- Workload Identity Provider:
Setting up Workload Identity Federation in GCP allows your GitHub Actions workflow to securely authenticate with your Google Cloud project without using long-lived service account keys. This process establishes a trust relationship between GitHub and a GCP service account.
Here are the steps to configure Workload Identity Federation:
A workload identity pool is a container for all external identities (like GitHub).
- In the Google Cloud Console, navigate to IAM & Admin > Workload Identity Federation.
- Click Create pool.
- Give the pool a unique ID and a display name. For this project, you can use an ID like
github-pool
. - Click Done.
A provider defines the rules for authenticating identities within the pool. In this case, it will be GitHub.
- After creating the pool, click Add a provider.
- Select GitHub as the provider type.
- The provider configuration will automatically fill in the GitHub-specific details.
- Under Repository owner, enter your GitHub username or organization name (e.g.,
sures-g
). - Under Repository name, enter the name of your repository (e.g.,
sample-test-api
). - Click Save. You will now see the provider details, including the Workload Identity Provider path, which you'll copy into your GitHub Actions workflow.
This is the final step that connects your GitHub identity to the GCP service account you want to use.
- In the Google Cloud Console, navigate to the service account you created earlier (e.g.,
github-actions-sa
). - Go to the Permissions tab and click Grant access.
- In the New principals field, paste the Principal value from your Workload Identity Provider. The format is typically
principal://iam.googleapis.com/projects/<project_number>/locations/global/workloadIdentityPools/<pool_id>/subject/repo:<owner>/<repository>
. - Assign the Workload Identity User role (
roles/iam.workloadIdentityUser
) to this principal. - Save the changes.
The docker-build.yml
file already contains the correct configuration for Workload Identity. It uses the google-github-actions/auth@v2
action, which handles the secure authentication process based on the trust relationship you just established. You'll simply need to ensure the workload_identity_provider
and service_account
values in your workflow match the resources you created in GCP.
After completing these steps, pushing code to the main
branch of your GitHub repository should automatically trigger the CI/CD pipeline, building the Docker image, pushing it to GHCR, and deploying the application to your GKE cluster.