A Jenkins plugin to use Azure CLI for managing Azure resources.
:exclamation: This is NOT an official Microsoft plugin
:star2: The advantage of this plugin that it let's you export the CLI result from each command to environment variables and to the next command.
You can install/update this plugin in Jenkins update center (Manage Jenkins -> Manage Plugins, search Azure CLI Plugin).
You can also manually install the plugin if you want to try the latest feature before it's officially released. To manually install the plugin:
mvn package
azure-cli.hpi
in target
folder of your repo, click Upload.To use this plugin, first you need to have an Azure Service Principal in your Jenkins instance.
az vm create -n MyLinuxVM -g MyResourceGroup --image UbuntuLTS --data-disk-sizes-gb 10 20
az vm create -n ${VM_NAME} -g MyResourceGroup --image UbuntuLTS --data-disk-sizes-gb 10 20
The CLI command output is JSON based:
Output:
{
"fqdns": "",
"id": "/subscriptions/some-guid/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/virtualMachines/MyLinuxVM",
"location": "northeurope",
"macAddress": "00-0D-AA-AA-AA-AA",
"powerState": "VM running",
"privateIpAddress": "10.0.0.4",
"publicIpAddress": "52.178.0.0",
"properties": {
"provisioningState": "Succeeded"
},
"resourceGroup": "MyResourceGroup"
}
If you want to export a property to an environment variable that you can use in other build steps, define the parameters in the "advanced" section:
/publicIpAddress|PUBLIC_IP
The '/publicIpAddress' is the path in the JSON and the 'PUBLIC_IP' is the environment variable that will be created. /properties/provisioningState|STATE
/publicIpAddress|PUBLIC_IP , /properties/provisioningState|STATE
You can also use this plugin in pipeline (Jenkinsfile). Here are some samples to use the plugin in pipeline script:
To create a new resource group and provision a new VM:
azureCLI commands: [[exportVariablesString: '', script: 'az group create -n MyResourceGroup --location northeurope'], [exportVariablesString: '/publicIpAddress|PUBLIC_IP', script: 'az vm create -n ${VM_NAME} -g MyResourceGroup --image UbuntuLTS --data-disk-sizes-gb 10 20']], principalCredentialId: '<credential_id>'
For advanced options, you can use Jenkins Pipeline Syntax tool to generate a sample script.
You can also use this plugin with using the Jobs DSL. For example:
To create a linux VM using the CLI:
job('AzCommand') {
steps {
azCommands('servicePrincipalId',
['az vm create -n MyLinuxVM -g MyResourceGroup --image UbuntuLTS --data-disk-sizes-gb 10 20 && /publicIpAddress|PUBLIC_IP'])
}
}