Introduction
In this guide, we’ll walk through setting up a professional DevOps workflow using GitHub, Docker, and best deployment practices. Whether you’re working on solo projects or managing complex applications, a robust DevOps workflow ensures smooth deployments, efficient CI/CD, and minimal downtime.
Why DevOps Matters
A structured DevOps pipeline brings benefits such as:
- Automated testing and deployments 🚀
- Efficient version control and rollback strategies 🔄
- Seamless collaboration between developers and operations 🤝
- Improved security with automated checks 🔐
Setting Up a GitHub Repository
Start by initializing a GitHub repository for your project:
git initCreate a .gitignore file to exclude unnecessary files:
echo "node_modules/\n.env\n" > .gitignore
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/your-username/devops-workflow.git
git push -u origin mainIntegrating Docker
Docker helps in containerizing applications, making deployments consistent across different environments.
Creating a Dockerfile
# Use an official Node.js image
FROM node:18-alpine
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install --production
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]Building and Running the Container
docker build -t my-app .
docker run -p 3000:3000 my-appSetting Up GitHub Actions for CI/CD
Automate builds and deployments using GitHub Actions.
Creating a .github/workflows/deploy.yml file
name: CI/CD Pipeline
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker Hub
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
- name: Build and Push Docker Image
run: |
docker build -t your-dockerhub-username/my-app:latest .
docker push your-dockerhub-username/my-app:latest
- name: Deploy to Server
run: |
ssh user@your-server "docker pull your-dockerhub-username/my-app:latest && docker stop my-app || true && docker rm my-app || true && docker run -d -p 3000:3000 --name my-app your-dockerhub-username/my-app:latest"Deploying to a Cloud Server
Use DigitalOcean, AWS, or any VPS to host your application.
Setting Up SSH Authentication
Generate an SSH key and add it to your server:
ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
cat ~/.ssh/id_rsa.pub | ssh user@your-server "cat >> ~/.ssh/authorized_keys"Running Docker on the Server
ssh user@your-server
sudo apt update && sudo apt install -y docker.ioNow, you can deploy automatically whenever you push to GitHub! 🚀
Conclusion
By implementing a GitHub + Docker + CI/CD workflow, you ensure your deployments are automated, reliable, and scalable. Start using this workflow for your projects and experience a seamless DevOps journey! 🔥