Github Actions to Deploy Microservices to Kubernetes
GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. You can create workflows that build and test every pull request to your repository, or deploy merged pull requests to production.
GitHub Actions goes beyond just DevOps and lets you run workflows when other events happen in your repository. For example, you can run a workflow to automatically add the appropriate labels whenever someone creates a new issue in your repository.
Code snippet
name: Deploy Spring Boot to Kubernetes
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Java
uses: actions/setup-java@v1
with:
java-version: 11
- name: Build with Maven
run: mvn clean install
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Kubernetes
uses: actions/setup-kubernetes@v1
with:
kubeconfig: ${{ secrets.KUBECONFIG }}
- name: Deploy to Kubernetes
run: kubectl apply -f deployment.yaml
This pipeline will first
checkout the code from the repository, then set up Java and build the
application with Maven. Once the application is built, it will deploy it to
Kubernetes using the kubectl apply command.
Here is a breakdown of
each step in the pipeline:
- on: push: branches: master - This event listener will
trigger the pipeline when a new commit is pushed to the master branch.
- jobs: build - This job will build the Spring Boot application.
- steps: - These steps are executed in order to build the application.
- uses: actions/checkout@v2 - This
step checks out the code from the repository.
- name: Set up Java - This step sets up Java
on the runner.
- run: mvn clean install - This step builds the
application with Maven.
- jobs: deploy - This job will deploy the Spring Boot application to
Kubernetes.
- needs: build - This job depends on the build job. This
means that the deploy job will not start until the build job has
completed successfully.
- steps: - These steps are executed in order to deploy the application
to Kubernetes.
- uses: actions/checkout@v2 - This
step checks out the code from the repository.
- name: Set up Kubernetes - This step sets up
Kubernetes on the runner.
- with: kubeconfig: ${{ secrets.KUBECONFIG }} - This
step sets the Kubernetes configuration file as a secret.
- name: Deploy to Kubernetes - This
step deploys the application to Kubernetes using the kubectl apply command.