Using Azure CLI in Jenkins pipeline - azure

I'm trying to use the Jenkins pipeline with Azure using this tutorial: https://learn.microsoft.com/en-us/azure/jenkins/execute-cli-jenkins-pipeline with the exception that I have Jenkins on localhost (running Win7).
The build works as expected, but deployment fails on the line in Jenkinsfile where the 'az login...' script should run, with a 'command not found' error.
withCredentials([azureServicePrincipal('...')]) {
sh '''
'az login ..'
I am able to use the 'az' commands in the command line outside of Jenkins.
Am I missing something? Any help would be appreciated.

You need to add Azure service principal to Jenkins credential. Please refer to the link you provided.
If you don't have a service principal, you could use Azure CLI 2.0 to create a new. Please refer to this link:Create an Azure service principal with Azure CLI 2.0.
withCredentials([azureServicePrincipal('<mySrvPrincipal>')])
<mySrcvPrincipal> is credential id you set.
Note:
When you add Azure service principal to Jenkins credential, click Verify Service Principal, please ensure you also see Successfully verified the Microsoft Azure Service Principal.

Related

Azure CLI not found in Jenkins

I have install azure cli plugins in jenkins after that i setup Azure Service Principal and got Successfully verified the Microsoft Azure Service Principal after that i create a new job and added az version to check az is installed or not in it and got failed error below is the screenshot of that error
Note:- My Jenkins is having v2.332.1 and installed in AKS Cluster as a Helm chat my jenkins is running
Here is the screenshot of plugin installed
Here is the my job new create in freestylejob
Please help out with this issue
You need to install azure cli in the Jenkins Host as well.
from
https://plugins.jenkins.io/azure-cli/ - Prerequisites
To use this plugin, first you need to have an Azure Service Principal
in your Jenkins instance.
1.Create an Azure Service Principal through Azure CLI or Azure portal.
2.Open Jenkins dashboard, go to Credentials, add a new Microsoft Azure Service Principal with the credential information you just created.
3.Install Azure CLI in the Jenkins Host
from your description, I can see you have 1,2 steps completed and possibly 3 is missing.

ARM template deployment scripts Azure login

I'm trying to use a Microsoft.Resources/deploymentScripts in my ARM template that will execute some Azure Powershell commands.
Obviously, without specifying any identity attribute, I need to run Connect-AzAccounts in my script.
My question is, how can I retrieve the Service Principal credentials and pass this to my ARM DeploymentScripts?
I'm assuming that the identity you're using to deploy your ARM (the "Deployment Principal") is the same as the one running your Deployment Script (the "Deployment Script Principal"). See this link.
Here's what I did. I specify the "Deployment Service Principal" credentials as ARM parameters, and then pass these to the Deployment Script as secure environment variables (I, too, am using one Principal for both the ARM and the Deployment Script).
This does mean that my ARM Deployment pipeline looks more or less like this:
az login --service-principal -u ${appId} -p ${clientSecret} ...
az deployment group create ... --parameters appId=${appId} clientSecret=${clientSecret}
In other words: I'm specifying the Service Principal Credentials twice:
to login using the Deployment Principal
to pass the Deployment Principal credentials to the Deployment Script
Assuming az login doesn't cache your Service Principal Credentials (let's hope it doesn't!), there is no way to 'fetch' those credentials dynamically, I think.

Using Azure-Cli to deploy an azure function to azure from Jenkins

I have created a docker container and generated final artifact of my azure function app code. Azure Cli is already installed in the container. Is it possible to deploy to Azure using Azure Cli and pass the credentials along?
I tried the following command (example credentials):
az functionapp deployment user set --user-name "MY_USER" --password "MY_PASS" --subscription "MY_SUBSCRIPTION"
But it results in the following error:
Subscription 'MY_SUBSCRIPTION' not recognized. ValidationError: Please run 'az login' to setup account.
If I login (which requires entering a pass code to a browser), then the command above seems to be working.
This is going to be part of CI/CD and manual login is not a solution. Any ideas?
You would need to use a service principle to authenticate. But since you mentioned you are using Jenkins, fortunately there are rich set of plugins available for different Azure resources which can handle the authentication for you if you setup in your Jenkins dashboard. For example, in this case you are using az cli for which you can install https://plugins.jenkins.io/azure-cli/
Yes, you need to be authenticated. Here you have several authentication options: Sign in with Azure CLI
Authenticating with a service principal is the best way to write secure scripts or programs
Sign in with a service principal

Running Azure CLI script from release pipeline using Service Principal - invalid_client

I have an Azure CLI script which runs perfectly well in the Cloud Shell but I run into trouble when I try to include it in a devops release pipeline. For debugging purposes I've reduced the script to simply signing-in as a Service Principal and then retrieving the version...
az login --service-principal -u http://[Service Principal name] -p [Service Principal password] --tenant [Service Principal tenant GUID]
az --version
...but it still incurs the same error.
Here are the properties of my Azure CLI task:
Script Type: Shell
Script Location: Inline script
Inline Script: [as above]
Script Arguments:
Access service principal details in script: false
Use global Azure CLI configuration: false
Working Directory:
Fail on Standard Error: false
Enabled: true
Continue on error: false*
Environment Variables:
And the error is:
2020-06-15T12:46:39.8710944Z ##[error]Error Code: [1]
2020-06-15T12:46:39.8724737Z ##[error]Error: Azure login failed
2020-06-15T12:46:39.8728448Z ##[error]Script failed with error: Get Token request returned http error: 401 and server response: {"error":"invalid_client","error_description":"AADSTS7000222: The provided client secret keys are expired. Visit the Azure Portal to create new keys for your app, or consider using certificate credentials for added security: https://learn.microsoft.com/azure/active-directory/develop/active-directory-certificate-credentials
Trace ID: d0f42793-739e-4ce9-9118-5049086aa800
Correlation ID: 2ad50471-9c2a-4c02-a4d0-189efad2f0c8
Timestamp: 2020-06-15 12:46:39Z","error_codes":[7000222],"timestamp":"2020-06-15 12:46:39Z","trace_id":"d0f42793-739e-4ce9-9118-5049086aa800","correlation_id":"2ad50471-9c2a-4c02-a4d0-189efad2f0c8","error_uri":"https://login.microsoftonline.com/error?code=7000222"}
I was able to authenticate as this Service Principal using the Azure Cloud Shell so I'm puzzled as to why I apparently can't do the same from within the release pipeline.
* I have tried with Continue on error: true and although the task completes the same error appears in the output.
To use the service principal in Azure CLI, no need to login manually.
For the correct way, please follow the steps below.
1.From the error message, the client secret(i.e. client key) of your service principal is expired. So you need to check it first, navigate to the azure portal -> Azure Active Directory -> App registrations -> All applications -> find the AD App related to your service principal -> Certificates & secrets -> check if the secret is expired. If it is expired, just click New client secret to create a new one and save the value.
2.In devops, navigate to the Project Settings -> Service connections -> New service connection -> Azure Resource Manager -> Service principal (manual).
Then fix the options with your service principal, you can get the values from your AD App in App registration page. The Service Principal Id is the Application (client) ID, the Service principal key is the client secret.
The Subscription Id and Subscription Name is which you want to use Azure CLI to access, you could find them in azure portal.
After input the values -> Verify -> Verify and save.
3.In your Azure CLI task -> Azure Resource Manager connection -> select the service connection in step 2. In my sample, I test to get a web app in my subscription with az webapp show --name xxxx --resource-group xxxx(Note: no need to use az login manually, it will login with the service principal which was configured in the service connection automatically).
Then run it and check the result, it works fine.
I think this is very similar to the problem i faced earlier, it happens when there is a special character in the client secret.
One workaround is to go to the Azure portal and keep generating new secrets until you get one that does not have special characters in it.
https://github.com/ansible/ansible/issues/54914

az devops login Failed to store PAT using keyring; falling back to file storage

I've installed AZ DEVOPS extension from az cli
az extension add --name azure-devops
az devops login --organization https://myorg.com
But when I insert my DEVOPS Personal Access Token, fail with the following error.
Failed to store PAT using keyring; falling back to file storage.
You can clear the stored credential by running az devops logout.
Refer https://aka.ms/azure-devops-cli-auth to know more on sign in with PAT.
I'm trying to access from a Linux Centos 7 machine to automate DevOps Pipeline creation.
Someone know how to fix this issue?
As a prerequisite,your Azure CLI version must be 2.0.49 at a minimum. You can use az --version to validate.
After you add az extension, you could also run az login to sign in.
If you have already signed in with az login interactively or using
user name and password, then you don't have to provide a token as az
devops commands now support sign in through az login.
When you are using az devops login command, first make sure you are using Azure DevOps Service organization URL. Example: https://dev.azure.com/MyOrganizationName. Azure DevOps Server/TFS is not support at present.
For Azure DevOps tokens, you need to make sure it's valid and with enough scopes for this token to authorize for your specific tasks.
More details please refer our official tutorial here-- Sign in with a Personal Access Token (PAT)

Resources