refactor: improve version checking logic in SemanticReleasingExecutor

Signed-off-by: zhenyus <zhenyus@mathmast.com>
This commit is contained in:
zhenyus 2025-04-21 11:48:44 +08:00
parent 733a559222
commit 64a4d5b02e

View File

@ -29,6 +29,15 @@ class SemanticReleasingExecutor {
steps.writeFile file: '.releaserc.json', text: steps.libraryResource(config) steps.writeFile file: '.releaserc.json', text: steps.libraryResource(config)
steps.sh "git config --global --add safe.directory ${steps.env.workroot}" steps.sh "git config --global --add safe.directory ${steps.env.workroot}"
steps.env.GIT_LOCAL_BRANCH = "${branch}" steps.env.GIT_LOCAL_BRANCH = "${branch}"
// Store the version before semantic release
def previousVersion = ""
def versionFileExistsBefore = steps.sh(script: 'test -f "VERSION"', returnStatus: true) == 0
if (versionFileExistsBefore) {
previousVersion = steps.readFile('VERSION').trim()
steps.log.info("SemanticReleasingExecutor", "Previous version: ${previousVersion}")
}
steps.sh """ steps.sh """
#!/bin/bash #!/bin/bash
semantic-release semantic-release
@ -37,21 +46,20 @@ class SemanticReleasingExecutor {
// check if VERSION file exists // check if VERSION file exists
def versionFileExists = steps.sh(script: 'test -f "VERSION"', returnStatus: true) == 0 def versionFileExists = steps.sh(script: 'test -f "VERSION"', returnStatus: true) == 0
def recentlyModified = false def released = false
if (versionFileExists) {
// check if VERSION file was modified recently (within last minute)
def modTime = steps.sh(script: 'stat -c %Y "VERSION"', returnStdout: true).trim() as Long
def currentTime = System.currentTimeMillis() / 1000
recentlyModified = (currentTime - modTime) < 60
}
def released = versionFileExists && recentlyModified if (versionFileExists) {
if (released) { def currentVersion = steps.readFile('VERSION').trim()
steps.env.LATEST_VERSION = steps.readFile('VERSION').trim() released = !versionFileExistsBefore || (currentVersion != previousVersion)
steps.env.SEMANTIC_RELEASED = true if (released) {
steps.log.info("SemanticReleasingExecutor", "Semantic release completed, version: ${steps.env.LATEST_VERSION}") steps.env.LATEST_VERSION = steps.readFile('VERSION').trim()
} else if (versionFileExists) { steps.env.SEMANTIC_RELEASED = true
steps.log.warn("SemanticReleasingExecutor", "VERSION file exists but was not modified recently, skipping release") steps.log.info("SemanticReleasingExecutor", "Semantic release completed, version: ${steps.env.LATEST_VERSION}")
} else {
steps.log.warn("SemanticReleasingExecutor", "No new version released, current version remains: ${currentVersion}")
}
} else {
steps.log.warn("SemanticReleasingExecutor", "VERSION file not found, semantic release may have failed ?")
} }
} }
} }