|
| 1 | +--- |
| 2 | +title: 'Manually configure CI/CD for load tests' |
| 3 | +titleSuffix: Azure Load Testing |
| 4 | +description: 'This article shows how to run your load tests with Azure Load Testing in CI/CD. Learn how to add a load test to GitHub Actions or Azure Pipelines.' |
| 5 | +author: ntrogh |
| 6 | +ms.author: nicktrog |
| 7 | +ms.service: load-testing |
| 8 | +ms.topic: how-to |
| 9 | +ms.date: 06/05/2023 |
| 10 | +--- |
| 11 | + |
| 12 | +# Manually configure CI/CD for load tests in GitHub Actions or Azure Pipelines |
| 13 | + |
| 14 | +Get started with automating load tests in Azure Load Testing by adding it to a CI/CD pipeline. After running a load test in the Azure portal, you export the configuration files, and configure a CI/CD pipeline in GitHub Actions or Azure Pipelines. |
| 15 | + |
| 16 | +After you complete this quickstart, you have a CI/CD workflow that is configured to run a load test with Azure Load Testing. |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 21 | +- An Azure Load Testing test. Create a [URL-based load test](./quickstart-create-and-run-load-test.md) or [use an existing JMeter script](./how-to-create-and-run-load-test-with-jmeter-script.md) to create a load test. |
| 22 | + |
| 23 | +# [Azure Pipelines](#tab/pipelines) |
| 24 | +- An Azure DevOps organization and project. If you don't have an Azure DevOps organization, you can [create one for free](/azure/devops/pipelines/get-started/pipelines-sign-up?view=azure-devops&preserve-view=true). If you need help with getting started with Azure Pipelines, see [Create your first pipeline](/azure/devops/pipelines/create-first-pipeline?preserve-view=true&view=azure-devops&tabs=java%2Ctfs-2018-2%2Cbrowser). |
| 25 | + |
| 26 | +# [GitHub Actions](#tab/github) |
| 27 | +- A GitHub account. If you don't have a GitHub account, you can [create one for free](https://github.com/). |
| 28 | +- A GitHub repository to store the load test input files and create a GitHub Actions workflow. To create one, see [Creating a new repository](https://docs.github.com/github/creating-cloning-and-archiving-repositories/creating-a-new-repository). |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +## Configure service authentication |
| 33 | + |
| 34 | +To run a load test in your CI/CD workflow, you need to grant permission to the CI/CD workflow to access your load testing resource. Create a service principal for the CI/CD workflow and assign the Load Test Contributor Azure RBAC role. |
| 35 | + |
| 36 | +# [Azure Pipelines](#tab/pipelines) |
| 37 | + |
| 38 | +### Create a service connection in Azure Pipelines |
| 39 | + |
| 40 | +In Azure Pipelines, you create a *service connection* in your Azure DevOps project to access resources in your Azure subscription. When you create the service connection, Azure DevOps creates an Azure Active Directory service principal object. |
| 41 | + |
| 42 | +1. Sign in to your Azure DevOps organization (`https://dev.azure.com/<your-organization>`), and select your project. |
| 43 | + |
| 44 | + Replace the `<your-organization>` text placeholder with your project identifier. |
| 45 | + |
| 46 | +1. Select **Project settings** > **Service connections** > **+ New service connection**. |
| 47 | + |
| 48 | +1. In the **New service connection** pane, select the **Azure Resource Manager**, and then select **Next**. |
| 49 | + |
| 50 | +1. Select the **Service Principal (automatic)** authentication method, and then select **Next**. |
| 51 | + |
| 52 | +1. Enter the service connection details, and then select **Save** to create the service connection. |
| 53 | + |
| 54 | + | Field | Value | |
| 55 | + | ----- | ----- | |
| 56 | + | **Scope level** | *Subscription*. | |
| 57 | + | **Subscription** | Select the Azure subscription that hosts your load testing resource. | |
| 58 | + | **Resource group** | Select the resource group that contains your load testing resource. | |
| 59 | + | **Service connection name** | Enter a unique name for the service connection. | |
| 60 | + | **Grant access permission to all pipelines** | Checked. | |
| 61 | + |
| 62 | +1. From the list of service connections, select the one you created earlier, and then select **Manage Service Principal**. |
| 63 | + |
| 64 | + :::image type="content" source="./media/quickstart-add-load-test-cicd/service-connection-manage-service-principal.png" alt-text="Screenshot that shows selections for managing a service principal." lightbox="./media/quickstart-add-load-test-cicd/service-connection-manage-service-principal.png"::: |
| 65 | + |
| 66 | + The Azure portal opens in a separate browser tab and shows the service principal details. |
| 67 | + |
| 68 | +1. In the Azure portal, copy the **Display name** value. |
| 69 | + |
| 70 | + You use this value in the next step to grant permissions for running load tests to the service principal. |
| 71 | + |
| 72 | +### Grant access to Azure Load Testing |
| 73 | + |
| 74 | +Azure Load Testing uses Azure RBAC to grant permissions for performing specific activities on your load testing resource. To run a load test from your CI/CD pipeline, you grant the Load Test Contributor role to the service principal. |
| 75 | + |
| 76 | +1. In the [Azure portal](https://portal.azure.com/), go to your Azure Load Testing resource. |
| 77 | + |
| 78 | +1. Select **Access control (IAM)** > **Add** > **Add role assignment**. |
| 79 | + |
| 80 | +1. In the **Role** tab, select **Load Test Contributor** in the list of job function roles. |
| 81 | + |
| 82 | + :::image type="content" source="media/quickstart-add-load-test-cicd/load-test-contributor-role-assignment.png" alt-text="Screenshot that shows the list of roles in the Add role assignment page in the Azure portal, highlighting the Load Test Contributor role." lightbox="media/quickstart-add-load-test-cicd/load-test-contributor-role-assignment.png"::: |
| 83 | + |
| 84 | +1. In the **Members** tab, select **Select members**, and then use the display name you copied previously to search the service principal. |
| 85 | + |
| 86 | +1. Select the service principal, and then select **Select**. |
| 87 | + |
| 88 | +1. In the **Review + assign tab**, select **Review + assign** to add the role assignment. |
| 89 | + |
| 90 | +You can now use the service connection in your Azure Pipelines workflow definition to access your Azure load testing resource. |
| 91 | + |
| 92 | +# [GitHub Actions](#tab/github) |
| 93 | + |
| 94 | +To access your Azure Load Testing resource from the GitHub Actions workflow, you first create an Azure Active Directory [service principal](/azure/active-directory/develop/app-objects-and-service-principals#service-principal-object). This service principal represents your GitHub Actions workflow in Azure Active Directory. |
| 95 | + |
| 96 | +Next, you grant permissions to the service principal to create and run a load test with your Azure Load Testing resource. |
| 97 | + |
| 98 | +### Create a service principal |
| 99 | + |
| 100 | +Create a service principal in the Azure subscription and assign the Load Test Contributor role so that your GitHub Actions workflow has access to your Azure load testing resource to run load tests. |
| 101 | + |
| 102 | +1. Create a service principal and assign the `Load Test Contributor` role: |
| 103 | + |
| 104 | + ```azurecli-interactive |
| 105 | + loadtest=$(az resource show -g <resource-group-name> -n <load-testing-resource-name> --resource-type "Microsoft.LoadTestService/loadtests" --query "id" -o tsv) |
| 106 | + echo $loadtest |
| 107 | +
|
| 108 | + az ad sp create-for-rbac --name "my-load-test-cicd" --role "Load Test Contributor" \ |
| 109 | + --scopes $loadtest \ |
| 110 | + --sdk-auth |
| 111 | + ``` |
| 112 | +
|
| 113 | + The output is a JSON object that represents the service principal. You use this information to authenticate with Azure in the GitHub Actions workflow. |
| 114 | +
|
| 115 | + ```output |
| 116 | + Creating 'Load Test Contributor' role assignment under scope |
| 117 | + { |
| 118 | + "clientId": "00000000-0000-0000-0000-000000000000", |
| 119 | + "clientSecret": "00000000-0000-0000-0000-000000000000", |
| 120 | + "subscriptionId": "00000000-0000-0000-0000-000000000000", |
| 121 | + "tenantId": "00000000-0000-0000-0000-000000000000", |
| 122 | + "activeDirectoryEndpointUrl": "https://login.microsoftonline.com", |
| 123 | + "resourceManagerEndpointUrl": "https://management.azure.com/", |
| 124 | + "activeDirectoryGraphResourceId": "https://graph.windows.net/", |
| 125 | + "sqlManagementEndpointUrl": "https://management.core.windows.net:8443/", |
| 126 | + "galleryEndpointUrl": "https://gallery.azure.com/", |
| 127 | + "managementEndpointUrl": "https://management.core.windows.net/" |
| 128 | + } |
| 129 | + ``` |
| 130 | +
|
| 131 | + > [!NOTE] |
| 132 | + > You might get a `--sdk-auth` deprecation warning when you run this command. Alternatively, you can use OpenID Connect (OIDC) based authentication for authenticating GitHub with Azure. Learn how to [use the Azure login action with OpenID Connect](/azure/developer/github/connect-from-azure#use-the-azure-login-action-with-openid-connect). |
| 133 | +
|
| 134 | +1. Copy the output JSON object to the clipboard. |
| 135 | +
|
| 136 | + In the next step, you store the service principal information as a GitHub Actions secret. |
| 137 | +
|
| 138 | +### Store Azure credentials in GitHub Actions secret |
| 139 | +
|
| 140 | +Create a GitHub Actions secret to securely store the service principal information. You use this secret in your workflow definition to connect to authenticate with Azure and access your Azure load testing resource. |
| 141 | +
|
| 142 | +> [!NOTE] |
| 143 | +> If you're using OpenID Connect to authenticate with Azure, you don't have to pass the service principal object in the Azure login action. Learn how to [use the Azure login action with OpenID Connect](/azure/developer/github/connect-from-azure#use-the-azure-login-action-with-openid-connect). |
| 144 | +
|
| 145 | +To create a GitHub Actions secret: |
| 146 | +
|
| 147 | +1. In [GitHub](https://github.com), browse to your repository. |
| 148 | +
|
| 149 | +1. Select **Settings** > **Secrets & variables** > **Actions**. |
| 150 | +
|
| 151 | +1. Select **New repository secret**, enter the secret information, and then select **Add secret** to create a new secret. |
| 152 | +
|
| 153 | + | Field | Value | |
| 154 | + | ----- | ----- | |
| 155 | + | **Name** | *AZURE_CREDENTIALS* | |
| 156 | + | **Secret** | Paste the JSON output from the service principal creation command you copied earlier. | |
| 157 | +
|
| 158 | +You can now access your Azure subscription and load testing resource from your GitHub Actions workflow by using the stored credentials. |
| 159 | +
|
| 160 | +--- |
| 161 | +
|
| 162 | +## Export load test input files |
| 163 | +
|
| 164 | +To run a load test with Azure Load Testing in a CI/CD workflow, you need to add the load test configuration settings and any input files in your source control repository. If you have an existing load test, you can download the configuration settings and all input files from the Azure portal. |
| 165 | +
|
| 166 | +Perform the following steps to download the input files for an existing load testing in the Azure portal: |
| 167 | +
|
| 168 | +1. In the [Azure portal](https://portal.azure.com/), go to your Azure Load Testing resource. |
| 169 | +
|
| 170 | +1. On the left pane, select **Tests** to view the list of load tests, and then select your test. |
| 171 | +
|
| 172 | + :::image type="content" source="media/quickstart-add-load-test-cicd/view-all-tests.png" alt-text="Screenshot that shows the list of tests for an Azure Load Testing resource."::: |
| 173 | +
|
| 174 | +1. Selecting the ellipsis (**...**) next to the test run you're working with, and then select **Download input file**. |
| 175 | +
|
| 176 | + The browser downloads a zipped folder that contains the load test input files. |
| 177 | +
|
| 178 | + :::image type="content" source="media/quickstart-add-load-test-cicd/download-load-test-input-files.png" alt-text="Screenshot that shows how to download the results file for a load test run."::: |
| 179 | +
|
| 180 | +1. Use any zip tool to extract the input files. |
| 181 | +
|
| 182 | + The folder contains the following files: |
| 183 | +
|
| 184 | + - `config.yaml`: the load test YAML configuration file. You reference this file in the CI/CD workflow definition. |
| 185 | + - `.jmx`: the JMeter test script |
| 186 | + - Any additional input files, such as CSV files or user properties files that are needed to run the load test. |
| 187 | +
|
| 188 | +1. Commit all extracted input files to your source control repository. |
| 189 | +
|
| 190 | + Use the source code repository in which you configure the CI/CD pipeline. |
| 191 | +
|
| 192 | +## Update the CI/CD workflow definition |
| 193 | +
|
| 194 | +Azure Load Testing supports both GitHub Actions and Azure Pipelines for running load tests. |
| 195 | +
|
| 196 | +# [Azure Pipelines](#tab/pipelines) |
| 197 | +
|
| 198 | +### Install the Azure Load Testing extension for Azure DevOps |
| 199 | +
|
| 200 | +To create and run a load test, the Azure Pipelines workflow definition uses the [Azure Load Testing task](/azure/devops/pipelines/tasks/test/azure-load-testing) extension from the Azure DevOps Marketplace. |
| 201 | +
|
| 202 | +1. Open the [Azure Load Testing task extension](https://marketplace.visualstudio.com/items?itemName=AzloadTest.AzloadTesting) in the Azure DevOps Marketplace, and select **Get it free**. |
| 203 | +
|
| 204 | +1. Select your Azure DevOps organization, and then select **Install** to install the extension. |
| 205 | +
|
| 206 | + If you don't have administrator privileges for the selected Azure DevOps organization, select **Request** to request an administrator to install the extension. |
| 207 | +
|
| 208 | +### Update the Azure Pipelines workflow |
| 209 | +
|
| 210 | +Update your Azure Pipelines workflow to run a load test for your Azure load testing resource. |
| 211 | +
|
| 212 | +1. Sign in to your Azure DevOps organization (`https://dev.azure.com/<your-organization>`), and select your project. |
| 213 | +
|
| 214 | +1. Select **Pipelines** in the left navigation, select your pipeline, and then select **Edit** to edit your workflow definition. |
| 215 | +
|
| 216 | + Alternately, select **Create Pipeline** to create a new pipeline in Azure Pipelines. |
| 217 | +
|
| 218 | +1. Use the `AzureLoadTest` task to run the load test. |
| 219 | +
|
| 220 | + Specify the load test configuration file you exported earlier in the `loadTestConfigFile` property. |
| 221 | + |
| 222 | + Replace the *`<load-testing-resource>`* and *`<load-testing-resource-group>`* text placeholders with the name of your Azure load testing resource and the resource group. |
| 223 | +
|
| 224 | + ```yml |
| 225 | + - task: AzureLoadTest@1 |
| 226 | + inputs: |
| 227 | + azureSubscription: $(serviceConnection) |
| 228 | + loadTestConfigFile: 'config.yaml' |
| 229 | + loadTestResource: <load-testing-resource> |
| 230 | + resourceGroup: <load-testing-resource-group> |
| 231 | + ``` |
| 232 | +
|
| 233 | + Optionally, you can pass parameters or secrets to the load test by using the `env` or `secrets` property. |
| 234 | +
|
| 235 | +1. Use the `publish` task to publish the test results as artifacts in your Azure Pipelines workflow run. |
| 236 | +
|
| 237 | + ```yml |
| 238 | + - publish: $(System.DefaultWorkingDirectory)/loadTest |
| 239 | + artifact: loadTestResults |
| 240 | + ``` |
| 241 | +
|
| 242 | +# [GitHub Actions](#tab/github) |
| 243 | +
|
| 244 | +### Update the GitHub Actions workflow |
| 245 | +
|
| 246 | +Update your GitHub Actions workflow to run a load test for your Azure load testing resource. |
| 247 | +
|
| 248 | +1. In [GitHub](https://github.com), browse to your repository. |
| 249 | +
|
| 250 | +1. Edit your GitHub Actions workflow or [create a new workflow](https://docs.github.com/actions/quickstart) in your GitHub repository. |
| 251 | +
|
| 252 | +1. Use the `actions/checkout` action to check out the repository with the load test input files. |
| 253 | +
|
| 254 | + ```yml |
| 255 | + - name: Checkout |
| 256 | + uses: actions/checkout@v3 |
| 257 | + ``` |
| 258 | + |
| 259 | +1. Use the `azure/login` action to authenticate with Azure by using the stored credentials. |
| 260 | +
|
| 261 | + Paste the following YAML contents in your workflow definition: |
| 262 | +
|
| 263 | + ```yml |
| 264 | + - name: Login to Azure |
| 265 | + uses: azure/login@v1 |
| 266 | + continue-on-error: false |
| 267 | + with: |
| 268 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 269 | + ``` |
| 270 | +
|
| 271 | +1. Use the `azure/load-testing` action to run the load test. |
| 272 | +
|
| 273 | + Specify the load test configuration file you exported earlier in the `loadTestConfigFile` property. |
| 274 | + |
| 275 | + Replace the *`<load-testing-resource>`* and *`<load-testing-resource-group>`* text placeholders with the name of your Azure load testing resource and the resource group. |
| 276 | +
|
| 277 | + ```yml |
| 278 | + - name: 'Azure Load Testing' |
| 279 | + uses: azure/load-testing@v1 |
| 280 | + with: |
| 281 | + loadTestConfigFile: 'config.yaml' |
| 282 | + loadTestResource: <load-testing-resource> |
| 283 | + resourceGroup: <load-testing-resource-group> |
| 284 | + ``` |
| 285 | +
|
| 286 | + Optionally, you can pass parameters or secrets to the load test by using the `env` or `secrets` property. |
| 287 | +
|
| 288 | +1. Use the `actions/upload-artifact` action to publish the test results as artifacts in your GitHub Actions workflow run. |
| 289 | +
|
| 290 | + ```yml |
| 291 | + - uses: actions/upload-artifact@v2 |
| 292 | + with: |
| 293 | + name: loadTestResults |
| 294 | + path: ${{ github.workspace }}/loadTest |
| 295 | + ``` |
| 296 | +
|
| 297 | +--- |
| 298 | +
|
| 299 | +## View load test results |
| 300 | +
|
| 301 | +When you run a load test from your CI/CD pipeline, you can view the summary results directly in the CI/CD output log. If you published the test results as a pipeline artifact, you can also download a CSV file for further reporting. |
| 302 | +
|
| 303 | +:::image type="content" source="./media/quickstart-add-load-test-cicd/github-actions-workflow-completed.png" alt-text="Screenshot that shows the workflow logging information."::: |
| 304 | +
|
| 305 | +## Clean up resources |
| 306 | +
|
| 307 | +If you don't plan to use any of the resources that you created, delete them so you don't incur any further charges. |
| 308 | +
|
| 309 | +# [Azure Pipelines](#tab/pipelines) |
| 310 | +
|
| 311 | +1. Remove Azure Pipelines changes: |
| 312 | + 1. Sign in to your Azure DevOps organization (`https://dev.azure.com/<your-organization>`), and select your project. |
| 313 | + |
| 314 | + Replace the `<your-organization>` text placeholder with your project identifier. |
| 315 | +
|
| 316 | + 1. If you created a new pipeline": |
| 317 | + 1. Select **Pipelines**, and then select your pipeline. |
| 318 | + 1. Select the ellipsis, and then select **Delete**. |
| 319 | +
|
| 320 | + :::image type="content" source="./media/quickstart-add-load-test-cicd/azure-pipelines-delete.png" alt-text="Screenshot that shows how to delete an Azure Pipelines definition." lightbox="./media/quickstart-add-load-test-cicd/azure-pipelines-delete.png"::: |
| 321 | +
|
| 322 | + 1. Enter the pipeline name, and then select **Delete** to delete the pipeline. |
| 323 | +
|
| 324 | + 1. If you modified an existing workflow definition, undo the modifications for running the load test, and save the workflow. |
| 325 | +
|
| 326 | +1. Remove the service connection: |
| 327 | + 1. Select **Project settings** > **Service connections**, and then select your service connection. |
| 328 | + 1. Select **Edit** > **Delete** to remove the service connection. |
| 329 | +
|
| 330 | +# [GitHub Actions](#tab/github) |
| 331 | +
|
| 332 | +1. Remove GitHub Actions changes: |
| 333 | + 1. In [GitHub](https://github.com), browse to your repository. |
| 334 | + 1. If you created a new workflow definition, delete the workflow YAML file in the `.github/workflows` folder. |
| 335 | + 1. If you modified an existing workflow definition, undo the modifications for running the load test, and save the workflow. |
| 336 | +
|
| 337 | +1. Remove the service principal: |
| 338 | +
|
| 339 | + ```azurecli-interactive |
| 340 | + az ad sp delete --id $(az ad sp show --display-name "my-load-test-cicd" -o tsv) |
| 341 | + ``` |
| 342 | +
|
| 343 | +--- |
| 344 | +
|
| 345 | +## Next steps |
| 346 | +
|
| 347 | +Advance to the next article to learn how to identify performance regressions by defining test fail criteria and comparing test runs. |
| 348 | +
|
| 349 | +> [!div class="nextstepaction"] |
| 350 | +> [Tutorial: automate regression tests](./tutorial-identify-performance-regression-with-cicd.md) |
| 351 | +> [Define test fail criteria](./how-to-define-test-criteria.md) |
0 commit comments