Skip to content

Latest commit

 

History

History
229 lines (153 loc) · 10.6 KB

ReusableWorkflows.md

File metadata and controls

229 lines (153 loc) · 10.6 KB

Reusable GitHub Actions Workflows

Unless noted in the YAML, these are all licensed under the root folder's MIT license.

GitHub Actions

npm-create-github-release-with-artifact.yml

Create a GitHub release using an existing tag, then attach a file from the runner artifact to the release. This only supports a single artifact file.

verify-tag-is-on-allowed-branch.yml

Given a list of branch names, verifies that the tag exists on one or more of those branch names. This helps guard against someone pushing a tag to another branch on the repo in cases where you can't restrict who can push tags. Or you just want to guard against someone pushing a version tag to the wrong branch.

GitHub Tokens

generate-github-token-from-github-app.yml

Generates a GITHUB_TOKEN from a GitHub Application registered for the organization. The list of requested permissions can be customized.

Notes:

  • Because of how GitHub takes efforts to protect GitHub Tokens from being passed between jobs, we have to encrypt/obfuscate the token. Doing this in a separate job is still useful because it limits exposure of the GitHub App's private key to just this job runner. The other job steps which consume the token have no way to request a higher level set of permissions.

  • As late as Aug 2023, GitHub App tokens can NOT be used to access GitHub Packages.

Versioning

calculate-version-from-txt-using-github-run-id.yml

Calculates the version using a version.txt file and GitHub "github.run_id" value. This will increment the patch value roughly every 15 minutes.

.NET

Scripts for executing the dotnet build / test / pack / publish cycle. These workflows run in separate GitHub Action jobs. Doing it this way will increase your GitHub Actions minutes consumption rate but provides the ability to do things in parallel and quickly see which part of the build failed.

We may provide consolidated variants in the future which use fewer separate jobs.

dotnet-build.yml

Build the solution using dotnet build. The workspace is then persisted into a short-lived artifact and the NuGet packages are cached for later build steps.

dotnet-test.yml

Using the persisted workspace and cached packages from the build step, run dotnet test against the solution. It produces a test results artifact in the 'trx' format.

dotnet-pack.yml

Use dotnet pack to create .[s]nupkg files for all projects in the solution which have <IsPackable>true</IsPackable> (which is the default). Projects that should not be packed should set that to false in the .csproj file.

dotnet-publish.yml

Package up a single project directory into a ZIP file that an be deployed to Azure Web Apps.

.NET/NPM

Workflows that are useful when you have a .NET application with a JavaScript front-end that can be built/tested with npm run build / npm run test steps.

dotnet-npm-build.yml

Build the solution using dotnet build against the solution and run npm run build in a single project folder. The workspace is then persisted into a short-lived artifact and the NuGet/NPM packages are cached for later build steps.

dotnet-npm-test.yml

Using the persisted workspace and cached packages from the build step, run dotnet test against the solution and run npm run test in a single project folder. It produces a dotnet test results artifact in the 'trx' format.

There is no way to skip test running. If you have no NPM tests, and won't be adding any, set the 'test' script in package.json to the no-op "exit 0".

NPM

Scripts for executing the NPM build / test / pack cycle. These workflows run in separate GitHub Action jobs. Doing it this way will increase your GitHub Actions minutes consumption rate but provides the ability to do things in parallel and quickly see which part of the build failed.

We may provide consolidated variants in the future which use fewer separate jobs.

Examples

Sequence Diagrams

Each box is a separate job within the GitHub Actions workflow.

PR Build Sequence

flowchart LR
    version["Calculate Version
    (Use PR Labels)"]
    build["NPM Build"]
    test["NPM Test"]
    pack["NPM Pack"]
    build --> test
    test --> pack
    version --> pack
Loading

PR Merge Sequence

flowchart LR
    version["Calculate Version
    (Use PR Labels)"]
    build["NPM Build"]
    test["NPM Test"]
    pack["NPM Pack"]
    build --> test
    test --> pack
    version --> pack
    generate-token["Generate Token"]
    generate-token --> create-tag
    create-tag["Create Tag"]
    version --> create-tag
    pack --> create-tag
Loading

Release on Tag Sequence

flowchart LR
    verify-tag["Verify that tag
    is on correct
    branch."]
    verify-tag --> version
    verify-tag --> build
    version["Extract Version
    (package.json)"]
    build["NPM Build"]
    test["NPM Test"]
    pack["NPM Pack"]
    build --> test
    test --> pack
    version --> pack
    pack --> publish-myget["MyGet"]
    pack --> publish-github["GitHub"]
    pack --> publish-npm["NPM"]
    pack --> release-notes["Create Release Notes"]
Loading

calculate-version-with-npm-version-using-pr-labels.yml

Calculate the version by starting with an NPM package.json file and then looking at labels applied to the pull request (PR). Once the version is calculated this workflow returns ouput variables that can be used in later jobs. There is support for package.json files that are named otherwise or do not exist at the root of the repository.

extract-version-from-npm-package-json.yml

Extract the version from the NPM package.json file. Once the version is extracted this workflow returns ouput variables that can be used in later jobs. There is support for package.json files that are named otherwise or do not exist at the root of the repository.

npm-build.yml

Execute 'npm run build' against the package.json file. There is support for package.json files that are named otherwise or do not exist at the root of the repository. This workflow only runs the build script.

At the end of the job, it bundles up the entire workspace into a .tgz file for use in later jobs. This reduces the amount of time needed to run tests, security scans, or the 'npm pack' command later on.

The disadvantage of separate jobs is cost. You are billed a minimum of one minute of runtime for every job in a workflow. But the advantage is the ability to perform job steps in parallel (such as tests) and process isolation.

npm-create-version-tag.yml

Executes npm version in a way that creates a git commit and also tags that commit. The only file that should change during this step is the 'package.json' file. The commit and tag are then pushed to the main git repo for future use by other PRs.

There is support for package.json files that are named otherwise or do not exist at the root of the repository.

Requires the use of a GitHub App token in order to perform the push. This is because we need to bypass any branch protection and/or rulesets on the branch.

npm-pack.yml

Execute npm pack against the package.json file. There is support for package.json files that are named otherwise or do not exist at the root of the repository. This workflow only runs the pack command.

The resulting artifact (a .tgz file) will be uploaded to the workflow run and output variables provide information on the archive name (artifact_name) and file path (artifact_file_path).

npm-packages-pr-build.yml

Designed to be called when a PR is opened against a branch in the repository. This operates with a read-only GitHub Token for safety. See the PR Build Sequence diagram.

npm-packages-pr-create-tag.yml

Designed to be called when a PR is merged against a branch in the repository. It creates a new commit and tag using the a GitHub App token upon completion. See the PR Merge Sequence diagram.

npm-packages-release-on-tag.yml

Designed to be called when a tag is created on a specific branch in the repository. Tags which are not created on the correct branch will result in workflow failure. See the Release on Tag Sequence diagram.

npm-publish-to-github-packages.yml

Publish the NPM package to the GitHub Packages registry. The artifact (.tgz) file is pulled from the artifacts on the workflow run.

npm-publish-to-myget.yml

Publish the NPM package to the MyGet NPM registry. The artifact (.tgz) file is pulled from the artifacts on the workflow run.

npm-test.yml

Execute 'npm run test' against the package.json file. There is support for package.json files that are named otherwise or do not exist at the root of the repository. This workflow only runs the test script.

Note: This restores the workspace created in the npm-build.yml job before executing the test script.