Teamcity Kotlin Scripts rename IDs - kotlin

I have an auto generated Kotlin Script.
steps {
script {
name = "Style check"
id = "RUNNER_633"
enabled = false
scriptContent = """
#!/bin/bash
make docker run="make ci lint"
""".trimIndent()
}
script {
name = "Build code"
id = "RUNNER_662"
scriptContent = """
#!/bin/bash
make docker run="make ci"
""".trimIndent()
}
stepsOrder = arrayListOf("RUNNER_1213", "RUNNER_1228", "RUNNER_633", "RUNNER_662", "RUNNER_642")
}
I really don't like the names RUNNER_633, RUNNER_1228 - which are values sometimes referenced in other kotlin script files.
Can I rename them? What if I make mistake with the variable value? Is it possible to catch this before deploy and update of the teamcity?

The Versioned Settings configuration has an option to also use settings from VCS. In this case you can push your changes of the settings to a branch and test them out by selecting your branch name in your build configuration.

Related

Cloudflare Wrangler "No KV Namespaces configured!"

I'm new to Cloudflare/Wrangler, but it seems like the documentation is missing something as
following the directions don't seem to work.
Starting from here:
https://developers.cloudflare.com/workers/wrangler/workers-kv/
I run the first command as wrangler kv:namespace create apikeys
🌀 Creating namespace with title "emailvalidator-apikeys"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
{ binding = "apikeys", id ="4818.........aa2c" }
I have a wrangler.toml file, and I add it to the kv_namespaces array:
kv_namespaces = [
{ binding = "apikeys", id = "4818.........aa2c" }
]
I attempt to add a key/value entry with Wrangler: wrangler kv:key put --binding=apikeys "MYKEY" "MYKEYVALUE"
✘ [ERROR] No KV Namespaces configured! Either use --namespace-id to upload directly or add a KV namespace to your wrangler config file.
What am I missing? I already validated in the Cloudflare platform that the namespace does exist, named as expected from the documentation as emailvalidator-apikeys.
Do you have your full wrangler.toml available? TOML has table inheritance which can cause issues where your kv_namespaces key isn't actually top-level or under an environment.
As an example:
name = "foo"
[triggers]
cron = ["* * * * *"]
kv_namespaces = [
{...}
]
This wouldn't work as kv_namespaces is now apart of the triggers table, so you would want to move it above or instead use the [[kv_namespaces]] syntax.
[[kv_namespaces]]
binding = "..."
id = "..."

Provide NextFlow workflow inputs (not parameters) via the CLI

I have the following (simplified) nextflow module. It has one process, which runs a multiple sequence alignment on a fasta file, and a workflow that runs this process (eventually it will run other processes too):
process clustal_omega_msa {
input:
path fastas
output:
path 'clustal.sto'
script:
"""
cat ${fastas} > merged.fa
clustalo -infile merged.fa --outfmt=stockholm
"""
container "https://depot.galaxyproject.org/singularity/clustalo:1.2.4--h1b792b2_4"
}
workflow msa {
take:
path fastas
main:
clustal_omega_msa(fastas)
}
I want this workflow to be both importable as a sub-workflow, and also executable directly. For this reason I have specified no parameters, and only used inputs (because I believe parameters can't be specified when calling a subworkflow).
However, I can see no way to run this subworkflow directly on the command line.
If I run nextflow run msa.nf -entry msa I get the following error:
No such variable: fastas
-- Check script 'msa.nf' at line: 1 or see '.nextflow.log' file for more details
This makes sense - I haven't specified where these files come from. But how can I? If I follow the config part of the docs and create a nextflow.config with the following contents:
fastas = "/some/path/to/*.fasta"
I still get this error. I am also aware there is a -params-file option, but I believe that only works for parameters, not inputs.
Implicit workflow definitions are ignored when a script is imported as module. This means that your workflow script that can be used either as a library module or as an application script:
nextflow.enable.dsl=2
params.input_fasta_files = './data/*.fasta'
process clustal_omega_msa {
input:
path fastas
output:
path 'clustal.sto'
"""
cat ${fastas} > merged.fa
clustalo -infile merged.fa --outfmt=stockholm
"""
}
workflow msa {
take:
fasta_files
main:
clustal_omega_msa(fasta_files)
}
workflow {
input_fasta_files = Channel.fromPath( params.input_fasta_files ).collect()
msa( input_fasta_files )
}
Note that if you were to move the 'msa' sub-workflow into a separate file, for example called 'msa.nf', you could then just import it and specify any required params to it using the addParams option. For example:
nextflow.enable.dsl=2
include { msa } from './path/to/msa.nf' addParams(foo: 'bar')
params.input_fasta_files = './data/*.fasta'
workflow {
input_fasta_files = Channel.fromPath( params.input_fasta_files ).collect()
msa(input_fasta_files)
}

Teamcity Kotlin Build Base Class Property Extension

I've extracted my Teamcity builds as Kotlin outputs. I want to create a base class that defines a number of common steps/settings, but allow individual builds to extend these properties.
e.g.
open class BuildBase(init: BuildBase.() -> Unit) : BuildType({
steps {
powerShell {
name = "Write First Message"
id = "RUNNER_FirstMessage"
scriptMode = script {
content = """
Write-Host "First Message"
""".trimIndent()
}
}
}
})
object Mybuild : BuildBase({
steps { // This would add a new step to the list, without wiping out the original step
powerShell {
name = "Write Last Message"
id = "RUNNER_LastMessage"
scriptMode = script {
content = """
Write-Host "Last Message"
""".trimIndent()
}
}
}
})
In this example, I want to inherit the step from the base class, but add additional steps relevant to the specific build. Additionally, I'd want to inherit the base disableSettings (if any) and disable other steps.
Is this even possible? if so, how would I go about structuring the classes to enable it?
You might have found a solution already but here's how I would solve your problem.
Like in the GUI, TeamCity supports build templates.
In your case you would have a template like following:
object MyBuildTemplate: Template({
id("MyBuildTemplate")
name = "My build template"
steps {
powerShell {
name = "Write First Message"
id = "RUNNER_FirstMessage"
scriptMode = script {
content = """
Write-Host "First Message"
""".trimIndent()
}
}
}
})
Then, you can define a build config extending this template:
object MyBuildConfig: BuildType({
id("MyBuildConfig")
name = "My build config"
steps { // This would add a new step to the list, without wiping out the original step
powerShell {
name = "Write Last Message"
id = "RUNNER_LastMessage"
scriptMode = script {
content = """
Write-Host "Last Message"
""".trimIndent()
}
}
// afaik TeamCity would append the build config's steps to the template's steps but there is way to explicitly define the order of the steps:
stepsOrder = arrayListOf("RUNNER_FirstMessage", "RUNNER_LastMessage")
}
})
This way, you should also be able to inherit disableSettings from the template.

How to pass parameters or arguments into a Gradle task?

I have a Gradle build script into which I am trying to include Eric Wendelin's CSS plugin.
It's easy enough to implement, and because I only want minification (rather than combining and gzipping), I've got the pertinent parts of the build script looking like this:
minifyCss {
source = "src/main/webapp/css/brandA/styles.css"
dest = "${buildDir}/brandA/styles.css"
yuicompressor {
lineBreakPos = -1
}
}
war {
baseName = 'ex-ren'
}
war.doFirst {
tasks.myTask.minifyCss.execute()
}
This is perfect - when I run the gradle war task, it calls the minifyCss task, takes the source css file, and creates a minified version in the buildDir
However, I have a handful of css files which need minify-ing, but not combining into one file (hence I'm not using the combineCss task)
What I'd like to be able to do is make the source and dest properties (assuming that's the correct terminology?) of the minifyCss task reference variables of some sort - either variables passed into the task in the signature, or global variables, or something ...
Something like this I guess (which doesn't work):
minifyCss(sourceFile, destFile) {
source = sourceFile
dest = destFile
yuicompressor {
lineBreakPos = -1
}
}
war {
baseName = 'ex-ren'
}
war.doFirst {
tasks.myTask.minifyCss.execute("src/main/webapp/css/brandA/styles.css", "${buildDir}/brandA/styles.css")
tasks.myTask.minifyCss.execute("src/main/webapp/css/brandB/styles.css", "${buildDir}/brandB/styles.css")
tasks.myTask.minifyCss.execute("src/main/webapp/css/brandC/styles.css", "${buildDir}/brandC/styles.css")
}
This doesn't work either:
def sourceFile = null
def destFile = null
minifyCss {
source = sourceFile
dest = destFile
yuicompressor {
lineBreakPos = -1
}
}
war {
baseName = 'ex-ren'
}
war.doFirst {
sourceFile = "src/main/webapp/css/brandA/styles.css"
destFile = "${buildDir}/brandA/styles.css"
tasks.myTask.minifyCss.execute()
}
For the life of me I cannot work out how to call a task and pass variables in :(
Any help very much appreciated;
You should consider passing the -P argument in invoking Gradle.
From Gradle Documentation :
--project-prop
Sets a project property of the root project, for example -Pmyprop=myvalue. See Section 14.2, “Gradle properties and system properties”.
Considering this build.gradle
task printProp << {
println customProp
}
Invoking Gradle -PcustomProp=myProp will give this output :
$ gradle -PcustomProp=myProp printProp
:printProp
myProp
BUILD SUCCESSFUL
Total time: 3.722 secs
This is the way I found to pass parameters.
If the task you want to pass parameters to is of type JavaExec and you are using Gradle 5, for example the application plugin's run task, then you can pass your parameters through the --args=... command line option. For example gradle run --args="foo --bar=true".
Otherwise there is no convenient builtin way to do this, but there are 3 workarounds.
1. If few values, task creation function
If the possible values are few and are known in advance, you can programmatically create a task for each of them:
void createTask(String platform) {
String taskName = "myTask_" + platform;
task (taskName) {
... do what you want
}
}
String[] platforms = ["macosx", "linux32", "linux64"];
for(String platform : platforms) {
createTask(platform);
}
You would then call your tasks the following way:
./gradlew myTask_macosx
2. Standard input hack
A convenient hack is to pass the arguments through standard input, and have your task read from it:
./gradlew myTask <<<"arg1 arg2 arg\ in\ several\ parts"
with code below:
String[] splitIntoTokens(String commandLine) {
String regex = "(([\"']).*?\\2|(?:[^\\\\ ]+\\\\\\s+)+[^\\\\ ]+|\\S+)";
Matcher matcher = Pattern.compile(regex).matcher(commandLine);
ArrayList<String> result = new ArrayList<>();
while (matcher.find()) {
result.add(matcher.group());
}
return result.toArray();
}
task taskName, {
doFirst {
String typed = new Scanner(System.in).nextLine();
String[] parsed = splitIntoTokens(typed);
println ("Arguments received: " + parsed.join(" "))
... do what you want
}
}
You will also need to add the following lines at the top of your build script:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;
3. -P parameters
The last option is to pass a -P parameter to Gradle:
./gradlew myTask -PmyArg=hello
You can then access it as myArg in your build script:
task myTask {
doFirst {
println myArg
... do what you want
}
}
Credit to #789 for his answer on splitting arguments into tokens
I would suggest the method presented on the Gradle forum:
def createMinifyCssTask(def brand, def sourceFile, def destFile) {
return tasks.create("minify${brand}Css", com.eriwen.gradle.css.tasks.MinifyCssTask) {
source = sourceFile
dest = destFile
}
}
I have used this method myself to create custom tasks, and it works very well.
task mathOnProperties << {
println Integer.parseInt(a)+Integer.parseInt(b)
println new Integer(a) * new Integer(b)
}
$ gradle -Pa=3 -Pb=4 mathOnProperties
:mathOnProperties
7
12
BUILD SUCCESSFUL
Its nothing more easy.
run command: ./gradlew clean -PjobId=9999
and
in gradle use: println(project.gradle.startParameter.projectProperties)
You will get clue.
I think you probably want to view the minification of each set of css as a separate task
task minifyBrandACss(type: com.eriwen.gradle.css.tasks.MinifyCssTask) {
source = "src/main/webapp/css/brandA/styles.css"
dest = "${buildDir}/brandA/styles.css"
}
etc etc
BTW executing your minify tasks in an action of the war task seems odd to me - wouldn't it make more sense to make them a dependency of the war task?
Here is a solution for Kotlin DSL (build.gradle.kts).
I first try to get the variable as a property and if it was null try to get it from OS environment variables (can be useful in CIs like GitHub Actions).
tasks.create("MyCustomTask") {
val songName = properties["songName"]
?: System.getenv("SONG_NAME")
?: error("""Property "songName" or environment variable "SONG_NAME" not found""")
// OR getting the property with 'by'. Did not work for me!
// For this approach, name of the variable should be the same as the property name
// val songName: String? by properties
println("The song name: $songName")
}
We can then pass a value for the property from command line:
./gradlew MyCustomTask -PsongName="Black Forest"
Or create a file named local.properties at the root of the project and set the property:
songName=Black Forest
We can also add an env variable named SONG_NAME with our desired value and then run the task:
./gradlew MyCustomTask

Android studio | Dependency Management

Can anyone suggest, how can we add a dependency at build time in android gradle based on some condition like:
dependencies{
if(someCondition){
// add dependency
}
}
Thanks in advance!!
I found a solution for this:
Step1: Declare a boolean variable in gradle at root level.
like: def someDependencyEnabled = true //This could be dynamically set.
Step2: Using this boolean variable we can apply a check like:
if(someDependencyEnabled){
//Add some dependency
}
else
{
//Add some other dependency
}
Step3: Define Different source set for different situations:
android.sourceSets {
main {
java.srcDirs = ['src/main/java', someDependencyEnabled ? 'src/dependency_enabled_src' : 'src/dependency_disabled_src']
}
}
where:
'src/main/java' : is the common src file which contain common code.
'src/dependency_enabled_src': is the source folder that contain dependency specific code. which is further used by 'src/main/java'.
'src/dependency_disabled_src': is the source folder that contain alternate code when particular dependency is disabled.
In my case I wrote same name classes, methods & package name in both folders (dependency_enabled & dependency_disabled src) and wrote methods with desired implementation in dependency_enabled_src & empty methods for dependency_disabled_src.