|
| 1 | +--- |
| 2 | +title: Azure Pipelines task for Azure Database for MySQL Single Server |
| 3 | +description: Enable Azure Database for MySQL Flexible Server CLI task for using with Azure Pipelines |
| 4 | +ms.topic: how-to |
| 5 | +ms.service: mysql |
| 6 | +ms.subservice: single-server |
| 7 | +ms.custom: seodec18, devops-pipelines-deploy |
| 8 | +ms.author: jukullam |
| 9 | +author: juliakm |
| 10 | +ms.date: 09/14/2022 |
| 11 | +--- |
| 12 | + |
| 13 | +# Azure Pipelines for Azure Database for MySQL Single Server |
| 14 | + |
| 15 | +Get started with Azure Database for MySQL by deploying a database update with Azure Pipelines. Azure Pipelines lets you build, test, and deploy with continuous integration (CI) and continuous delivery (CD) using [Azure DevOps](/azure/devops/). |
| 16 | + |
| 17 | +You'll use the [Azure Database for MySQL Deployment task](/azure/devops/pipelines/tasks/deploy/azure-mysql-deployment.md). The Azure Database for MySQL Deployment task only works with Azure Database for MySQL Single Server. |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +Before you begin, you need: |
| 22 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 23 | +- An active Azure DevOps organization. [Sign up for Azure Pipelines](/azure/devops/pipelines/get-started/pipelines-sign-up). |
| 24 | +- A GitHub repository that you can use for your pipeline. If you don’t have an existing repository, see [Create your first pipeline](/azure/devops/pipelines/create-first-pipeline). |
| 25 | + |
| 26 | +This quickstart uses the resources created in either of these guides as a starting point: |
| 27 | +- [Create an Azure Database for MySQL server using Azure portal](/azure/mysql/quickstart-create-mysql-server-database-using-azure-portal) |
| 28 | +- [Create an Azure Database for MySQL server using Azure CLI](/azure/mysql/quickstart-create-mysql-server-database-using-azure-cli) |
| 29 | + |
| 30 | + |
| 31 | +## Create your pipeline |
| 32 | + |
| 33 | +You'll use the basic starter pipeline as a basis for your pipeline. |
| 34 | + |
| 35 | +1. Sign in to your Azure DevOps organization and go to your project. |
| 36 | + |
| 37 | +2. In your project, navigate to the **Pipelines** page. Then choose the action to create a new pipeline. |
| 38 | + |
| 39 | +3. Walk through the steps of the wizard by first selecting GitHub as the location of your source code. |
| 40 | + |
| 41 | +4. You might be redirected to GitHub to sign in. If so, enter your GitHub credentials. |
| 42 | + |
| 43 | +5. When the list of repositories appears, select your desired repository. |
| 44 | + |
| 45 | +6. Azure Pipelines will analyze your repository and offer configuration options. Select **Starter pipeline**. |
| 46 | + |
| 47 | + :::image type="content" source="media/azure-pipelines-mysql-task/configure-pipeline-option.png" alt-text="Screenshot of Select Starter pipeline."::: |
| 48 | + |
| 49 | +## Create a secret |
| 50 | + |
| 51 | +You'll need to know your database server name, SQL username, and SQL password to use with the [Azure Database for MySQL Deployment task](/azure/devops/pipelines/tasks/deploy/azure-mysql-deployment). |
| 52 | + |
| 53 | +For security, you'll want to save your SQL password as a secret variable in the pipeline settings UI for your pipeline. |
| 54 | + |
| 55 | +1. Go to the **Pipelines** page, select the appropriate pipeline, and then select **Edit**. |
| 56 | +1. Select **Variables**. |
| 57 | +1. Add a new variable named `SQLpass` and select **Keep this value secret** to encrypt and save the variable. |
| 58 | + |
| 59 | + :::image type="content" source="media/azure-pipelines-mysql-task/save-secret-variable.png" alt-text="Screenshot of adding a secret variable."::: |
| 60 | + |
| 61 | +1. Select **Ok** and **Save** to add the variable. |
| 62 | + |
| 63 | +## Verify permissions for your database |
| 64 | + |
| 65 | +To access your MySQL database with Azure Pipelines, you need to set your database to accept connections from all Azure resources. |
| 66 | + |
| 67 | +1. In the Azure portal, open your database resource. |
| 68 | +1. Select **Connection security**. |
| 69 | +1. Toggle **Allow access to Azure services** to **Yes**. |
| 70 | + |
| 71 | + :::image type="content" source="media/azure-pipelines-mysql-task/allow-azure-access-mysql.png" alt-text="Screenshot of setting MySQL to allow Azure connections."::: |
| 72 | + |
| 73 | +## Add the Azure Database for MySQL Deployment task |
| 74 | + |
| 75 | +In this example, we'll create a new databases named `quickstartdb` and add an inventory table. The inline SQL script will: |
| 76 | + |
| 77 | +- Delete `quickstartdb` if it exists and create a new `quickstartdb` database. |
| 78 | +- Delete the table `inventory` if it exists and creates a new `inventory` table. |
| 79 | +- Insert three rows into `inventory`. |
| 80 | +- Show all the rows. |
| 81 | +- Update the value of the first row in `inventory`. |
| 82 | +- Delete the second row in `inventory`. |
| 83 | + |
| 84 | +You'll need to replace the following values in your deployment task. |
| 85 | + |
| 86 | +|Input |Description |Example | |
| 87 | +|---------|---------|---------| |
| 88 | +|`azureSubscription` | Authenticate with your Azure Subscription with a [service connection](/azure/devops/pipelines/library/connect-to-azure). | `My Subscription` | |
| 89 | +|`ServerName` | The name of your Azure Database for MySQL server. | `fabrikam.mysql.database.azure.com` | |
| 90 | +|`SqlUsername` | The user name of your Azure Database for MySQL. | `mysqladmin@fabrikam` | |
| 91 | +|`SqlPassword` | The password for the username. This should be defined as a secret variable. | `$(SQLpass)` | |
| 92 | + |
| 93 | +```yaml |
| 94 | + |
| 95 | +trigger: |
| 96 | +- main |
| 97 | + |
| 98 | +pool: |
| 99 | + vmImage: ubuntu-latest |
| 100 | + |
| 101 | +steps: |
| 102 | +- task: AzureMysqlDeployment@1 |
| 103 | + inputs: |
| 104 | + azureSubscription: '<your-subscription> |
| 105 | + ServerName: '<db>.mysql.database.azure.com' |
| 106 | + SqlUsername: '<username>@<db>' |
| 107 | + SqlPassword: '$(SQLpass)' |
| 108 | + TaskNameSelector: 'InlineSqlTask' |
| 109 | + SqlInline: | |
| 110 | + DROP DATABASE IF EXISTS quickstartdb; |
| 111 | + CREATE DATABASE quickstartdb; |
| 112 | + USE quickstartdb; |
| 113 | + |
| 114 | + -- Create a table and insert rows |
| 115 | + DROP TABLE IF EXISTS inventory; |
| 116 | + CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER); |
| 117 | + INSERT INTO inventory (name, quantity) VALUES ('banana', 150); |
| 118 | + INSERT INTO inventory (name, quantity) VALUES ('orange', 154); |
| 119 | + INSERT INTO inventory (name, quantity) VALUES ('apple', 100); |
| 120 | + |
| 121 | + -- Read |
| 122 | + SELECT * FROM inventory; |
| 123 | + |
| 124 | + -- Update |
| 125 | + UPDATE inventory SET quantity = 200 WHERE id = 1; |
| 126 | + SELECT * FROM inventory; |
| 127 | + |
| 128 | + -- Delete |
| 129 | + DELETE FROM inventory WHERE id = 2; |
| 130 | + SELECT * FROM inventory; |
| 131 | + IpDetectionMethod: 'AutoDetect' |
| 132 | +``` |
| 133 | +
|
| 134 | +## Deploy and verify resources |
| 135 | +
|
| 136 | +Select **Save and run** to deploy your pipeline. The pipeline job will be launched and after few minutes, the job status should indicate `Success`. |
| 137 | + |
| 138 | +You can verify that your pipeline ran successfully within the `AzureMysqlDeployment` task in the pipeline run. |
| 139 | + |
| 140 | +Open the task and verify that the last two entries show two rows in `inventory`. There are two rows because the second row has been deleted. |
| 141 | + |
| 142 | +:::image type="content" source="media/azure-pipelines-mysql-task/database-update-results.png" alt-text="Screenshot to show reviewing final table results."::: |
| 143 | + |
| 144 | + |
| 145 | +## Clean up resources |
| 146 | + |
| 147 | +When you’re done working with your pipeline, delete `quickstartdb` in your Azure Database for MySQL. You can also delete the deployment pipeline you created. |
| 148 | + |
| 149 | +## Next steps |
| 150 | + |
| 151 | +> [!div class="nextstepaction"] |
| 152 | +> [Tutorial: Build an ASP.NET Core and Azure SQL Database app in Azure App Service](/azure/app-service/tutorial-dotnetcore-sqldb-app) |
0 commit comments