Basic GitHub Actions Checkout

arif_hossain

Arif Hossain

Posted on November 9, 2024

Basic GitHub Actions Checkout

Overview
This hands-on lab introduces the fundamentals of GitHub Actions by implementing a basic workflow that demonstrates repository checkout and command execution. You'll learn how to create a workflow file, understand its structure, and execute various commands.

Prerequisites

  • GitHub account
  • A GitHub repository where you have write access
  • Basic understanding of YAML syntax
  • Basic knowledge of command line operations
    Learning Objectives
    After completing this lab, you will be able to:

  • Create a basic GitHub Actions workflow file

  • Understand workflow trigger conditions

  • Execute single and multi-line commands

  • Use the checkout action

  • View and interpret workflow results

Lab Structure

your-repository/
├── .github/
│   └── workflows/
│       └── basic-checkout.yml
└── README.md
Enter fullscreen mode Exit fullscreen mode

Implementation Guide
Step 1: Create Workflow Directory

mkdir -p .github/workflows
Enter fullscreen mode Exit fullscreen mode

Step 2: Create Workflow File
Create .github/workflows/basic-checkout.yml with the following content:

name: Basic Checkout Lab

# Trigger on push to main branch
on:
  push:
    branches:
      - main

# Define jobs and their steps
jobs:
  basic-checkout:
    runs-on: ubuntu-latest
    steps:
      # Step 1: Checkout the repository
      - name: Checkout repository
        uses: actions/checkout@v4

      # Step 2: List repository contents
      - name: List files
        run: ls -la

      # Step 3: Display system information
      - name: Show system info
        run: |
          echo "Repository: $GITHUB_REPOSITORY"
          echo "Operating System: $(uname -a)"
          echo "Current Directory: $(pwd)"

      # Step 4: Check software versions
      - name: Check versions
        run: |
          echo "Node version: $(node --version)"
          echo "Python version: $(python --version)"
          echo "Git version: $(git --version)"
Enter fullscreen mode Exit fullscreen mode

Detailed Explanation
Workflow Trigger
on:
push:
branches:
- main
This section configures the workflow to run whenever code is pushed to the main branch.

Job Configuration
jobs:
basic-checkout:
runs-on: ubuntu-latest

Defines a single job named basic-checkout
Specifies Ubuntu as the runner operating system
Steps Breakdown
`Checkout Step

  • name: Checkout repository uses: actions/checkout@v4 Uses the official checkout action Clones your repository into the runner Essential for accessing repository files File Listing
  • name: List files run: ls -la Lists all files and directories Includes hidden files Shows file permissions and ownership System Information - name: Show system info run: | echo "Repository: $GITHUB_REPOSITORY" echo "Operating System: $(uname -a)" echo "Current Directory: $(pwd)" Uses multi-line commands Demonstrates environment variable usage Shows system details Version Checks
  • name: Check versions run: | echo "Node version: $(node --version)" echo "Python version: $(python --version)" echo "Git version: $(git --version)"` Checks installed software versions Uses command substitution Demonstrates multi-line commands Expected Outputs
File Listing Output
total 24
drwxr-xr-x 4 runner docker 4096 Feb 20 10:00 .
drwxr-xr-x 3 runner docker 4096 Feb 20 10:00 ..
drwxr-xr-x 8 runner docker 4096 Feb 20 10:00 .git
drwxr-xr-x 3 runner docker 4096 Feb 20 10:00 .github
-rw-r--r-- 1 runner docker  654 Feb 20 10:00 README.md
Enter fullscreen mode Exit fullscreen mode

System Information Output

Repository: username/repository-name
Operating System: Linux runner-ubuntu-latest 5.15.0-1047-azure
Current Directory: /home/runner/work/repository-name/repository-name
Version Information Output
Node version: v20.11.0
Python version: Python 3.10.12
Git version: git version 2.42.0
Enter fullscreen mode Exit fullscreen mode

Practice Exercises
Add New Commands Modify the workflow to include:

  • Current date and time
  • Available disk space
  • Memory usage Custom Environment Variables Add environment variables:

env:
CUSTOM_MESSAGE: "Hello from GitHub Actions!"

Conditional Execution Add a conditional step:

- name: Conditional step
if: github.event_name == 'push'
run: echo "This was triggered by a push event"

Troubleshooting
Common Issues and Solutions
Workflow Not Triggering

  • Verify branch name matches trigger condition
  • Check workflow file syntax
  • Ensure Actions are enabled in repository settings
  • Checkout Action Fails

Verify Git configuration

git config --global --list
Enter fullscreen mode Exit fullscreen mode

Check permissions

ls -la .git
Enter fullscreen mode Exit fullscreen mode

Command Execution Errors

Check command availability on runner
Verify syntax for multi-line commands
Ensure proper environment variable usage
Debugging Tips
Enable debug logging by setting secret ACTIONS_RUNNER_DEBUG to true
Check workflow run logs in GitHub Actions tab
Use echo statements to debug variables

Additional Resources:

💖 💪 🙅 🚩
arif_hossain
Arif Hossain

Posted on November 9, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related