Skip to content

Commit

Permalink
Merge pull request #10 from Liftric/feature/initial_release_wait
Browse files Browse the repository at this point in the history
optional initialWaitSeconds
  • Loading branch information
Ingwersaft authored Dec 17, 2021
2 parents 5149c38 + 4217c11 commit 7f15cae
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 9 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,12 @@ Basically if the upload or progression triggers a release in octopus deploy, the

Configuration is done on the tasks themself:

Property | Description | default value
Property | Description | default value
---|---|---
waitForReleaseDeployments | If the task should wait | false
waitTimeoutSeconds | max time to poll the Ocotopus API | 600
delayBetweenChecksSeconds | how long to delay between polls | 5
waitForReleaseDeployments | If the task should wait | false
waitTimeoutSeconds | max time to poll the Ocotopus API | 600
delayBetweenChecksSeconds | how long to delay between polls | 5
initialWaitSeconds | initial delay before waitForReleaseDeployments logic starts | -

Example:
```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.liftric.octopusdeploy.task

import com.liftric.octopusdeploy.apiKey
import com.liftric.octopusdeploy.getPackageResponse
import com.liftric.octopusdeploy.serverUrl
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertNotNull
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.slf4j.LoggerFactory
import kotlin.random.Random

class UploadPackageTaskWithInitialWaitIntegrationTest {
private val log = LoggerFactory.getLogger(UploadPackageTaskWithInitialWaitIntegrationTest::class.java)
@get:Rule
val testProjectDir = TemporaryFolder()

@Test
fun testExecute() {
val major = Random.Default.nextInt(0, 100)
val minor = Random.Default.nextInt(0, 100)
val micro = Random.Default.nextInt(0, 100)
log.info(testProjectDir.root.absolutePath)
setupBuild(major, minor, micro)

val result = GradleRunner.create()
.forwardOutput()
.withProjectDir(testProjectDir.root)
.withArguments("build", "uploadPackage")
.withPluginClasspath()
.build()
log.info(result.output)
assertEquals(TaskOutcome.SUCCESS, result.task(":uploadPackage")?.outcome)

val packageItem = getPackageResponse()
.items
?.firstOrNull {
it.version == "$major.$minor.$micro"
}
assertNotNull(packageItem)
assertEquals(".jar", packageItem?.fileExtension)

val secondResult = GradleRunner.create()
.forwardOutput()
.withProjectDir(testProjectDir.root)
.withArguments("build", "uploadPackage")
.withPluginClasspath()
.buildAndFail()
log.info(secondResult.output)
// override is not set
assertEquals(TaskOutcome.FAILED, secondResult.task(":uploadPackage")?.outcome)
}

fun setupBuild(major: Int, minor: Int, micro: Int) {
testProjectDir.newFile("build.gradle.kts").apply {
writeText(
"""
import com.liftric.octopusdeploy.task.UploadPackageTask
plugins {
java
id("com.liftric.octopus-deploy-plugin")
}
group = "com.liftric.test"
version = "$major.$minor.$micro"
tasks {
withType<Jar> {
archiveFileName.set(
"${'$'}{archiveBaseName.get()
.removeSuffix("-")}.${'$'}{archiveVersion.get()}.${'$'}{archiveExtension.get()}"
)
}
withType<UploadPackageTask>{
initialWaitSeconds.set(2L)
waitForReleaseDeployments.set(true)
waitTimeoutSeconds.set(1L)
delayBetweenChecksSeconds.set(2L)
httpLogLevel.set(okhttp3.logging.HttpLoggingInterceptor.Level.HEADERS)
}
}
octopus {
serverUrl.set("$serverUrl")
apiKey.set("$apiKey")
generateChangelogSinceLastTag = true
val jar by tasks.existing(Jar::class)
packageName.set(jar.get().archiveBaseName.get().removeSuffix("-"))
version.set(jar.get().archiveVersion.get())
pushPackage.set(jar.get().archiveFile)
}
"""
)
}
testProjectDir.root.setupGitRepoCopy()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ class OctopusDeployPlugin : Plugin<Project> {
}
project.tasks.withType(PromoteReleaseTask::class.java) {
project.afterEvaluate {
apiKey.set(extension.apiKey ?: error("$extensionName: didn't specify apiKey!"))
octopusUrl.set(extension.serverUrl ?: error("$extensionName: didn't specify serverUrl!"))
apiKey.set(extension.apiKey)
octopusUrl.set(extension.serverUrl)
httpLogLevel.set(extension.httpLogLevel)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ open class PromoteReleaseTask : DefaultTask() {
@Optional
val waitTimeoutSeconds: Property<Long> = project.objects.property()

/**
* Octopus server might need longer for the deployment to trigger, so we can define an
* additional, initial, minimum wait before the actual release check logic even happens
*/
@Input
@Optional
val initialWaitSeconds: Property<Long> = project.objects.property()

@Input
@Optional
val delayBetweenChecksSeconds: Property<Long> = project.objects.property()
Expand Down Expand Up @@ -98,7 +106,8 @@ open class PromoteReleaseTask : DefaultTask() {
fromEnv = fromValue,
toEnv = toValue
)
}
},
initialWaitSeconds = initialWaitSeconds.orNull
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ open class UploadPackageTask : DefaultTask() {
@Optional
val waitTimeoutSeconds: Property<Long> = project.objects.property()

/**
* Octopus server might need longer for the deployment to trigger, so we can define an
* additional, initial, minimum wait before the actual release check logic even happens
*/
@Input
@Optional
val initialWaitSeconds: Property<Long> = project.objects.property()

@Input
@Optional
val delayBetweenChecksSeconds: Property<Long> = project.objects.property()
Expand Down Expand Up @@ -92,7 +100,8 @@ open class UploadPackageTask : DefaultTask() {
apiLogLevel = httpLogLevel.getOrElse(HttpLoggingInterceptor.Level.NONE),
checkLogic = {
determinAnyOngoingTask(packageNameValue = packageNameValue, versionValue = versionValue)
}
},
initialWaitSeconds = initialWaitSeconds.orNull
)
}
}
11 changes: 10 additions & 1 deletion src/main/kotlin/com/liftric/octopusdeploy/task/shared.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ import com.liftric.octopusdeploy.rest.*
import kotlinx.coroutines.*
import okhttp3.logging.HttpLoggingInterceptor
import java.time.Duration
import kotlin.time.seconds

/**
* repeatedly poll the octopus api for all releases for the given [packageNameValue] [versionValue] combination.
*
* @param checkLogic Remoting used to determin if any task is ongoing.
* Difference between upload and progress makes this necessary.
* @param initialWaitSeconds Octopus server might need longer for the deployment to trigger, so we can define an
* additional, initial, minimum wait before the actual release check logic even happens
*/
internal fun awaitReleaseLogic(
octopusUrlValue: String,
apiKeyValue: String,
waitTimeoutSeconds: Long,
delayBetweenChecksSeconds: Long,
apiLogLevel: HttpLoggingInterceptor.Level,
checkLogic: OctoApiClient.() -> Boolean
checkLogic: OctoApiClient.() -> Boolean,
initialWaitSeconds: Long?
) {
println("checking is upload triggered any release with auto deployment (${waitTimeoutSeconds}s max)")

Expand All @@ -33,6 +37,11 @@ internal fun awaitReleaseLogic(
octoApiClient.shutdown()
}
runBlocking(handler) {
initialWaitSeconds?.let {
println("initial check delay: ${it}s")
delay(it * 1000)
println("initial check delay done")
}
withTimeout(Duration.ofSeconds(waitTimeoutSeconds).toMillis()) {
var anyOngoingTask = octoApiClient.checkLogic()
while (anyOngoingTask) {
Expand Down

0 comments on commit 7f15cae

Please sign in to comment.