Skip to content
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

Use pkl-gha for GitHub Action Workflows #47

Merged
merged 20 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 0 additions & 10 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: /
schedule:
interval: weekly
groups:
actions-minor:
update-types:
- minor
- patch

- package-ecosystem: npm
directory: /
schedule:
Expand Down
42 changes: 42 additions & 0 deletions .github/pkl-workflows/check-actions.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
amends "package://pkg.pkl-lang.org/github.com/stefma/pkl-gha/[email protected]#/GitHubAction.pkl"
import "modules/Steps.pkl"

name = "Check Pkl Actions Converted"

on {
pull_request {}
push {
branches {
"main"
}
}
}

permissions {
contents = "read"
}

jobs {
["check-actions"] {
name = "Check Actions converted"
`runs-on` = "ubuntu-latest"
steps {
...Steps.checkoutAndSetupNode
new {
name = "Install pkl"
uses = "pkl-community/setup-pkl@v0"
with {
["pkl-version"] = "0.26.3"
}
}
new {
name = "Convert pkl actions to yaml"
run = "npm run gen:actions"
}
new {
name = "Verify if pkl actions are converted"
run = "git diff --exit-code"
}
}
}
}
68 changes: 68 additions & 0 deletions .github/pkl-workflows/check-dist.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
amends "package://pkg.pkl-lang.org/github.com/stefma/pkl-gha/[email protected]#/GitHubAction.pkl"
import "modules/Steps.pkl"

// In TypeScript actions, `dist/` is a special directory. When you reference
// an action with the `uses:` property, `dist/index.js` is the code that will be
// run. For this project, the `dist/index.js` file is transpiled from other
// source files. This workflow ensures the `dist/` directory contains the
// expected transpiled code.
//
// If this workflow is run from a feature branch, it will act as an additional CI
// check and fail if the checked-in `dist/` directory does not match what is
// expected from the build.
name = "Check Transpiled JavaScript"

on {
pull_request {}
push {
branches {
"main"
}
}
}

permissions {
contents = "read"
}

jobs {
["check-dist"] {
name = "Check Dist"
`runs-on` = "ubuntu-latest"
steps {
...Steps.checkoutAndSetupNode
new {
name = "Install pkl"
uses = "pkl-community/setup-pkl@v0"
with {
["pkl-version"] = "0.26.3"
}
}
new {
name = "Build dist/ Directory"
run = "npm run bundle"
}
// This will fail the workflow if the PR wasn't created by Dependabot.
new {
name = "Compare Directories"
id = "diff"
run = """
if [ "$(git diff --ignore-space-at-eol --text dist/ | wc -l)" -gt "0" ]; then
echo "Detected uncommitted changes after build. See status below:"
git diff --ignore-space-at-eol --text dist/
exit 1
fi
"""
}
new {
name = "Upload Artifact"
`if` = "${{ failure() && steps.diff.outcome == 'failure' }}"
uses = "actions/upload-artifact@v4"
with {
["name"] = "dist"
["path"] = "dist"
}
}
}
}
}
80 changes: 80 additions & 0 deletions .github/pkl-workflows/ci.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
amends "package://pkg.pkl-lang.org/github.com/stefma/pkl-gha/[email protected]#/GitHubAction.pkl"
import "modules/Steps.pkl"

name = "Continuous Integration"

env {
["CHECK_VERSION"] = "0.26.0"
}

on {
pull_request {}
push {
branches {
"main"
}
}
}

permissions {
contents = "read"
}

jobs {
["test-typescript"] {
name = "TypeScript Tests"
`runs-on` = "ubuntu-latest"
steps {
...Steps.checkoutAndSetupNode
new {
name = "Check Format"
run = "npm run format:check"
}
new {
name = "Lint"
run = "npm run lint"
}
new {
name = "Test"
run = "npm run ci-test"
}
}
}
["test-action"] {
strategy {
matrix {
["os"] = new {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this has been typed correctly in your base module, you shouldn't need = new {} to create a new list, the override syntax ["os"] { ... } should work. The only reason to do that typically with a Listing would be if there are default values in the parent object and you want to "start fresh" with a new Listing. Let me go check the typing in your parent module.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at https://github.com/StefMa/pkl-gha/blob/main/GitHubAction.pkl#L592, I think there's extra complexity because matrix is typed as Listing<String|*Dynamic>. Can it really be a Dynamic object?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it can be Dynamic.
See https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions#example-using-a-multi-dimension-matrix

A variable configuration in a matrix can be an array of objects.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha - to me that looks more like it could be Mapping<String, String> - remember that Dynamic can contain properties, entries, AND elements, eg:

new {
  "an entry"
  prop1 = "value"
  ["entry one"] = "value2"
}

Note that this is actually not representable in JSON or YAML at all (below is from the REPL):

pkl> x = new {
>   "an entry"
>   prop1 = "value"
>   ["entry one"] = "value2"
> }
pkl> (new JsonRenderer {}).renderDocument(x)
–– Pkl Error ––
Cannot render object with both elements and properties/entries as JSON.
Object: new Dynamic { prop1 = "value"; ["entry one"] = "value2"; "an entry" }

But that's an aside, not important to this PR. Just info for you and your library

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the hint!
Do you now how I could model this better/differently? 🤔

"ubuntu-latest"
"macos-13" // macos-13 is amd64
"macos-14" // macos-14 is aarch64 (M1)
"windows-latest"
}
}
}
`runs-on` = "${{ matrix.os }}"
steps {
Steps.checkout
new {
name = "Test Local Action"
uses = "./"
with {
["pkl-version"] = "${{ env.CHECK_VERSION }}"
}
}
new {
name = "Confirm download (unix)"
`if` = "matrix.os != 'windows-latest'"
run = """
pkl --version | grep "Pkl ${{ env.CHECK_VERSION }}"
"""
}
new {
name = "Confirm download (windows)"
`if` = "matrix.os == 'windows-latest'"
run = """
.github/pkl-workflows/Check-Version.ps1 "Pkl ${{ env.CHECK_VERSION }}"
"""
}
}
}
}
48 changes: 48 additions & 0 deletions .github/pkl-workflows/linter.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
amends "package://pkg.pkl-lang.org/github.com/stefma/pkl-gha/[email protected]#/GitHubAction.pkl"
import "modules/Steps.pkl"

name = "Lint Codebase"

on {
pull_request {}
push {
branches {
"main"
}
}
}

permissions {
contents = "read"
packages = "read"
statuses = "write"
}

jobs {
["test-typescript"] {
name = "Lint Codebase"
`runs-on` = "ubuntu-latest"
steps {
(Steps.checkout) {
with {
["fetch-depth"] = 0
}
}
Steps.installNode
Steps.installNodeDeps
new {
name = "Lint Codebase"
uses = "super-linter/super-linter/slim@v6"
env {
["DEFAULT_BRANCH"] = "main"
["FILTER_REGEX_EXCLUDE"] = "dist/**/*"
["GITHUB_TOKEN"] = "${{ secrets.GITHUB_TOKEN }}"
["VALIDATE_ALL_CODEBASE"] = "true"
["VALIDATE_JAVASCRIPT_STANDARD"] = "false"
["VALIDATE_JSCPD"] = "false"
["VALIDATE_TYPESCRIPT_STANDARD"] = "false"
}
}
}
}
}
26 changes: 26 additions & 0 deletions .github/pkl-workflows/modules/Steps.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import "package://pkg.pkl-lang.org/github.com/stefma/pkl-gha/[email protected]#/GitHubAction.pkl"

checkout: GitHubAction.Step = new {
name = "Checkout"
uses = "actions/checkout@v4"
}

installNode: GitHubAction.Step = new {
name = "Install node"
uses = "actions/setup-node@v4"
with {
["node-version-file"] = ".nvmrc"
["cache"] = "npm"
}
}

installNodeDeps: GitHubAction.Step = new {
name = "Install Dependencies"
run = "npm ci"
}

checkoutAndSetupNode: Listing<GitHubAction.Step> = new {
checkout
installNode
installNodeDeps
}
33 changes: 33 additions & 0 deletions .github/workflows/check-actions.generated.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Do not modify!
# This file was generated from a template using https://github.com/StefMa/pkl-gha

name: Check Pkl Actions Converted
'on':
pull_request: {}
push:
branches:
- main
permissions:
contents: read
jobs:
check-actions:
name: Check Actions converted
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install node
uses: actions/setup-node@v4
with:
node-version-file: .nvmrc
cache: npm
- name: Install Dependencies
run: npm ci
- name: Install pkl
uses: pkl-community/setup-pkl@v0
with:
pkl-version: 0.26.3
- name: Convert pkl actions to yaml
run: npm run gen:actions
- name: Verify if pkl actions are converted
run: git diff --exit-code
45 changes: 45 additions & 0 deletions .github/workflows/check-dist.generated.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Do not modify!
# This file was generated from a template using https://github.com/StefMa/pkl-gha

name: Check Transpiled JavaScript
'on':
pull_request: {}
push:
branches:
- main
permissions:
contents: read
jobs:
check-dist:
name: Check Dist
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install node
uses: actions/setup-node@v4
with:
node-version-file: .nvmrc
cache: npm
- name: Install Dependencies
run: npm ci
- name: Install pkl
uses: pkl-community/setup-pkl@v0
with:
pkl-version: 0.26.3
- name: Build dist/ Directory
run: npm run bundle
- name: Compare Directories
id: diff
run: |-
if [ "$(git diff --ignore-space-at-eol --text dist/ | wc -l)" -gt "0" ]; then
echo "Detected uncommitted changes after build. See status below:"
git diff --ignore-space-at-eol --text dist/
exit 1
fi
- name: Upload Artifact
if: ${{ failure() && steps.diff.outcome == 'failure' }}
uses: actions/upload-artifact@v4
with:
name: dist
path: dist
Loading
Loading