gha 0.0.2 copy "gha: ^0.0.2" to clipboard
gha: ^0.0.2 copied to clipboard

Type-safe GitHub Actions workflows in Dart with strongly typed models, action helpers, and YAML generation for creating workflows programmatically.

gha #

pub package Dart License Build Status Buy Me A Coffee

gha provides strongly typed Dart models for GitHub Actions workflows. The types mirror the gha-ts3 TypeScript definitions so you can create and serialize workflows entirely in Dart. Serialization is handled by dart_mappable, and YAML output is produced with json2yaml.

Features #

  • 🎯 Type-safe workflow definitions - Catch errors at compile-time
  • πŸ”§ Helper functions for common actions - Simplified API for popular GitHub Actions
  • πŸ“¦ Support for all major setup actions - Node, Python, Java, Go, .NET, and more
  • πŸš€ Artifact and cache management - Easy upload/download and caching
  • πŸ“„ GitHub Pages deployment - Built-in helpers for Pages workflows
  • πŸ” GitHub App token generation - Simplified authentication

Quick Start #

import 'package:gha/gha.dart';

void main() {
  final workflow = Workflow(
    name: 'CI',
    on: WorkflowTriggers.simple(['push', 'pull_request']),
    jobs: {
      'build': Job(
        name: 'Build',
        runsOn: RunnerSpec.single('ubuntu-latest'),
        steps: const [
          Step(name: 'Checkout', uses: 'actions/checkout@v4'),
          Step(name: 'Test', run: 'dart test'),
        ],
      ),
    },
  );

  // Print YAML to console
  print(workflow.toYamlString());

  // Or save directly to a file
  workflow.save(); // Saves to .github/workflows/ci.generated.yaml
}

Saving Workflows #

The save() extension method allows you to write workflows directly to files:

import 'package:gha/gha.dart';

void main() {
  final workflow = Workflow(name: 'CI', /* ... */);

  // Save with default settings
  workflow.save(); // Saves to .github/workflows/ci.generated.yaml

  // Save with custom filename
  workflow.save(filename: 'my-workflow.yaml');

  // Save to custom directory
  workflow.save(directory: 'custom/workflows');

  // Both custom directory and filename
  workflow.save(directory: 'workflows', filename: 'build.yaml');
}

The filename is automatically derived from the workflow's name property (lowercase, spaces to hyphens) unless you provide a custom filename.

Using Action Helpers #

The package includes type-safe helpers for common GitHub Actions:

import 'package:gha/gha.dart';

void main() {
  final workflow = Workflow(
    name: 'CI with Helpers',
    on: WorkflowTriggers.simple(['push']),
    jobs: {
      'build': Job(
        runsOn: RunnerSpec.single('ubuntu-latest'),
        steps: [
          // Use helper functions instead of raw strings
          checkout(CheckoutOptions(fetchDepth: 1)),
          setupNode(SetupNodeOptions(
            nodeVersion: '20',
            cache: 'npm',
          )),
          Step(run: 'npm ci'),
          Step(run: 'npm test'),
          uploadArtifact(UploadArtifactOptions(
            path: 'coverage/',
            name: 'coverage-report',
            retentionDays: 7,
          )),
        ],
      ),
    },
  );

  print(workflow.toYamlString());
}

Available Action Helpers #

Common Actions:

  • checkout() - Check out repository code
  • githubScript() - Run JavaScript with Octokit
  • createGitHubAppToken() - Generate GitHub App tokens

Artifact Management:

  • uploadArtifact() - Upload build artifacts
  • downloadArtifact() - Download artifacts

Caching:

  • cache() - Cache and restore dependencies
  • cacheRestore() - Restore cache only
  • cacheSave() - Save cache only

Language/Runtime Setup:

  • setupNode() - Set up Node.js
  • setupPython() - Set up Python
  • setupJava() - Set up Java
  • setupGo() - Set up Go
  • setupDotnet() - Set up .NET

GitHub Pages:

  • configurePages() - Configure Pages
  • uploadPagesArtifact() - Upload Pages artifact
  • deployPages() - Deploy to Pages

For detailed documentation on each action helper, see lib/src/actions_README.md.

Examples #

We provide focused examples for different use cases:

# Basic CI workflow
dart run example/01_basic_ci.dart

# Multi-language builds
dart run example/02_multi_language.dart

# Caching strategies
dart run example/03_caching.dart

# Artifact management
dart run example/04_artifacts.dart

# GitHub Pages deployment
dart run example/05_github_pages.dart

# GitHub Script automation
dart run example/06_github_script.dart

# GitHub App tokens
dart run example/07_github_app_token.dart

# Matrix builds
dart run example/08_matrix_builds.dart

See example/README.md for detailed descriptions of each example.

Inspiration #

This package is inspired by JLarky/gha-ts, bringing similar type-safe workflow definitions to Dart.

0
likes
160
points
--
downloads

Publisher

verified publisherglenfordwilliams.com

Weekly Downloads

Type-safe GitHub Actions workflows in Dart with strongly typed models, action helpers, and YAML generation for creating workflows programmatically.

Repository (GitHub)
View/report issues

Topics

#github-actions #workflow #ci-cd #yaml #automation

Documentation

API reference

Funding

Consider supporting this project:

www.buymeacoffee.com

License

MIT (license)

Dependencies

dart_mappable, json2yaml, json_schema, path, yaml

More

Packages that depend on gha