Skip to content

Commit

Permalink
Add build/release devops
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-muller666 committed Jul 28, 2024
1 parent 7ff5e33 commit 90b4471
Show file tree
Hide file tree
Showing 3 changed files with 339 additions and 42 deletions.
232 changes: 232 additions & 0 deletions .github/workflows/build-and-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
name: Build and Release

on:
push:
branches:
- main
- devops-test

jobs:

build-application:
runs-on: ubuntu-latest

outputs:
VERSION_TAG: ${{ steps.version_info.outputs.VERSION_TAG }}
PROJECT_VERSION: ${{ steps.version_info.outputs.PROJECT_VERSION }}

steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set Up JDK
uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'adopt'

- name: Generate version tag and build
id: version_info
run: |
git fetch --tags
MAJOR_MINOR=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout | sed -E 's/([0-9]+\.[0-9]+).*/\1/')
echo "Extracted [major.minor] version: $MAJOR_MINOR"
LATEST_TAG=$(git tag -l "v*" | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | sort -rV | head -n 1)
echo "Latest tag: $LATEST_TAG"
if [ -z "$LATEST_TAG" ]; then
PATCH=0
else
TAG_MAJOR_MINOR=$(echo $LATEST_TAG | sed -E 's/v([0-9]+\.[0-9]+)\.[0-9]+/\1/')
TAG_PATCH=$(echo $LATEST_TAG | sed -E 's/v[0-9]+\.[0-9]+\.([0-9]+)/\1/')
if [ "$(printf '%s\n' "$TAG_MAJOR_MINOR" "$MAJOR_MINOR" | sort -rV | head -n 1)" != "$MAJOR_MINOR" ]; then
echo "Current version is less than the last tag. Aborting."
exit 1
elif [ "$MAJOR_MINOR" = "$TAG_MAJOR_MINOR" ]; then
PATCH=$(($TAG_PATCH + 1))
echo "Incremented patch version: $PATCH"
else
PATCH=0
echo "Patch version reset to 0"
fi
fi
PROJECT_VERSION="$MAJOR_MINOR.$PATCH"
echo "New project version: $PROJECT_VERSION"
echo "-----> Packaging"
mvn --quiet package -Drevision=$PATCH -DskipTests=true -Dmaven.javadoc.skip=true
echo "Packaging completed"
echo "-----> Creating release pom"
mvn --quiet -Drevision=$PATCH gplus:execute
VERSION_TAG=v$PROJECT_VERSION
echo "-----> Tagging version: $VERSION_TAG"
git tag "$VERSION_TAG"
git push origin "$VERSION_TAG"
echo "VERSION_TAG=$VERSION_TAG" >> $GITHUB_OUTPUT
echo "PROJECT_VERSION=$PROJECT_VERSION" >> $GITHUB_OUTPUT
- name: Upload JAR file
uses: actions/upload-artifact@v2
with:
name: app-jar
path: target/*.jar
if-no-files-found: error

- name: Upload pom-release file
uses: actions/upload-artifact@v2
with:
name: release-pom
path: target/pom.xml
if-no-files-found: error

publish-docs:
needs: build-application
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up JDK
uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'adopt'

- name: Generate Javadoc
run: mvn dokka:javadoc -Ddokka.goal=javadoc -Ddokka.dir=./target/site/apidocs/javadoc

- name: Generate KDoc
run: mvn dokka:dokka -Ddokka.goal=dokka -Ddokka.dir=./target/site/apidocs/kdoc

- name: Prepare gh-pages content
run: |
cp -r ./src/main/resources/site/* .
mkdir -p ./apidocs
cp -r ./target/site/apidocs/* ./apidocs/
- name: Deploy to gh-pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./
keep_files: false
allow_empty_commit: false
force_orphan: true

create-release:
needs: [build-application]
runs-on: ubuntu-latest
steps:
- name: Download JAR file
uses: actions/download-artifact@v2
with:
name: app-jar
path: ./

- name: Download release POM
uses: actions/download-artifact@v2
with:
name: release-pom
path: ./

- name: Debug Output
run: |
echo "VERSION_TAG from build-application job: ${{ needs.build-application.outputs.VERSION_TAG }}"
echo "PROJECT_VERSION from build-application job: ${{ needs.build-application.outputs.PROJECT_VERSION }}"
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ needs.build-application.outputs.VERSION_TAG }}
release_name: Release ${{ needs.build-application.outputs.PROJECT_VERSION }}
body: |
## Installation
To manually install the JAR file included in this release, use the following Maven command:
```sh
mvn install:install-file \
-Dfile=<path-to-jar-file> \
-DgroupId=no.acntech.kollectivequery \
-DartifactId=kollectivequery \
-Dversion=${{ needs.build-application.outputs.PROJECT_VERSION }} \
-Dpackaging=jar \
-DpomFile=<path-to-pom-file>
```
Ensure to replace the placeholders with the actual path to the downloaded JAR file and the release pom file.
Alternatively, run the following bash script to download and install the JAR file:
```sh
#!/bin/bash
# URLs for jar and pom files
JAR_URL=https://github.com/acntech/kollectivequery/releases/download/${{ needs.build-application.outputs.VERSION_TAG }}/kollectivequery-${{ needs.build-application.outputs.PROJECT_VERSION }}.jar
POM_URL=https://github.com/acntech/kollectivequery/releases/download/${{ needs.build-application.outputs.VERSION_TAG }}/pom.xml
# Create a temporary directory
TEMP_DIR=$(mktemp -d -t ci-XXXXXXXXXX)
# File paths
JAR_FILE_PATH=$TEMP_DIR/kollectivequery-${{ needs.build-application.outputs.PROJECT_VERSION }}.jar
POM_FILE_PATH=$TEMP_DIR/pom.xml
# Download jar and pom files
curl -L -o $JAR_FILE_PATH $JAR_URL
curl -L -o $POM_FILE_PATH $POM_URL
echo "Jar and POM files downloaded to $TEMP_DIR"
# Maven install command
mvn install:install-file \
-Dfile=$JAR_FILE_PATH \
-DgroupId=no.acntech.kollectivequery \
-DartifactId=kollectivequery \
-Dversion=${{ needs.build-application.outputs.PROJECT_VERSION }} \
-Dpackaging=jar \
-DpomFile=$POM_FILE_PATH
# Delete the temporary directory
rm -r $TEMP_DIR
```
## Gradle/Maven Dependency
For Gradle, add this to your `build.gradle` file's dependencies block:
```groovy
implementation 'no.acntech.kollectivequery:kollectivequery:${{ needs.build-application.outputs.PROJECT_VERSION }}'
```
For Maven, add this to your `pom.xml` file's dependencies block:
```xml
<dependency>
<groupId>no.acntech.kollectivequery</groupId>
<artifactId>kollectivequery</artifactId>
<version>${{ needs.build-application.outputs.PROJECT_VERSION }}</version>
</dependency>
```
draft: false
prerelease: true

- name: Upload Jar
id: upload_release_jar
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./kollectivequery-${{ needs.build-application.outputs.PROJECT_VERSION }}.jar
asset_name: kollectivequery-${{ needs.build-application.outputs.PROJECT_VERSION }}.jar
asset_content_type: application/java-archive

- name: Upload Release POM
id: upload_release_pom
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./pom.xml
asset_name: pom.xml
asset_content_type: application/xml
42 changes: 0 additions & 42 deletions .github/workflows/generate-docs.yml

This file was deleted.

107 changes: 107 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<versions-maven-plugin.version>2.17.1</versions-maven-plugin.version>
<maven-dependency-plugin.version>3.7.1</maven-dependency-plugin.version>
<dependency-check-maven.version>10.0.3</dependency-check-maven.version>
<gmavenplus-plugin.version>3.0.2</gmavenplus-plugin.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -429,6 +430,112 @@
</executions>
</plugin>

<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>${gmavenplus-plugin.version}</version>
<executions>
<execution>
<!--
<id>generate-release-pom</id>
<phase>generate-resources</phase>
-->
<goals>
<goal>execute</goal>
</goals>
</execution>
</executions>
<configuration>
<scripts>
<script>
<![CDATA[
import org.apache.maven.model.io.xpp3.MavenXpp3Reader
import org.apache.maven.model.io.xpp3.MavenXpp3Writer
import org.apache.maven.model.Model
import org.apache.maven.model.Dependency
def pomFile = new File("pom.xml")
def reader = new FileReader(pomFile)
def mavenReader = new MavenXpp3Reader()
def model = mavenReader.read(reader)
reader.close()
def revision = System.properties['revision']
if (!revision) {
throw new IllegalStateException("Please specify the revision using '-Drevision'")
}
println "Creating release pom.xml with revision: $revision"
// Interpolate model version
String newVersion = "${model.version.replace('${revision}', revision)}"
println "version interpolated: $newVersion"
// Create a new minimal model
def newModel = new Model()
newModel.modelVersion = '4.0.0'
newModel.groupId = model.groupId
newModel.artifactId = model.artifactId
newModel.version = newVersion
// Add only compile, runtime, or no-scope dependencies
List<Dependency> dependencies = model.dependencies.findAll { dep ->
dep.scope in ['compile', 'runtime', null]
}.collect { dep ->
// Interpolate version for each dependency
String version
if (dep.version.contains('${') && dep.version.contains('}')) {
String versionKey = dep.version.replaceAll(/[\$\{\}]/, '')
// use resolution from model.properties first, then try System.properties
version = model.properties.getProperty(versionKey) ?: System.properties[versionKey] ?: dep.version
} else {
version = dep.version
}
new Dependency(groupId: dep.groupId, artifactId: dep.artifactId, version: version, scope: dep.scope)
}
newModel.dependencies.addAll(dependencies)
def writer = new MavenXpp3Writer()
writer.write(new FileWriter("./target/pom.xml"), newModel)
]]>
</script>
</scripts>
</configuration>
<dependencies>

<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>4.0.22</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.9.8</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model-builder</artifactId>
<version>3.9.8</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-shared-utils</artifactId>
<version>3.4.2</version>
</dependency>

</dependencies>
</plugin>

</plugins>
</build>

Expand Down

0 comments on commit 90b4471

Please sign in to comment.