Skip to content

Commit

Permalink
Add convention plugin for publishing (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahatzz11 authored Jun 28, 2022
1 parent 5796581 commit 60c6618
Show file tree
Hide file tree
Showing 17 changed files with 281 additions and 218 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ on:
paths-ignore:
- '**.md'

concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true

env:
NEXUS_USER: ${{ secrets.NEXUS_USER }}
NEXUS_PASS: ${{ secrets.NEXUS_PASS }}
Expand Down
60 changes: 32 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
# Gradle Semver Plugin

* [Overview](#overview)
* [Version Calculation](#version-calculation)
* [Branch Matching Strategy](#branch-matching-strategy)
* [Usage](#usage)
* [Plugin Extension Properties](#plugin-extension-properties)
* [Plugin Tasks](#plugin-tasks)
* [Using with CI/CD](#using-with-cicd)
* [Unsupported](#not-supported)
Welcome to the gradle-semver-plugin!
This plugin adds a flexible approach to adding semantic versioning to your gradle project.
It supports multi-module gradle projects, running in CI, and multiple types of git strategies.

## Overview

A Gradle plugin with a flexible approach to generating semantic versions, typically for use within a Gradle project. It supports running under Github Actions.

- [Version Calculation](#version-calculation)
- [Branch Matching Strategy](#branch-matching-strategy)
- [Usage](#usage)
- [Plugin Extension Properties](#plugin-extension-properties)
- [Plugin Tasks](#plugin-tasks)
- [Using with CI](#using-with-ci)
- [Unsupported](#not-supported)

## Overview
It comes bundled with a single Version Calculator that implements a target branch calculator: the version of the current branch is based on the latest version of the branch it targets, eg `develop` is branched from `main`, thus the version of `develop` is based on the current version of `main`.

The Target Branch Version Calculator includes two Branch Matching strategies:
-Flow - broadly based on a [Git Flow workflow](https://nvie.com/posts/a-successful-git-branching-model/) without release branches, the following branches are supported:
- `Flow` - Broadly based on a [Git Flow workflow](https://nvie.com/posts/a-successful-git-branching-model/) without release branches, the following branches are supported:

| branch | pre release label | target branch | example |
|--------------------------|-------------------|---------------|------------------------|
Expand All @@ -25,29 +27,30 @@ The Target Branch Version Calculator includes two Branch Matching strategies:
| `hotfix/badthings` | rc | main | 1.2.4-rc.2 |
| `xxx` | xxx | develop | 1.2.5-xxx.13 |

- Flat - ideal for simpler projects without an integration branch such as `develop`:
- `Flat` - Ideal for projects using a single branch strategy with `main`.

| branch | pre release label | target branch | example |
|--------|-------------------|---------------|--------------|
| `main` | | main | 1.2.3 |
| `xxx` | xxx | main | 1.2.4-xxx.13 |

The `Flow` strategy is automatically selected if a `develop` branch is present, otherwise the `Flat` strategy is selected.

By default, the `Flow` strategy is selected if a `develop` branch is present, otherwise the `Flat` strategy will be used.

## Version Calculation

The semver is calculated primarily based on:
* the version of the target branch
* the current branch
* the branchMatching strategy
- The version of the target branch
- The current branch
- The branchMatching strategy

## Branch Matching Strategy

A Strategy contains a list of `BranchMatchingConfiguration` instances which are applied in order until the first match is reached, it contains the following properties:
* branch name regex
* target branch
* version modifier: modifies the major, minor or patch components of the semver
* version qualifier: optionally qualifies the semver with a prerelease label and build metadata
- branch name regex
- target branch
- version modifier: modifies the major, minor or patch components of the semver
- version qualifier: optionally qualifies the semver with a prerelease label and build metadata

The `VersionModifier` can be set for every `BranchMatchingConfiguration` instance in the strategy with the plugin extension:

Expand Down Expand Up @@ -108,18 +111,19 @@ semver {
_**PLEASE NOTE:**_ the `semver` stanza should be declared **before** the `semver` extension functions are used.

## Plugin Extension Properties
* `version: String` returns the calculated version
* `versionTagName: String` returns the tag name for the current calculated version, ie `tagPrefix` + `version`
- `version: String` returns the calculated version
- `versionTagName: String` returns the tag name for the current calculated version, ie `tagPrefix` + `version`

## Plugin Tasks
* `cv` that will print out the current calculated version
* `generateVersionFile` will generate `build/semver/version.txt` containing the raw version and the tag version
* `createAndPushVersionTag` will create a tag from `semver.versionTagName` and push the tag to the remote repo, take care to use `:createAndPushVersionTag` in a multi module project otherwise it will attempt to create duplicates
- `cv` that will print out the current calculated version
- `generateVersionFile` will generate `build/semver/version.txt` containing the raw version and the tag version
- `createAndPushVersionTag` will create a tag from `semver.versionTagName` and push the tag to the remote repo, take care to use `:createAndPushVersionTag` in a multi module project otherwise it will attempt to create duplicates

## Using with CI/CD
## Using with CI

Make sure to check out all branches & tags, eg using GitHub Actions:
When using this plugin in CI check out all branches & tags to ensure an accurate version calculation.

GitHub Actions Example:
```yaml
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -128,4 +132,4 @@ Make sure to check out all branches & tags, eg using GitHub Actions:
```
## Not Supported
* Discrete versions for subprojects, all subprojects are calculated with the same version
- Discrete versions for subprojects, all subprojects are calculated with the same version
12 changes: 12 additions & 0 deletions build-logic/build-conventions/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
`kotlin-dsl`
}

repositories {
gradlePluginPortal() // so that external plugins can be resolved in dependencies section
}

dependencies {
// note: this `libs` is from build-logic/settings.gradle.kts, technically different from the 'main' project
implementation(libs.gradle.plugin.publish)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import org.gradle.kotlin.dsl.`java-gradle-plugin`

plugins {
`maven-publish`
`java-gradle-plugin`
id("com.gradle.plugin-publish")
}

/*
* Project information
*/
group = "com.figure.gradle"
description = "Gradle Plugin for Automated Semantic Versioning"

inner class ProjectInfo {
val longName = "Gradle Semver Plugin"
val description = "Gradle Plugin for Automated Semantic Versioning"
val pluginImplementationClass = "$group.semver.SemverPlugin"
val tags = listOf("semver", "gradle", "gitflow", "gitubflow")
val website = "https://github.com/FigureTechnologies/gradle-semver-plugin"
val vcsURL = "https://github.com/FigureTechnologies/gradle-semver-plugin.git"
}
val info = ProjectInfo()

/**
* Eventually we will be able to merge the pluginBundle and gradlePlugin section.
* Unfortunately, we can't do that gradle 7.6 is out, so we wait.
*
* Gradle 8+ will remove the pluginBundle
* Link: https://plugins.gradle.org/docs/publish-plugin-new
*/

// I think this is specifically for the gradle portal
pluginBundle {
website = info.website
vcsUrl = info.vcsURL
tags = info.tags

// shared description
// description = info.description
}

gradlePlugin {

plugins {
create(project.name) {
id = "$group.${project.name}"
displayName = info.longName
description = info.description
implementationClass = info.pluginImplementationClass
}
}
}

publishing {
repositories {
maven {
url = uri("https://nexus.figure.com/repository/figure")
credentials {
username = System.getenv("NEXUS_USER")
password = System.getenv("NEXUS_PASS")
}
}
}
publications {
create<MavenPublication>("mavenJava") {
pom {
// This line is what includes the java{} block, aka javadocs and sources
from(components["java"])

name.set(info.longName)
description.set(info.description)
url.set(info.website)
licenses {
license {
name.set("The Apache License, Version 2.0")
url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
}
}
developers {
developer {
id.set("ahatzz11")
name.set("Alex Hatzenbuhler")
email.set("[email protected]")
}
developer {
id.set("happyphan")
name.set("Emily Harris")
email.set("[email protected]")
}
developer {
id.set("luinstra")
name.set("Jeremy Luinstra")
email.set("[email protected]")
}
developer {
id.set("jonasg13")
name.set("Jonas Gorauskas")
email.set("[email protected]")
}
}
scm {
connection.set("scm:git:git://github.com/FigureTechnologies/gradle-semver-plugin.git")
developerConnection.set("scm:git:ssh://github.com/FigureTechnologies/gradle-semver-plugin.git")
url.set(info.website)
}
}
}
}
}
26 changes: 26 additions & 0 deletions build-logic/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")
enableFeaturePreview("VERSION_CATALOGS")

// This is for the kotlin-dsl
dependencyResolutionManagement {
repositories {
mavenLocal()
mavenCentral()

gradlePluginPortal() // so that external plugins can be resolved in dependencies section
}

// kinda hacky way to make a catalog from our own catalog, but it works!
// Link: https://github.com/gradle/gradle/issues/15383#issuecomment-779893192
versionCatalogs {
create("libs") {
from(files("../gradle/libs.versions.toml"))
}
}
}

plugins {

}

include("build-conventions")
Loading

0 comments on commit 60c6618

Please sign in to comment.