Skip to content

GitHub workflow improvements #1306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
28050ad
lint and action updates
nicktrn Sep 15, 2024
107b888
add custom image tag action
nicktrn Sep 15, 2024
fee02a4
remove publish infra bloat
nicktrn Sep 15, 2024
b2d1c3f
replace cancel action with native concurrency control
nicktrn Sep 15, 2024
90f2f78
ignore all docs changes for release trigger
nicktrn Sep 15, 2024
f58df38
image action updates
nicktrn Sep 15, 2024
eef94fa
update publish trigger paths
nicktrn Sep 15, 2024
8518b6f
simplify semver checks
nicktrn Sep 15, 2024
ce94909
make it possible to call publish
nicktrn Sep 15, 2024
cb87cb5
add tests for custom action
nicktrn Sep 15, 2024
d5b9167
rename publish workflows
nicktrn Sep 15, 2024
d36f047
call publish after successful package release
nicktrn Sep 15, 2024
5df1b7e
fix inputs and published package parsing
nicktrn Sep 16, 2024
483162a
clarify image tag action errors
nicktrn Sep 16, 2024
d2d0716
move test action
nicktrn Sep 16, 2024
db06c77
add test events
nicktrn Sep 16, 2024
19f4a3c
add changesets action mock
nicktrn Sep 16, 2024
404e18c
add docs checks
nicktrn Sep 16, 2024
7c57d0e
add github action test readme
nicktrn Sep 16, 2024
ca14c69
fix docs check on push
nicktrn Sep 16, 2024
bee7a77
update action versions in docs
nicktrn Sep 16, 2024
acab9a8
cache npm for docs check
nicktrn Sep 16, 2024
ef7e3d9
pretend to fix bad docs link
nicktrn Sep 16, 2024
a7508f5
fix docs link
nicktrn Sep 16, 2024
ec642af
Revert "fix docs link"
nicktrn Sep 16, 2024
698b244
Revert "Revert "fix docs link""
nicktrn Sep 16, 2024
066ed5a
improve caching
nicktrn Sep 16, 2024
2dafb71
Merge branch 'main' into workflow-improvements
nicktrn Sep 23, 2024
07e15af
disable automatic publishing after release
nicktrn Sep 23, 2024
0910b71
limit docs checks to pushes to main and PRs
nicktrn Sep 23, 2024
aded298
fix broken links
nicktrn Sep 23, 2024
65c7b89
prevent word splitting and globbing
nicktrn Sep 23, 2024
2b5e085
use latest checkout action everywhere
nicktrn Sep 23, 2024
0dcf8f2
correctly detect prereleases as valid semver
nicktrn Sep 23, 2024
30e94e2
update to latest cache action
nicktrn Sep 23, 2024
679ec0a
Merge branch 'main' into workflow-improvements
nicktrn Sep 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions .github/actions/get-image-tag/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: "#️⃣ Get image tag (action)"

description: This action gets the image tag from the commit ref or input (if provided)

outputs:
tag:
description: The image tag
value: ${{ steps.get_tag.outputs.tag }}
is_semver:
description: Whether the tag is a semantic version
value: ${{ steps.check_semver.outputs.is_semver }}

inputs:
tag:
description: The image tag. If this is set it will return the tag as is.
required: false
default: ""

runs:
using: "composite"
steps:
- name: "#️⃣ Get image tag (step)"
id: get_tag
shell: bash
run: |
if [[ -n "${{ inputs.tag }}" ]]; then
tag="${{ inputs.tag }}"
elif [[ "${{ github.ref_type }}" == "tag" ]]; then
if [[ "${{ github.ref_name }}" == infra-*-* ]]; then
env=$(echo ${{ github.ref_name }} | cut -d- -f2)
sha=$(echo ${{ github.sha }} | head -c7)
ts=$(date +%s)
tag=${env}-${sha}-${ts}
elif [[ "${{ github.ref_name }}" == v.docker.* ]]; then
version="${GITHUB_REF_NAME#v.docker.}"
tag="v${version}"
elif [[ "${{ github.ref_name }}" == build-* ]]; then
tag="${GITHUB_REF_NAME#build-}"
else
echo "Invalid git tag: ${{ github.ref_name }}"
exit 1
fi
elif [[ "${{ github.ref_name }}" == "main" ]]; then
tag="main"
else
echo "Invalid git ref: ${{ github.ref }}"
exit 1
fi
echo "tag=${tag}" >> "$GITHUB_OUTPUT"

- name: 🔍 Check for validity
id: check_validity
shell: bash
env:
tag: ${{ steps.get_tag.outputs.tag }}
run: |
if [[ "${tag}" =~ ^[a-z0-9]+([._-][a-z0-9]+)*$ ]]; then
echo "Tag is valid: ${tag}"
else
echo "Tag is not valid: ${tag}"
exit 1
fi

- name: 🆚 Check for semver
id: check_semver
shell: bash
env:
tag: ${{ steps.get_tag.outputs.tag }}
# Will match most semver formats except build metadata, i.e. v1.2.3+build.1
# Valid matches:
# v1.2.3
# v1.2.3-alpha
# v1.2.3-alpha.1
# v1.2.3-rc.1
# v1.2.3-beta-1
run: |
if [[ "${tag}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$ ]]; then
echo "Tag is a semantic version: ${tag}"
is_semver=true
else
echo "Tag is not a semantic version: ${tag}"
is_semver=false
fi
echo "is_semver=${is_semver}" >> "$GITHUB_OUTPUT"
70 changes: 70 additions & 0 deletions .github/test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# GitHub Action Tests

This directory contains necessary files to allow local testing of GitHub Actions workflows, composite actions, etc. You will need to install [act](https://github.com/nektos/act) to perform tests.

## Workflow tests

Trigger specific workflow files by specifying their full path:

```
act -W .github/workflow/release.yml
```

You will likely need to override any custom runners we use, e.g. buildjet. For example:

```
override=catthehacker/ubuntu:act-latest

act -W .github/workflow/release.yml \
-P buildjet-8vcpu-ubuntu-2204=$override

# override multiple images at the same time
act -W .github/workflow/release.yml \
-P buildjet-8vcpu-ubuntu-2204=$override \
-P buildjet-16vcpu-ubuntu-2204=$override
```

Trigger with specific event payloads to test pushing to branches or tags:

```
override=catthehacker/ubuntu:act-latest

# simulate push to main
act -W .github/workflow/publish.yml \
-P buildjet-8vcpu-ubuntu-2204=$override \
-P buildjet-16vcpu-ubuntu-2204=$override \
-e .github/events/push-tag-main.json

# simulate a `build-` prefixed tag
act -W .github/workflow/publish.yml \
-P buildjet-8vcpu-ubuntu-2204=$override \
-P buildjet-16vcpu-ubuntu-2204=$override \
-e .github/events/push-tag-buld.json
```

By default, `act` will send a push event. To trigger a different event:

```
# basic syntax
act <EVENT> ...

# simulate a pull request
act pull_request

# only trigger a specific workflow
act pull_request -W .github/workflow/pr_checks.yml
```

## Composite action tests

The composite (custom) action tests can be run by triggering the `test-actions` workflow:

```
act -W .github/test/test-actions.yml
```

## Helpful flags

- `--pull=false` - perform fully offline tests if all images are already present
- `-j <job_name>` - run the specified job only
- `-l push` - list all workflows with push triggers
3 changes: 3 additions & 0 deletions .github/test/events/push-main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ref": "refs/heads/main"
}
3 changes: 3 additions & 0 deletions .github/test/events/push-tag-build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ref": "refs/tags/build-buildtag"
}
3 changes: 3 additions & 0 deletions .github/test/events/push-tag-docker-nonsemver.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ref": "refs/tags/v.docker.nonsemver"
}
3 changes: 3 additions & 0 deletions .github/test/events/push-tag-docker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ref": "refs/tags/v.docker.1.2.3"
}
3 changes: 3 additions & 0 deletions .github/test/events/push-tag-infra-prod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ref": "refs/tags/infra-prod-anything"
}
3 changes: 3 additions & 0 deletions .github/test/events/push-tag-infra-test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ref": "refs/tags/infra-test-anything"
}
3 changes: 3 additions & 0 deletions .github/test/events/push-tag-semver.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ref": "refs/tags/1.2.3"
}
3 changes: 3 additions & 0 deletions .github/test/events/push-tag.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ref": "refs/tags/standard-tag"
}
152 changes: 152 additions & 0 deletions .github/test/test-actions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
name: Test Actions

on:
workflow_dispatch:

jobs:
get-image-tag-none:
runs-on: ubuntu-latest
continue-on-error: true
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Log current ref
run: |
echo "ref: ${{ github.ref }}"
echo "ref_type: ${{ github.ref_type }}"
echo "ref_name: ${{ github.ref_name }}"

- name: Run without input tag
id: get_tag
# this step may fail depending on the current ref
continue-on-error: true
uses: ./.github/actions/get-image-tag

- name: Verify output
run: |
echo "${{ toJson(steps.get_tag) }}"

get-image-tag-null:
runs-on: ubuntu-latest
continue-on-error: true
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Log current ref
run: |
echo "ref: ${{ github.ref }}"
echo "ref_type: ${{ github.ref_type }}"
echo "ref_name: ${{ github.ref_name }}"

- name: Run without input tag
id: get_tag
uses: ./.github/actions/get-image-tag
# this step may fail depending on the current ref
continue-on-error: true
with:
# this should behave exactly as when no tag is provided
tag: null

- name: Verify output
run: |
echo "${{ toJson(steps.get_tag) }}"

get-image-tag-override:
runs-on: ubuntu-latest
continue-on-error: true
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Run with tag override
id: get_tag
uses: ./.github/actions/get-image-tag
with:
tag: "abc-123"

- name: Verify output
run: |
echo "${{ toJson(steps.get_tag) }}"

get-image-tag-invalid-string:
runs-on: ubuntu-latest
continue-on-error: true
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Run with invalid string
id: get_tag
uses: ./.github/actions/get-image-tag
# this step is expected to fail
continue-on-error: true
with:
# does not end with alphanumeric character
tag: "abc-123-"

- name: Fail job if previous step did not fail
if: steps.get_tag.outcome != 'failure'
run: exit 1

- name: Verify output
run: |
echo "${{ toJson(steps.get_tag) }}"

get-image-tag-prerelease:
runs-on: ubuntu-latest
continue-on-error: true
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Run with prerelease semver
id: get_tag
uses: ./.github/actions/get-image-tag
with:
tag: "v1.2.3-beta.4"

- name: Verify output
run: |
echo "${{ toJson(steps.get_tag) }}"

get-image-tag-semver:
runs-on: ubuntu-latest
continue-on-error: true
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Run with basic semver
id: get_tag
uses: ./.github/actions/get-image-tag
with:
tag: "v1.2.3"

- name: Verify output
run: |
echo "${{ toJson(steps.get_tag) }}"

get-image-tag-invalid-semver:
runs-on: ubuntu-latest
continue-on-error: true
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Run with invalid semver
id: get_tag
uses: ./.github/actions/get-image-tag
# this step is expected to fail
continue-on-error: true
with:
tag: "v1.2.3-"

- name: Fail job if previous step did not fail
if: steps.get_tag.outcome != 'failure'
run: exit 1

- name: Verify output
run: |
echo "${{ toJson(steps.get_tag) }}"
42 changes: 42 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: 📚 Docs Checks

on:
push:
branches:
- main
paths:
- "docs/**"
pull_request:
types: [opened, synchronize, reopened]
paths:
- "docs/**"

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

permissions:
contents: read

jobs:
check-broken-links:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./docs
steps:
- name: 📥 Checkout repository
uses: actions/checkout@v4

- name: 📦 Cache npm
uses: actions/cache@v4
with:
path: |
~/.npm
key: |
${{ runner.os }}-mintlify
restore-keys: |
${{ runner.os }}-mintlify

- name: 🔗 Check for broken links
run: npx mintlify@4.0.222 broken-links
Loading
Loading