Skip to content

Commit 0fa1d47

Browse files
Implementation of GitHub Actions (#541)
* gha * spotless * test * rollback * fixed double entry of semver
1 parent cc06ac8 commit 0fa1d47

File tree

9 files changed

+299
-1
lines changed

9 files changed

+299
-1
lines changed

.github/workflows/ci.yml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# © 2025. TU Dortmund University,
2+
# Institute of Energy Systems, Energy Efficiency and Energy Economics,
3+
# Research group Distribution grid planning and operation
4+
#
5+
6+
name: CI
7+
8+
on:
9+
push:
10+
paths-ignore:
11+
- 'docs/**'
12+
branches:
13+
- main
14+
- dev
15+
- 'hotfix/*'
16+
- 'rel/*'
17+
- 'dependabot/*'
18+
pull_request:
19+
branches:
20+
- main
21+
- dev
22+
23+
jobs:
24+
buildAndTest:
25+
runs-on: ubuntu-latest
26+
timeout-minutes: 30
27+
28+
steps:
29+
- name: Checkout Source
30+
uses: actions/checkout@v4
31+
with:
32+
fetch-depth: 0
33+
34+
- name: Setup Java
35+
uses: actions/setup-java@v4
36+
with:
37+
distribution: 'temurin'
38+
java-version: 17
39+
40+
- name: Setup Gradle
41+
uses: gradle/actions/setup-gradle@v4
42+
43+
- name: Check Branch
44+
run: |
45+
if [ "${{ github.event_name }}" == "pull_request" ]; then
46+
BRANCH_NAME="${{ github.head_ref }}"
47+
else
48+
BRANCH_NAME="${{ github.ref_name }}"
49+
fi
50+
51+
if [[ "$BRANCH_NAME" == refs/heads/* ]]; then
52+
BRANCH_NAME="${BRANCH_NAME#refs/heads/}"
53+
fi
54+
55+
export BRANCH_NAME
56+
57+
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
58+
59+
./gradlew checkBranchName -PbranchName="$BRANCH_NAME" --warning-mode=none
60+
61+
bash scripts/branch_type.sh
62+
63+
- name: Version Check
64+
if: ${{ github.event_name == 'pull_request' }}
65+
env:
66+
BASE_BRANCH: ${{ github.event.pull_request.base.ref }}
67+
run: bash scripts/run-version-check.sh
68+
69+
- name: Build Project
70+
run: ./gradlew --refresh-dependencies clean assemble spotlessCheck
71+
72+
- name: Run Tests
73+
run: |
74+
./gradlew pmdMain pmdTest spotbugsMain spotbugsTest test jacocoTestReport jacocoTestCoverageVerification \
75+
reportScoverage checkScoverage
76+
77+
- name: Build Scala-Docs
78+
run: ./gradlew scaladoc
79+
80+
- name: SonarQube
81+
run: |
82+
./gradlew sonar \
83+
-Dsonar.projectKey=${{ vars.SONAR_PROJECT_KEY }} \
84+
-Dsonar.host.url=${{ vars.SONAR_HOST_URL }} \
85+
-Dsonar.login=${{ secrets.SONAR_TOKEN }} \
86+
-Dsonar.qualitygate.wait=true
87+
88+
#Deployment
89+
- name: Deploy
90+
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/dev'
91+
env:
92+
ORG_GRADLE_PROJECT_signingKey: ${{ secrets.MAVENCENTRAL_SIGNINGKEY }}
93+
ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.MAVENCENTRAL_SIGNINGPASS }}
94+
ORG_GRADLE_PROJECT_user: ${{ secrets.MAVENCENTRAL_USER }}
95+
ORG_GRADLE_PROJECT_password: ${{ secrets.MAVENCENTRAL_PASS }}
96+
run: |
97+
if [ "${GITHUB_REF}" == "refs/heads/main" ]; then
98+
currentVersion=$(./gradlew -q currentVersion)
99+
else
100+
currentVersion=$(./gradlew -q devVersion)
101+
fi
102+
103+
echo "currentVersion=$currentVersion"
104+
105+
./gradlew publish -PdeployVersion=$currentVersion
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Dependabot auto-merge
2+
on: pull_request
3+
4+
permissions:
5+
contents: write
6+
pull-requests: write
7+
8+
jobs:
9+
dependabot:
10+
runs-on: ubuntu-latest
11+
if: github.event.pull_request.user.login == 'dependabot[bot]' && github.repository == 'ie3-institute/PowerSystemUtils'
12+
steps:
13+
- name: Dependabot metadata
14+
id: metadata
15+
uses: dependabot/fetch-metadata@d7267f607e9d3fb96fc2fbe83e0af444713e90b7
16+
with:
17+
github-token: "${{ secrets.GITHUB_TOKEN }}"
18+
19+
- name: Enable auto-merge for Dependabot PRs
20+
if: steps.metadata.outputs.update-type == 'version-update:semver-patch'
21+
run: gh pr merge --auto --merge "$PR_URL"
22+
env:
23+
PR_URL: ${{ github.event.pull_request.html_url }}
24+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased/Snapshot]
88
### Added
99
- Added semVer Gradle plugin to enable semantic versioning [#544](https://github.com/ie3-institute/PowerSystemUtils/issues/544)
10+
- Implemented GitHub Actions Pipeline [#540](https://github.com/ie3-institute/PowerSystemUtils/issues/540)
1011

1112
### Changed
1213
- Added Bao and Staudt to the list of reviewers [#510](https://github.com/ie3-institute/PowerSystemUtils/issues/510)

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ apply from: scriptsLocation + 'mavenCentralPublish.gradle'
3939
apply from: scriptsLocation + 'sonarqube.gradle'
4040
apply from: scriptsLocation + 'scoverage.gradle' // scoverage scala code coverage
4141
apply from: scriptsLocation + 'semVer.gradle'
42+
apply from: scriptsLocation + 'branchName.gradle'
4243

4344
repositories {
4445
mavenCentral()
@@ -90,7 +91,7 @@ tasks.withType(JavaCompile) {
9091
}
9192

9293

93-
task printVersion {
94+
tasks.register("printVersion") {
9495
doLast {
9596
println project.version
9697
}

gradle/scripts/branchName.gradle

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
tasks.register('checkBranchName') {
2+
doLast {
3+
if (!project.hasProperty('branchName')) {
4+
throw new GradleException("Error: Missing required property 'branchName'.")
5+
}
6+
7+
def branchName = project.property('branchName')
8+
9+
def patterns = [
10+
~/^(developer|develop|dev)$/,
11+
~/.*rel\/.*/,
12+
~/^dependabot\/.*$/,
13+
~/.*hotfix\/\pL{2}\/#\d+.*/,
14+
~/.*main/,
15+
~/^[a-z]{2}\/#[0-9]+(?:-.+)?$/
16+
]
17+
18+
def isValid = patterns.any { pattern -> branchName ==~ pattern }
19+
20+
if (!isValid) {
21+
throw new GradleException("Error: Check Branch name format (e.g., ps/#1337-FeatureName). Current branch name is $branchName.")
22+
}
23+
24+
println "Branch name is $branchName"
25+
}
26+
}

scripts/branch_type.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
if [ -z "${BRANCH_NAME:-}" ]; then
5+
echo "Error: BRANCH_NAME variable is not set."
6+
exit 1
7+
fi
8+
9+
10+
pattern_dev='^(developer|develop|dev)$'
11+
pattern_release='.*rel/.*'
12+
pattern_dependabot='^dependabot/.*'
13+
pattern_hotfix='.*hotfix/.*'
14+
pattern_main='.*main'
15+
pattern_feature='^[a-z]{2}/#[0-9]+(-.+)?$'
16+
17+
BRANCH_TYPE="unknown"
18+
19+
if [[ "$BRANCH_NAME" =~ $pattern_dev ]]; then
20+
BRANCH_TYPE="dev"
21+
elif [[ "$BRANCH_NAME" =~ $pattern_release ]]; then
22+
BRANCH_TYPE="release"
23+
elif [[ "$BRANCH_NAME" =~ $pattern_dependabot ]]; then
24+
BRANCH_TYPE="dependabot"
25+
elif [[ "$BRANCH_NAME" =~ $pattern_hotfix ]]; then
26+
BRANCH_TYPE="hotfix"
27+
elif [[ "$BRANCH_NAME" =~ $pattern_main ]]; then
28+
BRANCH_TYPE="main"
29+
elif [[ "$BRANCH_NAME" =~ $pattern_feature ]]; then
30+
BRANCH_TYPE="feature"
31+
else
32+
echo "Error:'$BRANCH_NAME' does not match any pattern."
33+
exit 1
34+
fi
35+
36+
echo "========================="
37+
echo "Branch type: $BRANCH_TYPE"
38+
echo "BRANCH_TYPE=$BRANCH_TYPE" >> "$GITHUB_ENV"
39+
echo "========================="

scripts/get_versions.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
cd "$(dirname "$0")/.."
5+
6+
REPO_URL=$(git config --get remote.origin.url)
7+
export REPO_URL
8+
echo "REPO_URL=$REPO_URL" >> $GITHUB_ENV
9+
10+
echo "Fetching current version of PR..."
11+
PR_VERSION=$(./gradlew -q currentVersion)
12+
echo "PR_VERSION=$PR_VERSION"
13+
echo "export PR_VERSION=$PR_VERSION" >> versions.env
14+
echo "PR_VERSION=$PR_VERSION" >> "$GITHUB_ENV"
15+
16+
get_branch_version() {
17+
local BRANCH_NAME=$1
18+
local DIR_NAME="${BRANCH_NAME}-branch"
19+
20+
git clone --depth 1 --branch "$BRANCH_NAME" "$REPO_URL" "$DIR_NAME"
21+
cd "$DIR_NAME"
22+
23+
echo "Fetching version from $BRANCH_NAME branch..."
24+
BRANCH_VERSION=$(./gradlew -q currentVersion)
25+
cd ..
26+
27+
echo "${BRANCH_NAME^^}_VERSION=$BRANCH_VERSION"
28+
echo "export ${BRANCH_NAME^^}_VERSION=$BRANCH_VERSION" >> versions.env
29+
echo "${BRANCH_NAME^^}_VERSION=$BRANCH_VERSION" >> "$GITHUB_ENV"
30+
31+
rm -rf "$DIR_NAME"
32+
}
33+
34+
get_branch_version "main"
35+
36+
echo "Get Versions: OK!"

scripts/run-version-check.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
rm -f versions.env
5+
6+
scripts/get_versions.sh
7+
8+
source versions.env
9+
10+
scripts/version_check.sh

scripts/version_check.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
cd "$(dirname "$0")/.."
5+
6+
echo "========================="
7+
echo "LOADED ENV VARIABLES:"
8+
echo "PR_VERSION: $PR_VERSION"
9+
echo "MAIN_VERSION: $MAIN_VERSION"
10+
echo "BASE_BRANCH: $BASE_BRANCH"
11+
echo "========================="
12+
13+
semver_gt() {
14+
IFS='.' read -r major1 minor1 patch1 <<< "$1"
15+
IFS='.' read -r major2 minor2 patch2 <<< "$2"
16+
17+
# Compare major version
18+
if [ "$major1" -gt "$major2" ]; then
19+
return 0
20+
elif [ "$major1" -lt "$major2" ]; then
21+
return 1
22+
fi
23+
24+
# Compare minor version
25+
if [ "$minor1" -gt "$minor2" ]; then
26+
return 0
27+
elif [ "$minor1" -lt "$minor2" ]; then
28+
return 1
29+
fi
30+
31+
# Compare patch version
32+
if [ "$patch1" -gt "$patch2" ]; then
33+
return 0
34+
else
35+
return 1
36+
fi
37+
}
38+
39+
# Version Checking Logic
40+
if [ "$BASE_BRANCH" = "main" ]; then
41+
echo "'$BRANCH_TYPE' branch into main => applying '$BRANCH_TYPE' rules"
42+
if [ "$PR_VERSION" = "$MAIN_VERSION" ] && [ "$BRANCH_TYPE" = "feature" ]; then
43+
echo "OK: PR Version ($PR_VERSION) is identical with the current Main version ($MAIN_VERSION)."
44+
exit 0
45+
elif semver_gt "$PR_VERSION" "$MAIN_VERSION" && [ "$BRANCH_TYPE" = "release" ]; then
46+
echo "OK: PR Version ($PR_VERSION) is higher than Main version ($MAIN_VERSION)."
47+
exit 0
48+
else
49+
echo "FAIL: PR Version ($PR_VERSION) is neither equal to nor higher than the current Main version ($MAIN_VERSION)."
50+
exit 1
51+
fi
52+
53+
else
54+
echo "Skipping version check: Base branch is '$BASE_BRANCH'. No version enforcement required."
55+
exit 0
56+
fi

0 commit comments

Comments
 (0)