The Jenkins Anka Plugin provides a quick way to integrate Anka Build Cloud with Jenkins. The plugin helps Jenkins jobs dynamically provision Anka VM instances (based on the label used) for building, testing, and more.
In order to follow these instructions, you will need to install the Anka CLI and an understanding of how to start the VM and launch the viewer.
The Jenkins Anka Plugin requires a VM with Java, SSH Remote Login, and port forwarding:
System Properties
) and look for java.version
.java.version
is set to 1.8.0_242
, you can download and install the AdoptOpenJDK jdk8u242-b08.pkg.You can run any version of JAVA you want as long as the version inside of the VM matches your Jenkins server
System Preferences > Sharing
).sudo anka suspend <VM Template name>
sudo anka registry push <VM Template name> <Tag name>
These steps are based on Jenkins 2.222.1. Your version and UI may differ slightly.
Navigate to Manage Jenkins > Manage Plugins
and click on the Available tab. Search in Filter for “Anka”, then install it. You won't see any results if it's already installed.
Now you can configure the Anka Cloud. Navigate to Manage Jenkins > Manage Nodes and Clouds
and find the section called Configure Clouds
(on older versions you can find this under Configure System
). Inside of Configure Clouds
, click on Add a new Cloud at the bottom of the page and then click on Anka Cloud.
You now have an Anka Cloud panel exposed to do your configuration. You can set Anka Build Cloud to be anything you want. However, you'll need to set Build Controller URL with port to the proper URL of your controller, and make sure you include the port: http://<ip/domain>:80
. Once you're done, click Save.
At this point you can either setup Static Labels under Configure Cloud or use Dynamic Labelling in your Jenkinsfile.
Return to Manage Nodes and Clouds > Configure Clouds
. You should now see a VM Templates and Slave Templates section. Make sure Show Templates is checked and click on Add. This creates a new Slave Template you can edit.
Under the Slave Template, you want to select the proper Template and Tag or leave it to the default value to select the latest Tag in the Registry.
Leave # of Executors to 1.
Enter the VM user's home path in Remote FS Root. If you didn't set up a new user for Jenkins on the VM, you could use the default anka user: /Users/anka/
.
Enter a value in Labels that you can then use in your jobs.
Select the SSH or JNLP method for connection between Jenkins and your Anka VMs.
anka
and pass: admin
./tmp/log.txt
for errors.Enter a value for Slave name template. Provisioned VMs names will contain this value.
You can also pass Environment Variables into the VM.
Select a Node Group if you want VMs provisioned on a specific group defined in your Anka Build Cloud. Available only in Enterprise and Enterprise Plus Tiers.
Enter Priority for VM provisioning. Available only in Enterprise and Enterprise Plus Tiers.
Save the settings.
You can create multiple Slave Template profiles!
Dynamic Labelling requires two plugins: GitHub Authentication plugin and Pipeline.
This section describes the steps to create dynamic labels inside of your Jenkinsfile using the createDynamicAnkaNode
function.
createDynamicAnkaNode
function starts a VM and returns a unique label for it.createDynamicAnkaNode
has all the configuration options that the UI configured Static Slave Template has.Jenkins has a Pipeline Snippet Generator, which helps you craft your
createDynamicAnkaNode
definition. You can find it in your Jenkins instance at/pipeline-syntax
.
CreateDynamicAnkaNode
ParametersYou can obtain the “masterVmId” (VM UUID) using:
sudo anka show <Template name> uuid
Name | Type | Default Value | Description | Required |
---|---|---|---|---|
masterVmId | String | - | UUID of the VM Template | Yes |
tag | String | - | VM Template Tag name | - |
remoteFS | String | Users/anka | Remote node workspace | - |
launchMethod | String | jnlp | Node launch method: ‘ssh’ or ‘jnlp’ | - |
credentialsId | String | - | Jenkins credential ID for ssh launch method | - |
extraArgs | String | - | String to be appended to JNLP command | - |
javaArgs | String | - | String to append to JNLP java args | - |
jnlpJenkinsOverrideUrl | String | - | Override the Jenkins server url set in the Jenkins configuration | - |
jnlpTunnel | String | - | JNLP tunnel to use for node launcher | - |
keepAliveOnError | boolean | false | Keep the VM instance alive after a failed build | - |
timeout | int | 1200 | Timeout for starting the instance (in seconds) | - |
environments | List of tuples | - | List of environment variables to add for the build: [[name: 'FOO', value: 'BAR'], [name: 'OR', value: 'IS']] | - |
nameTemplate | string | - | Label to use in VM instance names (There are several variables available for interpolation: $Template_name, $Template_id, or $ts) | - |
priority | int | 1000 | Override the default priority (lower is more urgent). Available only in Enterprise and Enterprise Plus Tiers | - |
saveImage | boolean | false | Save the VM as a Tag before terminating it | - |
suspend | boolean | false | When saving the Tag, suspend the VM before the push | - |
TemplateId | string | - | When saving the Tag, push onto a specific Template UUID | - |
pushTag | string | - | When saving the Tag, set the Tag name (A timestamp will be appended) | - |
deleteLatest | boolean | false | When saving the Tag, delete the latest Tag for the Template out of the registry before pushing (Dangerous: only use if the Template isn't holding other project tags) | - |
TemplateDescription | string | - | When saving the Tag, set a description for the new Tag | - |
group | string | - | Group ID to start the instance in (Available only in Enterprise and Enterprise Plus Tiers) | - |
numberOfExecutors | int | 1 | Number of Jenkins executors to run on the Node (we recommend 1) | - |
description | string | - | On creation of the instance, set the description | - |
labelString | string | - | Override the returned label (not recommended) | - |
dontAppendTimestamp | boolean | false | Do not append timestamp to the tag being pushed | - |
def NODE_LABEL = createDynamicAnkaNode(
masterVmId: 'e1173284-39df-458c-b161-a54123409280'
)
pipeline {
agent {
label "${NODE_LABEL}"
}
stages {
stage("hello") {
steps {
sh "echo hello"
}
}
}
}
Cache Tag building is useful when you need to automate the preparation of a VM with dependencies. You can then execute your jobs using this Tag/VM and avoid having to prepare the dependencies over and over again before the build, or test commands run, usually resulting in significant bandwidth and time savings.
The plugin will push to the Registry for any buildStatus except for FAILURE, ABORTED, or UNSTABLE. You can wrap your step-level commands in
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE')
or use the Build Execute shell advanced Exit code to set build unstable setting to prevent the VM from pushing.
You can find the Cache Builder in the Anka Cloud Slave Template definition under Manage Jenkins > Manage Nodes and Clouds > Configure Clouds
.
Target Template : A dropdown list of Templates and UUIDs.
Tag : Specify a name for the Tag here. However, the current date is appended onto the end: If I use project1
in this field, I'll end up with a Tag named project1_20200408121026
in my Registry. (Optional; If left empty, it uses the Jenkins jobname)
Description : Description for the VM being pushed (Optional).
Suspend : If selected, it will suspend the VM before pushing the Tag. Otherwise, it will stop the VM before pushing.
Delete Latest Tag : Deletes the latest Tag from the Template/Registry before pushing the newly generated Tag. This is useful to optimize disk space usage.
WARNING: This can delete Tags unrelated to your cache builder configuration.
Wait For Build Finish : This causes the plugin to wait until the entire to job finish before pushing the new Tag to the Registry.
WARNING: Using the
Wait for Build Finish
option in combination with ankaGetSaveImageResult results in a deadlock.
Fail build : Will set the build as failed if the image save request
failed (or timed out). If this is not selected, the Post Build Step does nothing.
Timeout : Sets the minutes to wait for the Tag push to Registry to complete (this needs to be determined manually or with trial and error).
Pipelines can have multiple agents running in one build (also in parallel). Therefore, the plugin relies on the buildResult to tell if it needs to execute the “save image request”. You have two options to prevent pushing the new Tag prematurely:
stage("run-on-NESTED_LABEL-vm") {
agent { label "${NESTED_LABEL}" }
steps {
// If buildResults == 'FAILURE', Anka will not push the NESTED_LABEL VM. Example:
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
sh 'uname -r;'
}
}
}
Please review our Nested Cache Builder Example.
If buildResult receives any of the failed statuses (FAILURE, ABORTED, or UNSTABLE), the ankaGetSaveImageResult outputs
Checking save image status… Done!
in the Jenkins job logs/console since there is no push operation to wait on.
Create an ankaGetSaveImageResult
Pipeline step:
stage("check-generated-tag-from-nested-vm") {
steps {
script {
def getPushResult = ankaGetSaveImageResult( shouldFail: true, timeoutMinutes: 120 )
echo "ankaGetSaveImageResult Returned: $getPushResult"
}
}
}
If you're creating multiple cache Tags in a single pipeline, The ankaGetSaveImageResult
function returns true if all previous image save calls for this particular build have executed.
Name | Type | Default Value | Decription | Required |
---|---|---|---|---|
shouldFail | boolean | true | Fails the job if one of the image save requests has failed or timed out | |
timeoutMinutes | int | 120 | Stops waiting for the result of the Tag -> Registry push after x minutes. |
Remember,
ankaGetSaveImageResult
returns true immediately if nothing pushes to the Registry in a failed build.
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.