/self-contained-dart-github-action

A template to demonstrate how to build a self-contained Dart GitHub Action, that doesn't need a Dart container to run

Primary LanguageDockerfile

Self-contained Dart GitHub Action

See here for a comparison of the different ways to build a GitHub Action with Dart.

In 2020, I published the Pub package github_actions_toolkit to write GitHub Actions with Dart more easily. However, using such actions in a workflow is still cumbersome, as they need a full Dart container to be pulled before they can be run, which is very time consuming for these scripts that generally run in a few seconds.

This repository is a template to demonstrate how to create a GitHub Action with Dart that is self-contained in a tiny Docker container, that can be pulled and used in a workflow very quickly.

How it works

The repository leverages the dart compile (ex dart2native) command and GitHub Container Registry to package and publish the Dart action as a small container image.

A GitHub Action workflow automatically compiles the Dart application into a native executable using dart compile. Then, this executable is wrapped in a Docker container that contains only the libraries required to run it. Finally, the image of this container is published on GitHub Container Registry using the Docker API.

By default, the full image tag is {OWNER}/{REPOSITORY}:{BRANCH}, where BRANCH is the branch on which the workflow that published the image was executed.

The image is listed at the address github.com/{OWNER}/{REPOSITORY}/packages.

How to use it in a workflow

The following workflow is inspired by the Hello World example of the GitHub documentation:

on: [push]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Hello World action step
        # docker://{host}/{image}:{tag}
        # See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-using-the-github-packages-container-registry
        uses: docker://ghcr.io/axel-op/self-contained-dart-action:main
        id: hello
        with:
          who-to-greet: "you"
      # Use the output from the `hello` step
      - name: Get the output time
        run: echo "The time was ${{ steps.hello.outputs.time }}"