Logo

0x3d.site

is designed for aggregating information and curating knowledge.

"How to use github actions"

Published at: May 13, 2025
Last Updated at: 5/13/2025, 2:53:43 PM

Understanding GitHub Actions

GitHub Actions provides a platform to automate tasks directly within the GitHub workflow. It allows for building, testing, and deploying code, as well as automating other development tasks like managing issues or pull requests. Actions are triggered by specific events within a GitHub repository, such as pushing code, creating a pull request, or opening an issue.

Core Concepts of GitHub Actions

Using GitHub Actions involves understanding a few key components:

  • Workflows: Automated processes defined in a YAML file (.yml) located in the .github/workflows directory of a repository. A repository can have multiple workflows.
  • Events: Specific activities that trigger a workflow. Examples include push, pull_request, issue_comment, schedule, or workflow_dispatch (manual trigger).
  • Jobs: A set of steps that execute on the same runner. A workflow can have one or more jobs, which can run sequentially or in parallel.
  • Steps: An individual task within a job. Steps can run commands (like run: npm install) or use pre-built actions.
  • Actions: Reusable units of code that perform a specific task. These can be created by GitHub, the community, or developed custom. Examples include checking out code, setting up a Node.js environment, or publishing to a package registry. Actions are referenced using uses:.
  • Runners: Servers that execute workflow jobs. GitHub provides hosted runners (various operating systems like Ubuntu, Windows, macOS) or self-hosted runners can be used for custom environments.

Creating a GitHub Actions Workflow

Workflows are defined in YAML files within the .github/workflows directory at the root of a repository. The file name does not affect the workflow's execution, but it should be descriptive (e.g., ci.yml, deploy.yml).

A basic workflow file structure includes:

name: Descriptive Workflow Name

on:
  event_type: # The event that triggers the workflow

jobs:
  job_name: # A unique identifier for the job
    runs-on: ubuntu-latest # The runner environment

    steps:
      - name: Checkout code # A descriptive name for the step
        uses: actions/checkout@v4 # Use a pre-built action to checkout the repository

      - name: Run a command # A step that runs a shell command
        run: echo Hello, world!

Step-by-Step Workflow Creation:

  1. Navigate to the repository on GitHub.
  2. Click on the "Actions" tab.
  3. GitHub may suggest some workflow templates based on the repository's language. To create a custom workflow, click "set up a workflow yourself" or locate a similar option.
  4. This opens a web editor for a new YAML file (typically .github/workflows/main.yml).
  5. Define the name of the workflow.
  6. Specify the on event(s) that trigger the workflow.
  7. Define one or more jobs.
  8. For each job, specify the runs-on runner.
  9. List the steps within each job. Each step must have a name and either run (for commands) or uses (for actions).
  10. Commit the new workflow file directly to the repository or create a pull request.

Real-World Example: Continuous Integration (CI)

A common use case is setting up Continuous Integration to automatically build and test code on every push or pull request.

name: CI Build and Test

on:
  push:
    branches: [ main, develop ] # Trigger on push to main or develop branches
  pull_request:
    branches: [ main, develop ] # Trigger on pull request targeting main or develop

jobs:
  build-and-test:
    runs-on: ubuntu-latest # Run on a fresh Ubuntu environment

    steps:
      - name: Checkout Code
        uses: actions/checkout@v4 # Get the code from the repository

      - name: Set up Node.js # Example: Set up a specific Node.js version
        uses: actions/setup-node@v4
        with:
          node-version: '20' # Specify Node.js version

      - name: Install Dependencies # Run package manager install command
        run: npm ci # Use ci for cleaner installs in CI environments

      - name: Run Tests # Execute the test command
        run: npm test # Assumes a 'test' script in package.json

This workflow will automatically:

  • Check out the code.
  • Set up a Node.js environment.
  • Install project dependencies.
  • Run the tests defined in the project.

The status of the workflow (success or failure) is reported back to the GitHub UI on the commit or pull request.

Real-World Example: Simple Deployment

After a successful CI build, a deployment workflow could be triggered. This example shows a conceptual deployment step (deployment details vary widely).

name: Deploy to Staging

on:
  push:
    branches: [ main ] # Trigger deployment on push to main branch

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Set up Node.js # If deployment involves building assets
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install Dependencies
        run: npm ci

      # Example: Build production assets
      - name: Build Project
        run: npm run build # Assumes a 'build' script

      # Example: Deploy using a custom script or an action
      - name: Deploy via SSH # Conceptual step
        run: |
          echo "Deploying project..."
          # Add deployment commands here (e.g., rsync, ssh commands)
          # Securely access deployment credentials using GitHub Secrets

      # Or using a deployment action (example - requires specific action)
      # - name: Deploy to Service X
      #   uses: some/deployment-action@v1
      #   with:
      #     service_credentials: ${{ secrets.SERVICE_CREDENTIALS }}

Deployment workflows often utilize GitHub Secrets to store sensitive information like API keys, passwords, or private keys securely. Secrets are accessed within the workflow using the syntax ${{ secrets.SECRET_NAME }}.

Practical Tips for Using GitHub Actions

  • Start Simple: Begin with a basic workflow (like running linters or simple tests) and gradually add complexity.
  • Use Specific Action Versions: Always pin actions to a specific version (e.g., actions/checkout@v4) instead of using the floating main or latest tag. This prevents unexpected failures if the action changes.
  • Leverage Marketplace Actions: Explore the GitHub Actions Marketplace for pre-built actions that perform common tasks.
  • Secure Secrets: Use GitHub Secrets to store sensitive information. Never hardcode credentials or secrets in workflow files.
  • Optimize Job Execution: Configure jobs to run in parallel when possible to reduce workflow duration. Use needs to define dependencies between jobs that must run sequentially.
  • Understand Runner Environments: Be aware of the software pre-installed on GitHub's hosted runners. This can save time setting up common tools.
  • Monitor Workflow Runs: Regularly check the "Actions" tab in the repository to monitor workflow execution status, view logs, and debug failures.
  • Utilize Caching: For workflows with many dependencies (like Node.js, Python, Java), use caching actions (e.g., actions/cache@v4) to significantly speed up installation time between runs.

Insights on GitHub Actions

GitHub Actions is a powerful, integrated CI/CD and automation tool. Its key advantages include:

  • Native Integration: Seamlessly integrated with GitHub repositories, pull requests, and issue management.
  • YAML-based: Workflows are defined in standard YAML, making them readable and version-controlled alongside the code.
  • Extensible: A large and growing marketplace of reusable actions, plus the ability to create custom actions.
  • Cost-Effective: Offers free usage minutes for public repositories and generous free tiers for private repositories, with competitive pricing for additional usage.

By defining workflows in YAML files, teams can automate repetitive development tasks, enforce code quality standards, and establish robust continuous integration and deployment pipelines directly within their GitHub workflow.


Related Articles

See Also

Bookmark This Page Now!