Setup Azure Cognitive Services
- Go to the Azure Portal and sign in with your Azure account.
- Navigate to 'Create a resource'. In the search box, type 'Cognitive Services' and select it from the dropdown.
- Click on 'Create'. Fill in the required information such as Subscription, Resource Group, Region, etc.
- Under 'Pricing tier', select the appropriate tier for your requirements.
- Review and then click 'Create'. It will take a few moments to create the resource.
- Once created, go to the resource, and in the 'Keys and Endpoint' section, you will find your API endpoint and keys which you'll need later.
Install Jenkins and Required Plugins
- Install Jenkins on your server. You can download it from the official Jenkins website.
- Configure Jenkins by unlocking it using the password provided during the installation.
- Install recommended plugins or manually select plugins based on your needs.
- Ensure that Jenkins has access to the required Azure plugin. Go to 'Manage Jenkins' > 'Manage Plugins'. Search for 'Azure Credentials' and 'Azure SDK Libraries' plugins, and install them.
Configure Azure Credentials in Jenkins
- Go to 'Manage Jenkins' > 'Manage Credentials'.
- Select your preferred domain or 'Global' if you want these credentials to be available everywhere.
- Click on 'Add Credentials'. Choose 'Microsoft Azure Service Principal' as the kind.
- Fill in the Service Principal's credentials: Client ID, Client Secret, and Tenant ID. These are typically created in Azure Active Directory.
- Test the connection to ensure they are correctly set up.
Integrate Azure Cognitive Services API in Jenkins Pipeline
- Create a new pipeline job in Jenkins by selecting 'New Item' and then 'Pipeline'.
- In the pipeline script, you can use the following simple script to access Azure Cognitive Services:
pipeline {
agent any
stages {
stage('Azure Cognitive Services') {
steps {
script {
def azureServiceKey = "${YOUR_AZURE_COGNITIVE_KEY}"
sh 'curl -X POST \
-H "Content-Type: application/json" \
-H "Ocp-Apim-Subscription-Key: ${azureServiceKey}" \
--data "{\'documents\':[{\'id\':\'1\', \'language\':\'en\', \'text\':\'Hello World\'}]}" \
https://<your-cognitive-service-endpoint>/text/analytics/v3.0/sentiment'
}
}
}
}
}
This script uses a simple curl command to send a POST request to the Azure Cognitive Service's text analytics API. Adjust the API endpoint and payload as per your specific needs.
Testing and Verification
- Run the Jenkins pipeline to ensure everything is set up correctly.
- Check the Jenkins build console output for any errors or confirmation that Azure Cognitive Services' API call was successful.
- If the integration is successful, you should see a valid response from Azure Cognitive Services in the output.