Reputation: 1633
pipeline{
agent any
stages{
stage('Checkout') {
steps {
git branch: 'master',
credentialsId: 'xxx-test-credentials',
url: 'https://gitlab.com/xxxx-xxxx/xxxxx/terraform.git'
sh "pwd"
sh "ls -lat"
}
}
stage('Set Terraform path') {
steps {
script {
def tfHome = tool name: 'Terraform'
env.PATH = "${tfHome}:${env.PATH}"
}
sh 'terraform version'
}
}
stage('Provision infrastructure') {
steps {
dir('environments/dev')
{
withCredentials([azureServicePrincipal('xxx-test-service-principal')]) {
script{
sh 'terraform init'
sh 'terraform plan'
sh 'az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET -t $AZURE_TENANT_ID'
}
}
// sh ‘terraform destroy -auto-approve’
}
}
}
}
}
I ran the above basic pipeline which runs Terraform init and plan. I have used the service principal credentials created to authenticate with the subscription where I wanted to create the resources but I get the below error even though Azure CLI plugin has been installed on the Jenkins from where I'm trying to run this pipeline.
[1m[31mError: [0m[0m[1mError refreshing state: 1 error occurred:
* provider.azurerm: Error building AzureRM Client: Azure CLI Authorization Profile was not found. Please ensure the Azure CLI is installed and then log-in with az login
.
[0m
[0m[0m[0m
Upvotes: 0
Views: 1374
Reputation: 72151
I'm fairly certain you need to auth first and do plan after, try this:
sh 'az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET -t $AZURE_TENANT_ID'
sh 'terraform init'
sh 'terraform plan'
Upvotes: 1