Streamlining CI/CD Pipelines: Automating Docker and Kubernetes Deployment with GitHub Actions
Reza Chegini
Certified GCP & AWS DevOps Engineer| Seeking Entry-Level Cloud Developer, DevOps, SRE Roles, Software Engineer or Developer | Aspiring DevOps & SRE
As I continue building my skills in DevOps, I recently explored automating a CI/CD workflow using AWS EKS. The process involves building and pushing a Docker image to Docker Hub and deploying it to an AWS Elastic Kubernetes Service (EKS) cluster using GitHub Actions. Here’s a simplified breakdown of the steps I followed:
Workflow File: Overview
This GitHub Actions workflow has two main jobs:
Detailed Explanation of Each Step
Workflow Title
name: Docker CI/CD with Kubernetes Deployment
The workflow title clearly defines its purpose—automating the process of building, pushing, and deploying Docker images to a production environment using Kubernetes and GitHub Actions.
Trigger
on:
push:
branches:
- main
This section triggers the workflow every time a commit is pushed to the main branch. It ensures your CI/CD pipeline runs whenever new changes are made to the production-ready code.
Job 1: Build and Push Docker Image
Step 1: Checkout Code
- name: Checkout code
uses: actions/checkout@v4
This pulls the latest version of your repository code into the GitHub Actions runner. It's the first step in the pipeline, ensuring that the code being built and deployed is the latest version.
Step 2: Log in to Docker Hub
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
Step 3: Build Docker Image
- name: Build Docker image
run: |
docker build -t ${{ secrets.DOCKER_USERNAME }}/my-app:latest .
Step 4: Push Docker Image
- name: Push Docker image
run: |
docker push ${{ secrets.DOCKER_USERNAME }}/my-app:latest
Job 2: Deploy to AWS EKS
Step 1: Configure AWS Credentials
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v3
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-west-2
Step 2: Set up Kubernetes Context
- name: Set up kubectl for EKS
run: |
aws eks update-kubeconfig --region us-west-2 --name my-eks-cluster
curl -LO "https://dl.k8s.io/release/v1.31.0/bin/linux/amd64/kubectl"
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/
Step 3: Apply Kubernetes Manifests
- name: Apply Kubernetes manifests
run: |
kubectl apply -f k8s/deployment.yaml
kubectl apply -f k8s/service.yaml
How to Create GitHub Secrets
Navigate to Secrets Settings:
Add Secrets: Click New repository secret and add the following
Secure Your Secrets: Ensure you never hard-code these values in your workflow file.
Conclusion
This workflow showcases how to seamlessly integrate Docker and Kubernetes into a CI/CD pipeline using GitHub Actions. It simplifies deploying applications and ensures efficiency and consistency.
name: Docker CI/CD with Kubernetes Deployment for Production
on:
push:
branches:
- main
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
# Step 1: Checkout the Code
- name: Checkout code
uses: actions/checkout@v4
# Step 2: Log in to Docker Hub
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
# Step 3: Build Docker Image
- name: Build Docker image
run: |
docker build -t ${{ secrets.DOCKER_USERNAME }}/my-app:latest .
# Step 4: Push Docker Image
- name: Push Docker image
run: |
docker push ${{ secrets.DOCKER_USERNAME }}/my-app:latest
deploy-to-eks:
needs: build-and-push
runs-on: ubuntu-latest
steps:
# Step 1: Configure AWS Credentials
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v3
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-west-2 # Replace with your AWS region
# Step 2: Set up kubectl for EKS
- name: Set up kubectl for EKS
run: |
aws eks update-kubeconfig --region us-west-2 --name my-eks-cluster
curl -LO "https://dl.k8s.io/release/v1.31.0/bin/linux/amd64/kubectl"
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/
# Step 3: Apply Kubernetes Manifests
- name: Apply Kubernetes manifests
run: |
kubectl apply -f k8s/deployment.yaml
kubectl apply -f k8s/service.yaml
--Better .....
2 个月...
Bradsen Bookkeeping and Accounting Inc.
2 个月Insightful