Dart SDK 安装与设置 GitHub Action 教程
1、项目介绍
setup-dart
是一个 GitHub Action,用于在 GitHub Actions 工作流中安装和设置 Dart SDK。这个 Action 允许你在 CI/CD 流程中轻松地配置 Dart SDK,以便运行 Dart 项目的构建、测试和其他检查。
2、项目快速启动
安装最新稳定版本的 Dart SDK
以下是一个简单的 GitHub Actions 工作流配置,用于安装最新稳定版本的 Dart SDK 并运行常见的检查:
name: Dart
on:
pull_request:
branches: [main]
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dart-lang/setup-dart@v1
- run: dart pub get
- run: dart format --output=none --set-exit-if-changed
- run: dart analyze
- run: dart test
安装特定版本的 Dart SDK
如果你想安装特定版本的 Dart SDK,可以在 setup-dart
步骤中指定 sdk
输入参数:
- uses: dart-lang/setup-dart@v1
with:
sdk: '2.19.0'
3、应用案例和最佳实践
应用案例
假设你有一个 Dart 项目,并且希望在每次提交代码时自动运行格式检查、静态分析和测试。你可以使用 setup-dart
Action 来实现这一目标。
最佳实践
- 使用最新稳定版本:建议使用最新稳定版本的 Dart SDK,以确保你获得最新的功能和修复。
- 矩阵测试:如果你希望在多个操作系统和 Dart SDK 版本上运行测试,可以使用矩阵策略:
jobs:
test:
runs-on: $[[ matrix.os ]]
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
sdk: [3.1, stable, beta]
steps:
- uses: actions/checkout@v4
- uses: dart-lang/setup-dart@v1
with:
sdk: $[[ matrix.sdk ]]
- name: Install dependencies
run: dart pub get
- name: Run tests
run: dart test
4、典型生态项目
Flutter
Flutter 是一个由 Dart 驱动的开源 UI 软件开发工具包,用于构建跨平台的移动、Web 和桌面应用程序。Flutter 与 Dart SDK 紧密集成,使用 setup-dart
Action 可以轻松地在 CI/CD 流程中配置 Flutter 项目。
Aqueduct
Aqueduct 是一个用于构建 RESTful API 的 Dart 框架。它使用 Dart SDK 来运行服务器端应用程序,setup-dart
Action 可以帮助你在 GitHub Actions 中自动化 Aqueduct 项目的构建和测试。
Stagehand
Stagehand 是一个 Dart 项目的脚手架工具,用于生成不同类型的 Dart 项目模板。通过 setup-dart
Action,你可以在 CI/CD 流程中自动生成和测试这些项目模板。
通过以上教程,你应该能够轻松地在 GitHub Actions 中配置和使用 Dart SDK,并将其应用于各种 Dart 生态项目中。