Skip to content

Commit

Permalink
Write GitHub Actions workflow files in Kotlin instead of Yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
Vampire committed Apr 12, 2023
1 parent e97e2ed commit c3ed268
Show file tree
Hide file tree
Showing 13 changed files with 1,004 additions and 260 deletions.
153 changes: 153 additions & 0 deletions .github/workflows/branches-and-prs.main.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#!/usr/bin/env kotlin

/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

@file:DependsOn("io.github.typesafegithub:github-workflows-kt:0.40.1")

import io.github.typesafegithub.workflows.actions.actions.CheckoutV3
import io.github.typesafegithub.workflows.actions.codecov.CodecovActionV3
import io.github.typesafegithub.workflows.actions.gradle.GradleBuildActionV2
import io.github.typesafegithub.workflows.domain.Concurrency
import io.github.typesafegithub.workflows.domain.RunnerType
import io.github.typesafegithub.workflows.domain.actions.CustomAction
import io.github.typesafegithub.workflows.domain.triggers.PullRequest
import io.github.typesafegithub.workflows.domain.triggers.Push
import io.github.typesafegithub.workflows.dsl.expressions.Contexts
import io.github.typesafegithub.workflows.dsl.expressions.expr
import io.github.typesafegithub.workflows.dsl.workflow
import io.github.typesafegithub.workflows.yaml.writeToFile

workflow(
name = "Verify Branches and PRs",
on = listOf(
Push(
branchesIgnore = listOf(
"master",
"gh-pages"
)
),
PullRequest(
branches = listOf("*")
)
),
sourceFile = __FILE__.toPath(),
//# https://stackoverflow.com/a/72408109/16358266
concurrency = Concurrency(
group = "${expr { github.workflow }}-${expr("${Contexts.github.eventPullRequest.pull_request.number} || ${Contexts.github.ref}")}",
cancelInProgress = true
)
) {
job(
id = "build-and-verify",
name = "Build and Verify",
runsOn = RunnerType.Custom(expr("matrix.os")),
_customArguments = mapOf(
"strategy" to mapOf(
"fail-fast" to false,
"matrix" to mapOf(
"os" to listOf("ubuntu-latest"),
"variant" to listOf("2.5", "3.0", "4.0"),
"java" to listOf("8", "11", "17"),
"exclude" to listOf(
mapOf(
"os" to "ubuntu-latest",
"variant" to "2.5",
"java" to "17"
)
),
"include" to listOf(
mapOf(
"os" to "windows-latest",
"variant" to "2.5",
"java" to "8"
),
mapOf(
"os" to "windows-latest",
"variant" to "3.0",
"java" to "8"
),
mapOf(
"os" to "windows-latest",
"variant" to "4.0",
"java" to "8"
),
mapOf(
"os" to "macos-latest",
"variant" to "2.5",
"java" to "8"
),
mapOf(
"os" to "macos-latest",
"variant" to "3.0",
"java" to "8"
),
mapOf(
"os" to "macos-latest",
"variant" to "4.0",
"java" to "8"
)
)
)
)
)
) {
uses(
name = "Checkout Repository",
action = CheckoutV3(
// Codecov needs fetch-depth > 1
fetchDepth = CheckoutV3.FetchDepth.Value(2)
)
)
uses(
name = "Set up JDKs",
action = CustomAction(
actionOwner = ".github",
actionName = "actions/setup-build-env",
actionVersion = "v0",
inputs = mapOf(
"additional-java-version" to expr("matrix.java")
)
),
_customArguments = mapOf(
"uses" to "./.github/actions/setup-build-env"
)
)
val SPOCK_BUILD_CACHE_USERNAME by Contexts.secrets
val SPOCK_BUILD_CACHE_PASSWORD by Contexts.secrets
val GRADLE_ENTERPRISE_ACCESS_KEY by Contexts.secrets
uses(
name = "Build Spock",
action = GradleBuildActionV2(
arguments = """--no-parallel --stacktrace ghActionsBuild "-Dvariant=${expr("matrix.variant")}" "-DjavaVersion=${
expr(
"matrix.java"
)
}""""
),
// secrets are not injected for pull requests
env = linkedMapOf(
"ORG_GRADLE_PROJECT_spockBuildCacheUsername" to expr(SPOCK_BUILD_CACHE_USERNAME),
"ORG_GRADLE_PROJECT_spockBuildCachePassword" to expr(SPOCK_BUILD_CACHE_PASSWORD),
"GRADLE_ENTERPRISE_ACCESS_KEY" to expr(GRADLE_ENTERPRISE_ACCESS_KEY)
)
)
uses(
name = "Upload to Codecov.io",
action = CodecovActionV3()
)
}
}.writeToFile()
94 changes: 94 additions & 0 deletions .github/workflows/branches-and-prs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# This file was generated using Kotlin DSL (.github/workflows/branches-and-prs.main.kts).
# If you want to modify the workflow, please change the Kotlin file and regenerate this YAML file.
# Generated with https://github.com/typesafegithub/github-workflows-kt

name: Verify Branches and PRs
on:
push:
branches-ignore:
- master
- gh-pages
pull_request:
branches:
- '*'
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
check_yaml_consistency:
name: Check YAML consistency
runs-on: ubuntu-latest
steps:
- id: step-0
name: Check out
uses: actions/checkout@v3
- id: step-1
name: Execute script
run: rm '.github/workflows/branches-and-prs.yaml' && '.github/workflows/branches-and-prs.main.kts'
- id: step-2
name: Consistency check
run: git diff --exit-code '.github/workflows/branches-and-prs.yaml'
build-and-verify:
name: Build and Verify
runs-on: ${{ matrix.os }}
needs:
- check_yaml_consistency
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
variant:
- 2.5
- 3.0
- 4.0
java:
- 8
- 11
- 17
exclude:
- os: ubuntu-latest
variant: 2.5
java: 17
include:
- os: windows-latest
variant: 2.5
java: 8
- os: windows-latest
variant: 3.0
java: 8
- os: windows-latest
variant: 4.0
java: 8
- os: macos-latest
variant: 2.5
java: 8
- os: macos-latest
variant: 3.0
java: 8
- os: macos-latest
variant: 4.0
java: 8
steps:
- id: step-0
name: Checkout Repository
uses: actions/checkout@v3
with:
fetch-depth: 2
- id: step-1
name: Set up JDKs
uses: ./.github/actions/setup-build-env
with:
additional-java-version: ${{ matrix.java }}
- id: step-2
name: Build Spock
uses: gradle/gradle-build-action@v2
with:
arguments: --no-parallel --stacktrace ghActionsBuild "-Dvariant=${{ matrix.variant }}" "-DjavaVersion=${{ matrix.java }}"
env:
ORG_GRADLE_PROJECT_spockBuildCacheUsername: ${{ secrets.SPOCK_BUILD_CACHE_USERNAME }}
ORG_GRADLE_PROJECT_spockBuildCachePassword: ${{ secrets.SPOCK_BUILD_CACHE_PASSWORD }}
GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}
- id: step-3
name: Upload to Codecov.io
uses: codecov/codecov-action@v3
68 changes: 0 additions & 68 deletions .github/workflows/branches-and-prs.yml

This file was deleted.

Loading

0 comments on commit c3ed268

Please sign in to comment.