7.7 KiB
7.7 KiB
Workflow Operations Reference
Comprehensive guide for GitHub Actions workflow management using gh CLI.
Listing Workflows
View Available Workflows
# List all workflows in repository
gh workflow list
# List with detailed status
gh workflow list --all
# List workflows as JSON
gh workflow list --json name,id,state,path
Viewing Workflow Details
Inspect Workflow Configuration
# View workflow details
gh workflow view workflow-name
# View workflow by ID
gh workflow view 12345
# View workflow YAML
gh workflow view workflow-name --yaml
# View workflow in browser
gh workflow view workflow-name --web
Enabling and Disabling Workflows
Workflow State Management
# Enable workflow
gh workflow enable workflow-name
# Enable workflow by ID
gh workflow enable 12345
# Disable workflow
gh workflow disable workflow-name
# Disable workflow by ID
gh workflow disable 12345
Running Workflows
Manual Workflow Triggers
# Run workflow manually
gh workflow run workflow-name
# Run workflow on specific branch
gh workflow run workflow-name --ref feature-branch
# Run workflow with inputs
gh workflow run workflow-name -f input1=value1 -f input2=value2
# Run workflow with JSON inputs
gh workflow run workflow-name \
-f config='{"env":"production","debug":false}'
Viewing Workflow Runs
List Workflow Runs
# List all workflow runs
gh run list
# List runs for specific workflow
gh run list --workflow=workflow-name
# List runs with filters
gh run list --status success
gh run list --status failure
gh run list --branch main
# List recent runs
gh run list --limit 20
# List runs as JSON
gh run list --json databaseId,status,conclusion,headBranch,event
Viewing Specific Run Details
Inspect Run Information
# View specific run details
gh run view run-id
# View run in browser
gh run view run-id --web
# View run logs
gh run view run-id --log
# View failed run logs only
gh run view run-id --log-failed
# Get run as JSON
gh run view run-id --json status,conclusion,jobs,createdAt
Monitoring Runs
Real-Time Monitoring
# Watch workflow run in real-time
gh run watch run-id
# Watch with log output
gh run watch run-id --exit-status
# Watch interval (check every N seconds)
gh run watch run-id --interval 10
Downloading Artifacts and Logs
Retrieve Run Data
# Download workflow run logs
gh run download run-id
# Download specific artifact
gh run download run-id --name artifact-name
# Download to specific directory
gh run download run-id --dir ./downloads
# List available artifacts
gh run view run-id --log | grep "artifact"
Canceling and Rerunning Workflows
Run Control Operations
# Cancel workflow run
gh run cancel run-id
# Rerun workflow
gh run rerun run-id
# Rerun only failed jobs
gh run rerun run-id --failed
# Rerun with debug logging
gh run rerun run-id --debug
Workflow Jobs
Viewing Job Details
# List jobs for a run
gh api repos/{owner}/{repo}/actions/runs/{run_id}/jobs
# View specific job logs
gh run view run-id --log --job job-id
# Download job logs
gh api repos/{owner}/{repo}/actions/jobs/{job_id}/logs > job.log
Advanced Workflow Operations
Workflow Timing Analysis
# Get run timing
gh run view run-id --json createdAt,startedAt,updatedAt,conclusion
# List slow runs
gh run list --workflow=ci --json databaseId,createdAt,updatedAt | \
jq '.[] | select((.updatedAt | fromdate) - (.createdAt | fromdate) > 600)'
Workflow Success Rate
# Calculate success rate for workflow
gh run list --workflow=ci --limit 100 --json conclusion | \
jq '[.[] | .conclusion] | group_by(.) | map({conclusion: .[0], count: length})'
Bulk Operations
Managing Multiple Runs
# Cancel all running workflows
gh run list --status in_progress --json databaseId -q '.[].databaseId' | \
xargs -I {} gh run cancel {}
# Rerun all failed runs from today
gh run list --status failure --created today --json databaseId -q '.[].databaseId' | \
xargs -I {} gh run rerun {}
# Download artifacts from multiple runs
gh run list --workflow=build --limit 5 --json databaseId -q '.[].databaseId' | \
xargs -I {} gh run download {}
Workflow Secrets and Variables
Managing Secrets (via API)
# List repository secrets
gh api repos/{owner}/{repo}/actions/secrets
# Create/update secret
gh secret set SECRET_NAME --body "secret-value"
# Create secret from file
gh secret set SECRET_NAME < secret.txt
# Delete secret
gh secret delete SECRET_NAME
# List secrets
gh secret list
Managing Variables
# List repository variables
gh variable list
# Set variable
gh variable set VAR_NAME --body "value"
# Delete variable
gh variable delete VAR_NAME
Workflow Dispatch Events
Triggering with workflow_dispatch
Example workflow file configuration:
on:
workflow_dispatch:
inputs:
environment:
description: 'Deployment environment'
required: true
default: 'staging'
type: choice
options:
- staging
- production
debug:
description: 'Enable debug mode'
required: false
type: boolean
Trigger with inputs:
gh workflow run deploy.yml \
-f environment=production \
-f debug=true
Monitoring and Debugging
Common Debugging Techniques
# View recent failures
gh run list --status failure --limit 10
# Check specific run logs
gh run view run-id --log-failed
# Download logs for analysis
gh run download run-id
# Rerun with debug logging
gh run rerun run-id --debug
# Check workflow syntax
gh workflow view workflow-name --yaml
Workflow Performance Monitoring
# Get average run duration
gh run list --workflow=ci --limit 50 --json createdAt,updatedAt | \
jq '[.[] | ((.updatedAt | fromdate) - (.createdAt | fromdate))] | add / length'
# Find longest running jobs
gh api repos/{owner}/{repo}/actions/runs/{run_id}/jobs | \
jq '.jobs | sort_by(.started_at) | reverse | .[0:5]'
Best Practices
Workflow Organization
- Use descriptive names - Make workflow purpose clear
- Modular workflows - Break complex workflows into reusable actions
- Cache dependencies - Speed up builds with caching
- Matrix strategies - Test across multiple environments
- Workflow dependencies - Use
needsto control execution order
Workflow Triggers
- Selective triggers - Use path filters to run only when needed
- Schedule wisely - Avoid resource waste with cron triggers
- Manual triggers - Provide workflow_dispatch for flexibility
- PR workflows - Separate validation from deployment
- Branch protection - Require status checks before merge
Secrets Management
- Use secrets - Never hardcode credentials
- Scope appropriately - Use environment-specific secrets
- Rotate regularly - Update secrets periodically
- Audit access - Review who can access secrets
- Use OIDC - Prefer token-less authentication when possible
Performance Optimization
- Conditional execution - Skip unnecessary jobs
- Parallel jobs - Run independent jobs concurrently
- Artifact management - Clean up old artifacts
- Self-hosted runners - Use for resource-intensive workloads
- Job timeouts - Set reasonable timeout limits
Monitoring and Alerts
- Enable notifications - Get alerted on failures
- Status badges - Display workflow status in README
- Metrics tracking - Monitor success rates and duration
- Log retention - Configure appropriate retention policies
- Dependency updates - Automate with Dependabot