npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

github-actions-wac

v1.4.1

Published

GitHub Actions - Workflows as Code (WaC).

Downloads

7,029

Readme

github-actions-wac

license code style: prettier PRs Welcome

GitHub Actions - Workflows as Code (WaC).

Table of Contents

Installation

npm install --save github-actions-wac --dev

Or if you prefer yarn:

yarn add github-actions-wac --dev

Overview

The github-actions-wac package enables you to create GitHub Actions workflows via TypeScript code.

To get started, simply create a new .wac.ts file in your .github/workflows folder and start creating your GitHub Actions workflow. For example:

// .github/workflows/index.wac.ts

import { createWorkflow, NormalJob } from "github-actions-wac";

// Some global environment variables.
const defaultEnv = {
  NODE_OPTIONS: "--max_old_space_size=4096"
};

// Let's assign some of the common steps into a standalone const.
const checkoutInstallBuildTest: NormalJob["steps"] = [
  { uses: "actions/checkout@v2" },
  { name: "Install dependencies", run: "yarn --immutable" },
  { name: "Build", run: "yarn build" }
];

// Create "Push to main branch" workflow.
export const push = createWorkflow({
  name: "Push to main branch",
  on: "push",
  env: defaultEnv,
  jobs: {
    buildTestRelease: {
      name: "Build, test, release",
      "runs-on": "ubuntu-latest",
      steps: [
        ...checkoutInstallBuildTest,
        {
          name: "Release",
          uses: "cycjimmy/semantic-release-action@v3",
          with: { working_directory: "./dist" },
          env: {
            GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}",
            NPM_TOKEN: "${{ secrets.NPM_TOKEN }}"
          }
        }
      ]
    }
  }
});

// Create "Pull requests" workflow.
export const pullRequests = createWorkflow({
  name: "Pull requests",
  on: "pull_request",
  env: defaultEnv,
  jobs: {
    buildTest: {
      name: "Build and test",
      "runs-on": "ubuntu-latest",
      steps: [...checkoutInstallBuildTest]
    }
  }
});

Once you're done, in your terminal, simply run the npx github-actions-wac build (or npx ghawac build) CLI command to emit regular YAML files. For example, if we were to build the above example, we'd end up with two YAML files: push.yml and pullRequests.yml.

The npx github-actions-wac build commands detects all exported workflows from .wac.ts files and emits a standalone YAML file for each one.

It's up to you to decide whether you want a single .wac.ts file that exports all workflows, or multiple .wac.ts files where each exports a single workflow.

Why Create GitHub Actions via Code?

Creating GitHub Actions workflows via (TypeScript) code has a couple of benefits:

  • if you don't like YAML in general, then this might be a more favorable approach
  • type safety - the mentioned npx github-actions-wac build CLI command will throw TypeScript errors if something is wrong
  • no need to copy/paste dozens of lines of YAML - simply store all of your repetitive jobs/steps as variables (or even as factory functions if additional dynamicity is required)
  • it's even possible to import external NPM modules if needed (although, personally I haven't had the need to do it yet)

Examples

| Example | Description | | ------------------------------------------------------------ | --------------------------------------------------------------------------- | | Simple Workflow | Exports a single workflow that consists of a couple of a single job and some steps. | | Multiple Workflows | Exports multiple workflows that consist of a single jobs and multiple steps. | | Complex Example | A more complex example that exports multiple branch-dependent workflows. |

Reference

Functions

createWorkflow

export declare const createWorkflow: (workflow: Workflow) => Workflow;

Creates a new GitHub Actions workflow. Accepts a Workflow object.

import { createWorkflow } from "github-actions-wac";

export const push = createWorkflow({
    name: "Push to main branch",
    on: "push",
    env: defaultEnv,
    jobs: { ... }
});

CLI

This package includes a small CLI that can be invoked via npx or yarn:

# Using npx:
npx github-actions-wac

# Using yarn: 
yarn github-actions-wac

Instead of github-actions-wac, you can also use ghawac to invoke the CLI. For example: npx ghawac (yarn ghawac).

You can also run npx github-actions-wac --help for in-terminal reference.

build

Builds YAML from detected TypeScript (.wac.ts) workflow files.

npx github-actions-wac build

watch

Watches for changes in detected TypeScript (.wac.ts) workflow files and automatically generates YAML.

npx github-actions-wac watch