Skip to content

Commit

Permalink
Add publishing workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
bglw committed Oct 11, 2024
1 parent 8a861ed commit 1d3e9a9
Show file tree
Hide file tree
Showing 10 changed files with 349 additions and 2 deletions.
56 changes: 56 additions & 0 deletions .backstage/changelog.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const fs = require("fs");
const path = require("path");

const version = process.env.GIT_VERSION;
const changelogFile = path.join(__dirname, "../CHANGELOG.md");
const releaseFile = path.join(__dirname, "../RELEASE.md");

const err = (m) => {
console.error(m);
process.exit(1);
}

const date = () => {
let options = { year: 'numeric', month: 'long', day: 'numeric' };
return new Date().toLocaleString('en-US', options);
}

if (!version) err("Script expected a GIT_VERSION environment variable");

if (!fs.existsSync(changelogFile)) err(`Script expected a file at ${changelogFile}`);

let contents = fs.readFileSync(changelogFile, { encoding: "utf-8" });
let release = [], lines = contents.split(/\n/g);
let it = lines.entries();

while (!(entry = it.next()).done) {
let [num, line] = entry.value;
// Read until we reach our unreleased changelog section.
if (/^\s*## Unreleased\s*$/.test(line)) {
let releaseHeader = `## v${version} (${date()})`;
lines[num] = `## Unreleased\n\n${releaseHeader}`;
break;
}
}


while (!(entry = it.next()).done) {
let [, line] = entry.value;
// Read until we reach the section for a new version.
if (/^\s*##\s+v/i.test(line)) {
break;
}
release.push(line);
}

if (!release.some((v => v.trim().length))) {
err([
`No unreleased changes exist in ${changelogFile}.`,
`Cancelling release — please write release notes!`
].join('\n'));
}

if (process.argv[2] === "write") {
fs.writeFileSync(releaseFile, release.join('\n'));
fs.writeFileSync(changelogFile, lines.join('\n'));
}
22 changes: 22 additions & 0 deletions .backstage/get_tag.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const version = process.env.GIT_VERSION;

if (!version) {
console.error("Script expected a GIT_VERSION environment variable");
process.exit(1);
}

// Only allow latest tag if we are releasing a major/minor/patch
if (/^\d+\.\d+\.\d+$/.test(version)) {
console.log("latest");
process.exit(0);
}

// Use the suffix as the tag. i.e. `0.11.0-rc5` -> `rc`
const suffix = version.match(/^\d+\.\d+\.\d+-([a-z]+)/i)?.[1];
if (suffix) {
console.log(suffix.toLowerCase());
process.exit(0);
}

// Fall back to an unknown tag for safety
console.log("unknown");
28 changes: 28 additions & 0 deletions .backstage/version.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const fs = require("fs");
const path = require("path");

const version = process.env.GIT_VERSION;
const version_re = /"?version"?\s*[=:]\s*"0.0.0"/;

const err = (m) => {
console.error(m);
process.exit(1);
};

if (!version) err("Script expected a GIT_VERSION environment variable");

const file = (localPath) => {
localPath = path.join(__dirname, localPath);
if (!fs.existsSync(localPath)) err(`Script expected a file at ${localPath}`);
const contents = fs.readFileSync(localPath, { encoding: "utf-8" });
if (!version_re.test(contents))
err(`Expected ${localPath} to contain a version of "0.0.0"`);
return { path: localPath, contents };
};

let matterhornCfg = file("../Cargo.toml");
matterhornCfg.contents = matterhornCfg.contents.replace(
version_re,
`version = "${version}"`,
);
fs.writeFileSync(matterhornCfg.path, matterhornCfg.contents);
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "cargo"
directory: "/"
open-pull-requests-limit: 1
schedule:
interval: "daily"
26 changes: 26 additions & 0 deletions .github/workflows/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: auto-merge

on: pull_request_target

jobs:
auto-merge:
runs-on: ubuntu-latest
if: ${{ github.actor == 'dependabot[bot]' }}
steps:
- uses: actions/checkout@v4
- name: Get Token
id: get_workflow_token
uses: peter-murray/workflow-application-token-action@v2
with:
application_id: ${{ secrets.CC_OSS_BOT_ID }}
application_private_key: ${{ secrets.CC_OSS_BOT_PEM }}
- uses: fastify/github-action-merge-dependabot@v3
with:
github-token: ${{ steps.get_workflow_token.outputs.token }}
target: minor
approve-only: true
- name: Enable auto-merge for Dependabot PR
run: gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{ github.event.pull_request.html_url }}
GITHUB_TOKEN: ${{ steps.get_workflow_token.outputs.token }}
157 changes: 157 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
name: Test

on:
push:
branches: [main]
pull_request:
branches: [main]

env:
CARGO_TERM_COLOR: always

jobs:
publish-crate:
name: Publish Crate
runs-on: ubuntu-20.04
needs: publish-github-release
steps:
- name: Clone
uses: actions/checkout@v4
- name: Cache
uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
~/.rustup
target
key: ${{ runner.os }}-stable-min165

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
default: true
components: rustfmt, clippy

- name: Get Version
run: echo GIT_VERSION="$(git describe --tags | sed 's/^v\(.*\)$/\1/')" >> $GITHUB_ENV
- name: Prepare Git
run: |
git config user.email "[email protected]"
git config user.name "Github Actions"
git checkout -b main
# Use throw-away branch so we don't push the changes to origin
git checkout -b deploy_branch
- name: Prepare Crates
run: |
# Update cargo version,
node ./.backstage/version.cjs
git add ./Cargo.toml
# Commit changes so cargo doesn't complain about dirty repo
git commit -m "Deploy changes."
- name: Publish
run: cargo publish --allow-dirty
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

publish-github-release:
name: GitHub Release
runs-on: ubuntu-20.04
needs: test
steps:
- name: Get Token
id: get_workflow_token
uses: peter-murray/workflow-application-token-action@v2
with:
application_id: ${{ secrets.CC_OSS_BOT_ID }}
application_private_key: ${{ secrets.CC_OSS_BOT_PEM }}

- name: Clone
uses: actions/checkout@v4
with:
token: ${{ steps.get_workflow_token.outputs.token }}

- name: Swap to main
uses: actions/checkout@v4
with:
ref: main
fetch-depth: 0 # Full fetch
token: ${{ steps.get_workflow_token.outputs.token }}

- name: Get Version
run: echo GIT_VERSION="$(git describe --tags | sed 's/^v\(.*\)$/\1/')" >> $GITHUB_ENV
- name: Get Tag
run: echo GIT_TAG="$(node ./.backstage/get_tag.cjs)" >> $GITHUB_ENV

- name: Build CHANGELOG
if: env.GIT_TAG == 'latest'
run: |
node ./.backstage/changelog.cjs write
echo CHANGELOG=\"$(base64 -w 0 -i CHANGELOG.md)\" >> $GITHUB_ENV
echo SHA=\"$( git rev-parse main:CHANGELOG.md )\" >> $GITHUB_ENV
- name: Build CHANGELOG
if: env.GIT_TAG != 'latest'
run: |
echo "## Prerelease" > RELEASE.md
node ./.backstage/changelog.cjs write || true
- name: Commit new CHANGELOG
uses: octokit/[email protected]
if: env.GIT_TAG == 'latest'
id: push_changes
with:
route: PUT /repos/{owner}/{repo}/contents/CHANGELOG.md
owner: cloudcannon
repo: matterhorn
branch: main
message: Changelog for ${{ env.GIT_VERSION }}
sha: ${{ env.SHA }}
content: ${{ env.CHANGELOG }}
env:
GITHUB_TOKEN: ${{ steps.get_workflow_token.outputs.token }}

- name: Release
uses: softprops/action-gh-release@v1
with:
repository: cloudcannon/matterhorn
prerelease: false
body_path: RELEASE.md
env:
GITHUB_TOKEN: ${{ steps.get_workflow_token.outputs.token }}

test:
name: Test
runs-on: ubuntu-20.04
defaults:
run:
shell: bash
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 1

- name: Cache
uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
~/.rustup
target
key: ${{ runner.os }}-${{ matrix.rust }}-min165

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
target: ${{ matrix.target }}
override: true
default: true
components: rustfmt, clippy

- name: Test Lib
run: cargo test
44 changes: 44 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Release

on:
push:
tags:
- v*

env:
CARGO_TERM_COLOR: always

jobs:
test:
name: Test
runs-on: ubuntu-20.04
defaults:
run:
shell: bash
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 1

- name: Cache
uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
~/.rustup
target
key: ${{ runner.os }}-${{ matrix.rust }}-min165

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
target: ${{ matrix.target }}
override: true
default: true
components: rustfmt, clippy

- name: Test Lib
run: cargo test
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@

## Unreleased

## v0.1.0 (October 11, 2024)

* Initial release
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "matterhorn"
version = "0.1.0"
version = "0.0.0"
edition = "2021"
description = "A lenient front matter parsing crate that supports files prefixed with YAML, JSON, and TOML front matter."
license = "MIT"
Expand Down

0 comments on commit 1d3e9a9

Please sign in to comment.