Skip to main content

Command Palette

Search for a command to run...

Daily Dose of DevOps — GitHub Actions basics for DevOps

Published
3 min read
M

Platform Engineer writing about distributed systems and devops practices

GitHub Actions Basics for DevOps

In the world of DevOps, automation is key. One of the most powerful tools for automating continuous integration and continuous deployment (CI/CD) processes is GitHub Actions. As a DevOps engineer, you might be familiar with Jenkins, CircleCI, or GitLab CI/CD, but GitHub Actions offers a simple, yet powerful alternative that integrates seamlessly with your existing workflows.

What are GitHub Actions?

GitHub Actions are a set of tools that enable you to automate your development workflow on GitHub. They allow you to define workflows that can run on every push to a branch or commit to a pull request. These workflows can include a variety of steps such as running tests, building and packaging, deploying, and more.

Getting Started with GitHub Actions

To start using GitHub Actions, you need to set up a GitHub account if you don't already have one. Once you have an account, you can create a repository for your project. Inside your repository, you'll find a .github directory, where you can define your workflow files.

Workflow Files

The main file that defines your workflow is the Workflow file, which is typically named workflow.yaml. This file specifies the sequence of steps that will be executed when a push or pull request is detected.

Here's a simple example of a workflow file:

name: CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v3
        with:
          python-version: 3.8
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
      - name: Run tests
        run: |
          pytest
      - name: Build documentation
        run: |
          mkdocs build

This example defines a build job that checks out the code, installs Python, installs dependencies from requirements.txt, runs tests with pytest, and builds the documentation.

Triggering Actions

GitHub Actions can be triggered by various events. For example, a push to a branch or a pull request can trigger a build and deployment. You can also configure actions to be triggered manually by clicking on the "Run action" button in your pull request or branch.

Best Practices for GitHub Actions

  1. Use a .gitignore file: This file tells GitHub which files to ignore during build and deployment. This helps to avoid errors caused by files that shouldn't be included in the build process.

  2. Use environment variables: Instead of hardcoding values, use environment variables to make your workflows more flexible and reusable. You can define environment variables in your .env file or use GitHub Actions environment variables.

  3. Write readable code: Your workflow files should be easy to understand and maintain. Use clear variable names, concise commands, and comments to explain what each step does.

  4. Test your workflow: Before deploying, test your workflow to ensure it works as expected. You can do this by manually running your workflow or by setting up a test GitHub Action.

Key Takeaways

  • GitHub Actions is a powerful tool for automating CI/CD processes.
  • Use a .github directory to define your workflow.
  • Use a .gitignore file to ignore files during build and deployment.
  • Use environment variables to make your workflows more flexible.
  • Test your workflow thoroughly before deploying.

By leveraging GitHub Actions, you can streamline your development workflow, reduce manual errors, and improve your team's productivity.