DevOps for Beginners

Learn the basics of Python programming with hands-on examples.

Illustration of Python programming

DevOps Basics Tutorial

Master the fundamentals of DevOps with practical examples on Git, CI/CD, and Docker.

1. Introduction to DevOps

DevOps combines development and operations to improve software delivery through automation, collaboration, and continuous improvement. This tutorial introduces key DevOps practices: version control with Git, continuous integration/continuous deployment (CI/CD) with GitHub Actions, and containerization with Docker.

Setting Up Git

Git is a version control system for tracking code changes. Install Git from git-scm.com and verify:

git --version

Initialize a Git repository:

mkdir my-project
cd my-project
git init

Create a file (e.g., `index.html`), stage, and commit it:

echo "

Hello, DevOps!

" > index.html git add . git commit -m "Initial commit"

Push to a remote repository (e.g., GitHub):

git remote add origin https://github.com/username/my-project.git
git push -u origin main

Setting Up a CI/CD Pipeline with GitHub Actions

GitHub Actions automates testing and deployment. Create a workflow file at `.github/workflows/ci.yml` in your repository:

name: CI Pipeline
on:
  push:
    branches: [ main ]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '16'
      - name: Install dependencies
        run: npm install
      - name: Run tests
        run: npm test

This workflow triggers on pushes to the `main` branch, checks out the code, sets up Node.js, installs dependencies, and runs tests. Create a simple `package.json` for a Node.js project:

{
  "name": "my-project",
  "version": "1.0.0",
  "scripts": {
    "test": "echo \"Tests passed\" && exit 0"
  }
}

Push the changes to see the workflow run on GitHub.

Containerizing with Docker

Docker packages applications with dependencies. Install Docker from docker.com and verify:

docker --version

Create a simple Node.js app (`app.js`):

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello, DevOps!');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Create a `Dockerfile`:

FROM node:16
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]

Build and run the Docker container:

docker build -t my-devops-app .
docker run -p 3000:3000 my-devops-app

Access the app at `http://localhost:3000`.

Practical Example: CI/CD with Docker Deployment

Extend the GitHub Actions workflow to build and push a Docker image to Docker Hub. Update `ci.yml`:

name: CI/CD Pipeline
on:
  push:
    branches: [ main ]
jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      - name: Login to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          context: .
          push: true
          tags: username/my-devops-app:latest

Add `DOCKER_USERNAME` and `DOCKER_PASSWORD` as GitHub Secrets in your repository settings. This workflow builds and pushes the Docker image to Docker Hub on every push to `main`.

Next Steps

Explore tools like Kubernetes for orchestration, Jenkins for advanced CI/CD, or Terraform for infrastructure as code. Practice by deploying the app to a cloud provider like AWS or Azure.