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

generate a build info class with version property #961

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 6 additions & 12 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ jobs:
steps:
- name: Git Checkout
uses: actions/checkout@v3
- name: Fetch Library version
id: vars
run: echo ::set-output name=libVersion::${GITHUB_REF#refs/*/}
- name: benchmark test
if: ${{ success() }}
run: ./gradlew clean setLibraryVersion benchmark
run: ./gradlew -Dbuild.version="${{ github.ref_name }}" clean benchmark
env:
GITHUB_TAG: ${{ steps.vars.outputs.libVersion }}
GITHUB_TAG: ${{ github.ref_name }}
deployment:
name: deployment
needs: benchmark_tests
Expand All @@ -39,22 +36,19 @@ jobs:
steps:
- name: Git Checkout
uses: actions/checkout@v3
- name: Set output
id: vars
run: echo ::set-output name=tag::${GITHUB_REF#refs/*/}
- name: Set up Java
uses: actions/setup-java@v3
with:
java-version: 11
distribution: 'zulu'
- name: status
run: echo Build is tagged. Uploading artifact ${{ steps.vars.outputs.tag }} to maven central.
run: echo Build is tagged. Uploading artifact ${{ github.ref_name }} to maven central.
- name: Publish GitHub Pages
run: ./gradlew --info -Dbuild.version="${{ steps.vars.outputs.tag }}" mkdocsPublish
run: ./gradlew --info -Dbuild.version="${{ github.ref_name }}" mkdocsPublish
- name: deploy to sonatype and publish to maven central
run: ./gradlew setLibraryVersion -Dbuild.version="${{ steps.vars.outputs.tag }}" publishToSonatype closeAndReleaseSonatypeStagingRepository
run: ./gradlew -Dbuild.version="${{ github.ref_name }}" publishToSonatype closeAndReleaseSonatypeStagingRepository
env:
GITHUB_TAG: ${{ steps.vars.outputs.tag }}
GITHUB_TAG: ${{ github.ref_name }}
MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }}
PGP_KEY: ${{ secrets.PGP_KEY }}
Expand Down
9 changes: 3 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
java-version: 11
distribution: 'zulu'
- name: Build with Gradle
run: ./gradlew clean setLibraryVersion test integrationTest jacocoTestCoverageVerification jacocoTestReport
run: ./gradlew clean test integrationTest jacocoTestCoverageVerification jacocoTestReport
env:
SOURCE_PROJECT_KEY: java-sync-source
SOURCE_CLIENT_ID: ${{ secrets.SOURCE_CLIENT_ID }}
Expand All @@ -58,11 +58,8 @@ jobs:
steps:
- name: Git Checkout
uses: actions/checkout@v3
- name: Fetch Library version
id: vars
run: echo ::set-output name=libVersion::${GITHUB_REF#refs/*/}
- name: benchmark test
if: ${{ success() }}
run: ./gradlew clean setLibraryVersion benchmark
run: ./gradlew -Dbuild.version="${{ github.ref_name }}" clean benchmark
env:
GITHUB_TAG: ${{ steps.vars.outputs.libVersion }}
GITHUB_TAG: ${{ github.ref_name }}
2 changes: 1 addition & 1 deletion docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ If you have push access to the repository you can fix them directly otherwise ju

##### Build and publish to Maven Central
````bash
./gradlew clean setLibraryVersion -Dbuild.version={version} publishToSonatype closeAndReleaseSonatypeStagingRepository
./gradlew clean -Dbuild.version={version} publishToSonatype closeAndReleaseSonatypeStagingRepository
````

For more detailed information on the build and the release process, see [Build and Release](BUILD.md) documentation.
Expand Down
3 changes: 1 addition & 2 deletions gradle-scripts/execution-order.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ check.dependsOn jacocoTestReport
// Ensure jacocoTestCoverageVerification and jacocoTestReport run after integrationTest
jacocoTestCoverageVerification.mustRunAfter integrationTest
jacocoTestReport.mustRunAfter integrationTest
// Ensure build runs after setLibraryVersion
build.mustRunAfter setLibraryVersion
compileJava.dependsOn versionTxt
// Ensure benchmark results are only committed runs after the benchmarks are run
benchmarkCommit.mustRunAfter benchmark
44 changes: 16 additions & 28 deletions gradle-scripts/set-library-version.gradle
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
task setLibraryVersion {
description 'If the env var "GITHUB_TAG" is set, injects the value in the ' +
'src/main/java/com/commercetools/sync/commons/utils/SyncSolutionInfo.java. Otherwise, if the env var is not ' +
'set it sets the version to the value "dev-version". Note: Should only be executed before compilation in ' +
'the CI tool (e.g. github action.)'
doLast {
def versionFile = 'src/main/java/com/commercetools/sync/commons/utils/SyncSolutionInfo.java'
def versionFileContents = new File(versionFile).text
def versionPlaceholder = '#{LIB_VERSION}'
def libVersion = 'dev-version'
def tagName = System.getenv('GITHUB_TAG')

if (!versionFileContents.contains(versionPlaceholder)) {
throw new InvalidUserCodeException("$versionFile does not contain the placeholder: $versionPlaceholder. " +
"Please make sure the file contains the placeholder, in order for the version to be injected " +
"correctly.")
sourceSets {
main {
java {
srcDir 'build/generated/src/main/java'
}
}
}

// if build was triggered by a git tag, set version to tag name
if (tagName) {
libVersion = tagName
}
task versionTxt() {
doLast {
new File(projectDir, "build/generated/src/main/java/com/commercetools/sync/").mkdirs()
new File(projectDir, "build/generated/src/main/java/com/commercetools/sync/BuildInfo.java").text = """
package com.commercetools.sync;

if (libVersion) {
println "Injecting the version: '$libVersion' in $versionFile"
ant.replace(file: versionFile, token: versionPlaceholder, value: libVersion)
} else {
throw new InvalidUserDataException("Unable to set library version in $versionFile. Please make sure the" +
" var 'libVersion' is set correctly.")
}
public class BuildInfo {
public static final String VERSION = "$version";
}
"""
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.commercetools.sync.commons.utils;

import com.commercetools.sync.BuildInfo;
import io.sphere.sdk.client.SolutionInfo;

public final class SyncSolutionInfo extends SolutionInfo {
private static final String LIB_NAME = "commercetools-sync-java";
/** This value is injected by the script at gradle-scripts/set-library-version.gradle. */
public static final String LIB_VERSION = "#{LIB_VERSION}";
public static final String LIB_VERSION = BuildInfo.VERSION;

/**
* Extends {@link SolutionInfo} class of the JVM SDK to append to the User-Agent header with
Expand Down