From 457992e1292fa885f54b67a481120aecf68ebaee Mon Sep 17 00:00:00 2001 From: Cody Worsnop Date: Thu, 14 Apr 2022 07:56:37 -0700 Subject: [PATCH] githubReleases + semversioning --- .github/workflows/docker.yaml | 57 ------------------ .github/workflows/release.yaml | 61 +++++++++++++++++++ build.gradle.kts | 104 +++++++++++++++++++++++++++++---- 3 files changed, 154 insertions(+), 68 deletions(-) delete mode 100644 .github/workflows/docker.yaml create mode 100644 .github/workflows/release.yaml diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml deleted file mode 100644 index d8dc6545..00000000 --- a/.github/workflows/docker.yaml +++ /dev/null @@ -1,57 +0,0 @@ -name: Docker - -on: - push: - branches-ignore: # Should publish on main and regular branches, but not release/ or tagged branches - - 'release/**' - - 'hotfix/**' - - "v[0-9]+.[0-9]+.[0-9]+" # branches matching v*, i.e. v1.0, v20.15.10 - - "v[0-9]+.[0-9]+.[0-9]+-rc*" # branches matching v*, i.e. v1.0-rc1, v20.15.10-rc5 - tags: - - "v[0-9]+.[0-9]+.[0-9]+" # Push events to matching v*, i.e. v1.0, v20.15.10 - - "v[0-9]+.[0-9]+.[0-9]+-rc*" # Push events to matching v*, i.e. v1.0-rc1, v20.15.10-rc5 - -jobs: - docker: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Set up JDK 11 - uses: actions/setup-java@v1 - with: - java-version: 11 - - name: Prepare - id: prep - run: | - DOCKER_IMAGE=provenanceio/p8e-cee-api - VERSION=noop - if [[ $GITHUB_REF == refs/tags/* ]]; then - VERSION=${GITHUB_REF#refs/tags/} - elif [[ $GITHUB_REF == refs/heads/* ]]; then - VERSION=$(echo ${GITHUB_REF#refs/heads/} | sed -r 's#/+#-#g') - echo ::set-output name=branch::${VERSION} - if [ "${{ github.event.repository.default_branch }}" = "$VERSION" ]; then - VERSION=latest - fi - fi - TAGS="${DOCKER_IMAGE}:${VERSION}" - if [[ $VERSION =~ ^v[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then - TAGS="$TAGS,${DOCKER_IMAGE}:${VERSION}" - fi - echo ::set-output name=version::${VERSION} - echo ::set-output name=tags::${TAGS} - echo ::set-output name=created::$(date -u +'%Y-%m-%dT%H:%M:%SZ') - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 - - name: Login to DockerHub - uses: docker/login-action@v1 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - - name: Build and push service docker - uses: docker/build-push-action@v2 - with: - file: service/docker/Dockerfile - context: . - push: true - tags: ${{ steps.prep.outputs.tags }} diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 00000000..97b58fe3 --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,61 @@ +name: Release + +on: + workflow_dispatch: + inputs: + versionModifier: + description: 'Version Modifier' + default: 'patch' + type: choice + required: false + options: + - 'patch' + - 'minor' + - 'major' +jobs: + docker: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: Set up JDK 11 + uses: actions/setup-java@v1 + with: + java-version: 11 + - name: Build with Gradle + run: ./gradlew -i clean build --refresh-dependencies :generateVersionFile githubRelease + if: ${{ github.ref_name == 'main' }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Create Outputs + run: |- + echo "::set-output name=semVersion::$(cat build/semver/version.txt | head -1)" + echo "::set-output name=semVersionTag::$(cat build/semver/version.txt | tail -1)" + id: ci-release-create-outputs + shell: bash + - name: Prepare + id: prep + run: | + DOCKER_IMAGE=provenanceio/p8e-cee-api + VERSION={{ steps.ci-release-create-outputs.outputs.semVersion }} + TAGS=${{ steps.ci-release-create-outputs.outputs.semVersionTag }} + + echo ::set-output name=version::${VERSION} + echo ::set-output name=tags::${TAGS} + echo ::set-output name=created::$(date -u +'%Y-%m-%dT%H:%M:%SZ') + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - name: Login to DockerHub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Build and push service docker + uses: docker/build-push-action@v2 + with: + file: service/docker/Dockerfile + context: . + push: true + tags: ${{ steps.prep.outputs.tags }} diff --git a/build.gradle.kts b/build.gradle.kts index 5aff2073..a5b97420 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,18 +1,19 @@ +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile +import net.swiftzer.semver.SemVer + +buildscript { + dependencies { + classpath("net.swiftzer.semver:semver:1.1.2") + classpath("com.github.breadmoirai:github-release:2.2.12") + } +} + plugins { kotlin("jvm") version Versions.Kotlin id("java") id("maven-publish") -} - -allprojects { - val project = this - group = "io.provenance.p8e-cee-api" - version = this.findProperty("artifactVersion")?.toString() - ?: "1.0-snapshot" - - repositories { - mavenCentral() - } + id("com.github.breadmoirai.github-release") version "2.2.12" + id("io.github.nefilim.gradle.semver-plugin") version "0.3.10" } subprojects { @@ -45,3 +46,84 @@ java { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } + +semver { + tagPrefix("v") + initialVersion("0.1.0") + findProperty("semver.overrideVersion")?.toString()?.let { overrideVersion(it) } + val semVerModifier = findProperty("semver.modifier")?.toString()?.let { buildVersionModifier(it) } ?: { nextPatch() } + versionModifier(semVerModifier) +} + +/** + * The code below is a workaround for [gradle/gradle#20016](https://github.com/gradle/gradle/issues/20016) derived from + * [platform-portal@14319ed/docs/semver.md](https://github.com/FigureTechnologies/platform-portal/blob/14319ed7e97d88a1b8cbb2f2f7708cc0660dc518/docs/semver.md#limiting-dependencies-to-release-versions) + * to prevent version strings like `"1.0.+"` from resolving to pre-release versions. + * You can adjust the string set constants or comment the code out entirely to allow pre-release versions to be used. + */ + +val invalidQualifiers = setOf("alpha", "beta", "rc", "nightly") +val onlyReleaseArtifacts = setOf("p8e-cee-api") +val whiteListedMavenGroups = setOf("tech.figure", "io.provenance") + +configurations.all { + resolutionStrategy { + componentSelection { + all { + when { + ( + onlyReleaseArtifacts.any { candidate.moduleIdentifier.name.startsWith(it) } && + !candidate.version.toSemVer()?.preRelease.isNullOrEmpty() + ) -> { + reject("Rejecting prerelease version for OnlyReleaseArtifact[$candidate]") + } + ( + whiteListedMavenGroups.none { candidate.group.startsWith(it) } && + invalidQualifiers.any { candidate.version.contains(it) } + ) -> { + reject("Invalid qualifier versions for $candidate") + } + } + } + } + } +} + +fun String?.toSemVer(): SemVer? = + try { + this?.let { versionString -> + SemVer.parse(versionString) + } + } catch (e: Exception) { + project.logger.info("Failed to parse semantic version from string '$this'") + null + } + + +val semVersion = semver.version +allprojects { + val project = this + group = "io.provenance.p8e-cee-api" + version = semVersion + + repositories { + mavenCentral() + } +} + +val githubTokenValue = findProperty("githubToken")?.toString() ?: System.getenv("GITHUB_TOKEN") +githubRelease { + token(githubTokenValue) + owner("provenance-io") + targetCommitish("main") + draft(false) + prerelease(false) + repo("p8e-cee-api") + tagName(semver.versionTagName) + body(changelog()) + + overwrite(false) + dryRun(false) + apiEndpoint("https://api.github.com") + client +}