Skip to content

Implementation of GitHub Actions #541

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

Merged
merged 7 commits into from
Apr 15, 2025
Merged
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
105 changes: 105 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# © 2025. TU Dortmund University,
# Institute of Energy Systems, Energy Efficiency and Energy Economics,
# Research group Distribution grid planning and operation
#

name: CI

on:
push:
paths-ignore:
- 'docs/**'
branches:
- main
- dev
- 'hotfix/*'
- 'rel/*'
- 'dependabot/*'
pull_request:
branches:
- main
- dev

jobs:
buildAndTest:
runs-on: ubuntu-latest
timeout-minutes: 30

steps:
- name: Checkout Source
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup Java
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: 17

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4

- name: Check Branch
run: |
if [ "${{ github.event_name }}" == "pull_request" ]; then
BRANCH_NAME="${{ github.head_ref }}"
else
BRANCH_NAME="${{ github.ref_name }}"
fi

if [[ "$BRANCH_NAME" == refs/heads/* ]]; then
BRANCH_NAME="${BRANCH_NAME#refs/heads/}"
fi

export BRANCH_NAME

echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV

./gradlew checkBranchName -PbranchName="$BRANCH_NAME" --warning-mode=none

bash scripts/branch_type.sh

- name: Version Check
if: ${{ github.event_name == 'pull_request' }}
env:
BASE_BRANCH: ${{ github.event.pull_request.base.ref }}
run: bash scripts/run-version-check.sh

- name: Build Project
run: ./gradlew --refresh-dependencies clean assemble spotlessCheck

- name: Run Tests
run: |
./gradlew pmdMain pmdTest spotbugsMain spotbugsTest test jacocoTestReport jacocoTestCoverageVerification \
reportScoverage checkScoverage

- name: Build Scala-Docs
run: ./gradlew scaladoc

- name: SonarQube
run: |
./gradlew sonar \
-Dsonar.projectKey=${{ vars.SONAR_PROJECT_KEY }} \
-Dsonar.host.url=${{ vars.SONAR_HOST_URL }} \
-Dsonar.login=${{ secrets.SONAR_TOKEN }} \
-Dsonar.qualitygate.wait=true

#Deployment
- name: Deploy
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/dev'
env:
ORG_GRADLE_PROJECT_signingKey: ${{ secrets.MAVENCENTRAL_SIGNINGKEY }}
ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.MAVENCENTRAL_SIGNINGPASS }}
ORG_GRADLE_PROJECT_user: ${{ secrets.MAVENCENTRAL_USER }}
ORG_GRADLE_PROJECT_password: ${{ secrets.MAVENCENTRAL_PASS }}
run: |
if [ "${GITHUB_REF}" == "refs/heads/main" ]; then
currentVersion=$(./gradlew -q currentVersion)
else
currentVersion=$(./gradlew -q devVersion)
fi

echo "currentVersion=$currentVersion"

./gradlew publish -PdeployVersion=$currentVersion
24 changes: 24 additions & 0 deletions .github/workflows/dependabot-auto-merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Dependabot auto-merge
on: pull_request

permissions:
contents: write
pull-requests: write

jobs:
dependabot:
runs-on: ubuntu-latest
if: github.event.pull_request.user.login == 'dependabot[bot]' && github.repository == 'ie3-institute/PowerSystemUtils'
steps:
- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@d7267f607e9d3fb96fc2fbe83e0af444713e90b7
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"

- name: Enable auto-merge for Dependabot PRs
if: steps.metadata.outputs.update-type == 'version-update:semver-patch'
run: gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{ github.event.pull_request.html_url }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased/Snapshot]
### Added
- Added semVer Gradle plugin to enable semantic versioning [#544](https://github.com/ie3-institute/PowerSystemUtils/issues/544)
- Implemented GitHub Actions Pipeline [#540](https://github.com/ie3-institute/PowerSystemUtils/issues/540)

### Changed
- Added Bao and Staudt to the list of reviewers [#510](https://github.com/ie3-institute/PowerSystemUtils/issues/510)
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ apply from: scriptsLocation + 'mavenCentralPublish.gradle'
apply from: scriptsLocation + 'sonarqube.gradle'
apply from: scriptsLocation + 'scoverage.gradle' // scoverage scala code coverage
apply from: scriptsLocation + 'semVer.gradle'
apply from: scriptsLocation + 'branchName.gradle'

repositories {
mavenCentral()
Expand Down Expand Up @@ -90,7 +91,7 @@ tasks.withType(JavaCompile) {
}


task printVersion {
tasks.register("printVersion") {
doLast {
println project.version
}
Expand Down
26 changes: 26 additions & 0 deletions gradle/scripts/branchName.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
tasks.register('checkBranchName') {
doLast {
if (!project.hasProperty('branchName')) {
throw new GradleException("Error: Missing required property 'branchName'.")
}

def branchName = project.property('branchName')

def patterns = [
~/^(developer|develop|dev)$/,
~/.*rel\/.*/,
~/^dependabot\/.*$/,
~/.*hotfix\/\pL{2}\/#\d+.*/,
~/.*main/,
~/^[a-z]{2}\/#[0-9]+(?:-.+)?$/
]

def isValid = patterns.any { pattern -> branchName ==~ pattern }

if (!isValid) {
throw new GradleException("Error: Check Branch name format (e.g., ps/#1337-FeatureName). Current branch name is $branchName.")
}

println "Branch name is $branchName"
}
}
39 changes: 39 additions & 0 deletions scripts/branch_type.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash
set -euo pipefail

if [ -z "${BRANCH_NAME:-}" ]; then
echo "Error: BRANCH_NAME variable is not set."
exit 1
fi


pattern_dev='^(developer|develop|dev)$'
pattern_release='.*rel/.*'
pattern_dependabot='^dependabot/.*'
pattern_hotfix='.*hotfix/.*'
pattern_main='.*main'
pattern_feature='^[a-z]{2}/#[0-9]+(-.+)?$'

BRANCH_TYPE="unknown"

if [[ "$BRANCH_NAME" =~ $pattern_dev ]]; then
BRANCH_TYPE="dev"
elif [[ "$BRANCH_NAME" =~ $pattern_release ]]; then
BRANCH_TYPE="release"
elif [[ "$BRANCH_NAME" =~ $pattern_dependabot ]]; then
BRANCH_TYPE="dependabot"
elif [[ "$BRANCH_NAME" =~ $pattern_hotfix ]]; then
BRANCH_TYPE="hotfix"
elif [[ "$BRANCH_NAME" =~ $pattern_main ]]; then
BRANCH_TYPE="main"
elif [[ "$BRANCH_NAME" =~ $pattern_feature ]]; then
BRANCH_TYPE="feature"
else
echo "Error:'$BRANCH_NAME' does not match any pattern."
exit 1
fi

echo "========================="
echo "Branch type: $BRANCH_TYPE"
echo "BRANCH_TYPE=$BRANCH_TYPE" >> "$GITHUB_ENV"
echo "========================="
36 changes: 36 additions & 0 deletions scripts/get_versions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash
set -euo pipefail

cd "$(dirname "$0")/.."

REPO_URL=$(git config --get remote.origin.url)
export REPO_URL
echo "REPO_URL=$REPO_URL" >> $GITHUB_ENV

echo "Fetching current version of PR..."
PR_VERSION=$(./gradlew -q currentVersion)
echo "PR_VERSION=$PR_VERSION"
echo "export PR_VERSION=$PR_VERSION" >> versions.env
echo "PR_VERSION=$PR_VERSION" >> "$GITHUB_ENV"

get_branch_version() {
local BRANCH_NAME=$1
local DIR_NAME="${BRANCH_NAME}-branch"

git clone --depth 1 --branch "$BRANCH_NAME" "$REPO_URL" "$DIR_NAME"
cd "$DIR_NAME"

echo "Fetching version from $BRANCH_NAME branch..."
BRANCH_VERSION=$(./gradlew -q currentVersion)
cd ..

echo "${BRANCH_NAME^^}_VERSION=$BRANCH_VERSION"
echo "export ${BRANCH_NAME^^}_VERSION=$BRANCH_VERSION" >> versions.env
echo "${BRANCH_NAME^^}_VERSION=$BRANCH_VERSION" >> "$GITHUB_ENV"

rm -rf "$DIR_NAME"
}

get_branch_version "main"

echo "Get Versions: OK!"
10 changes: 10 additions & 0 deletions scripts/run-version-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -euo pipefail

rm -f versions.env

scripts/get_versions.sh

source versions.env

scripts/version_check.sh
56 changes: 56 additions & 0 deletions scripts/version_check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash
set -euo pipefail

cd "$(dirname "$0")/.."

echo "========================="
echo "LOADED ENV VARIABLES:"
echo "PR_VERSION: $PR_VERSION"
echo "MAIN_VERSION: $MAIN_VERSION"
echo "BASE_BRANCH: $BASE_BRANCH"
echo "========================="

semver_gt() {
IFS='.' read -r major1 minor1 patch1 <<< "$1"
IFS='.' read -r major2 minor2 patch2 <<< "$2"

# Compare major version
if [ "$major1" -gt "$major2" ]; then
return 0
elif [ "$major1" -lt "$major2" ]; then
return 1
fi

# Compare minor version
if [ "$minor1" -gt "$minor2" ]; then
return 0
elif [ "$minor1" -lt "$minor2" ]; then
return 1
fi

# Compare patch version
if [ "$patch1" -gt "$patch2" ]; then
return 0
else
return 1
fi
}

# Version Checking Logic
if [ "$BASE_BRANCH" = "main" ]; then
echo "'$BRANCH_TYPE' branch into main => applying '$BRANCH_TYPE' rules"
if [ "$PR_VERSION" = "$MAIN_VERSION" ] && [ "$BRANCH_TYPE" = "feature" ]; then
echo "OK: PR Version ($PR_VERSION) is identical with the current Main version ($MAIN_VERSION)."
exit 0
elif semver_gt "$PR_VERSION" "$MAIN_VERSION" && [ "$BRANCH_TYPE" = "release" ]; then
echo "OK: PR Version ($PR_VERSION) is higher than Main version ($MAIN_VERSION)."
exit 0
else
echo "FAIL: PR Version ($PR_VERSION) is neither equal to nor higher than the current Main version ($MAIN_VERSION)."
exit 1
fi

else
echo "Skipping version check: Base branch is '$BASE_BRANCH'. No version enforcement required."
exit 0
fi