Django devops - Рдуард Кабринский
<h1>Django devops</h1>
<p>[youtube]</p>
Django devops <a href="http://remmont.com">Today's national news</a> Django devops
<h1>Use CI/CD to deploy a Python web app to Azure App Service on Linux</h1>
<p><strong>Azure Pipelines</strong></p>
<p>In this article, you use Azure Pipelines continuous integration and continuous delivery (CI/CD) to deploy a Python web app to Azure App Service on Linux. You begin by running app code from a GitHub repository locally. You then provision a target App Service through the Azure portal. Finally, you create an Azure Pipelines CI/CD pipeline that automatically builds the code and deploys it to the App Service whenever there's a commit to the repository.</p>
<h2>Create a repository for your app code</h2>
<p>If you already have a Python web app to use, make sure it's committed to a GitHub repository.</p>
<p>If your app uses Django and a SQLite database, it won't work for this walkthrough. For more information, see considerations for Django later in this article. If your Django app uses a separate database, you can use it with this walkthrough.</p>
<p>If you need an app to work with, you can fork and clone the repository at https://github.com/Microsoft/python-sam ... k-tutorial. The code is from the tutorial Flask in Visual Studio Code.</p>
<p>To test the example app locally, from the folder containing the code, run the following appropriate commands for your operating system:</p>
<p>Open a browser and navigate to <em>http://localhost:5000</em> to view the app. When you're finished, close the browser, and stop the Flask server with <strong>Ctrl</strong>+<strong>C</strong>.</p>
<h2>Provision the target Azure App Service</h2>
<p>The quickest way to create an App Service instance is to use the Azure command-line interface (CLI) through the interactive Azure Cloud Shell. In the following steps, you use az webapp up to both provision the App Service and perform the first deployment of your app.</p>
<p>Sign in to the Azure portal at https://portal.azure.com.
<p>Open the Azure CLI by selecting the Cloud Shell button on the portal's toolbar:</p>
<p> </p>
<p>The Cloud Shell appears along the bottom of the browser. Select <strong>Bash</strong> from the dropdown:</p>
<p style="clear: both"><img src="https://docs.microsoft.com/en-us/azure/devops/pipelines/media/python/azure-cloud-shell-interface.png" /></p>
<p>In the Cloud Shell, clone your repository using git clone . For the example app, use:</p>
<p>Replace with the name of the GitHub account you used to fork the repository.</p>
<p>To paste into the Cloud Shell, use <strong>Ctrl</strong>+<strong>Shift</strong>+<strong>V</strong>, or right-click and select <strong>Paste</strong> from the context menu.</p>
<p>The Cloud Shell is backed by an Azure Storage account in a resource group called <em>cloud-shell-storage-</em> . That storage account contains an image of the Cloud Shell's file system, which stores the cloned repository. There is a small cost for this storage. You can delete the storage account at the end of this article, along with other resources you create.</p>
<p>In the Cloud Shell, change directories into the repository folder that has your Python app, so the az webapp up command will recognize the app as Python.</p>
<p>In the Cloud Shell, use az webapp up to create an App Service and initially deploy your app.</p>
<p>Change to a name for your app service that's unique across Azure. Typically, you use a personal or company name along with an app identifier, such as -flaskpipelines . The app URL becomes <em>.azurewebsites.net</em>.</p>
<p>When the command completes, it shows JSON output in the Cloud Shell.</p>
<p>If you encounter a "Permission denied" error with a <em>.zip</em> file, you may have tried to run the command from a folder that doesn't contain a Python app. The az webapp up command then tries to create a Windows app service plan, and fails.</p>
<p>If your app uses a custom startup command, set the az webapp config property. For example, the <em>python-sample-vscode-flask-tutorial</em> app contains a file named <em>startup.txt</em> that contains its specific startup command, so you set the az webapp config property to startup.txt .</p>
<p>From the first line of output from the previous az webapp up command, copy the name of your resource group, which is similar to <strong>_rg_Linux_</strong> .</p>
<p>Enter the following command, using your resource group name, your app service name, and your startup file or command:</p>
<p>Again, when the command completes, it shows JSON output in the Cloud Shell.</p>
<p>To see the running app, open a browser and go to <em>http:// .azurewebsites.net</em>. If you see a generic page, wait a few seconds for the App Service to start, and refresh the page.</p>
<p>For a detailed description of the specific tasks performed by the az webapp up command, see Provision an App Service with single commands at the end of this article.</p>
<h2>Create an Azure DevOps project and connect to Azure</h2>
<p>To deploy to Azure App Service from Azure Pipelines, you need to establish a <em>service connection</em> between the two services.</p>
<p>In a browser, go to dev.azure.com. If you don't yet have an account on Azure DevOps, select <strong>Start free</strong> and get a free account. If you have an account already, select <strong>Sign in to Azure DevOps</strong>.</p>
<p>To simplify the service connection, use the same email address for Azure DevOps as you use for Azure.</p>
<p>Once you sign in, the browser displays your Azure DevOps dashboard, at the URL <em>https://dev.azure.com/</em> . An Azure DevOps account can belong to one or more <em>organizations</em>, which are listed on the left side of the Azure DevOps dashboard. If more than one organization is listed, select the one you want to use for this walkthrough. By default, Azure DevOps creates a new organization using the email alias you used to sign in.</p>
<p>A project is a grouping for boards, repositories, pipelines, and other aspects of Azure DevOps. If your organization doesn't have any projects, enter the project name <em>Flask Pipelines</em> under <strong>Create a project to get started</strong>, and then select <strong>Create project</strong>.</p>
<p style="clear: both"><img src="https://docs.microsoft.com/en-us/azure/devops/pipelines/media/python/azure-devops-dashboard.png" /></p>
<p>If your organization already has projects, select <strong>New project</strong> on the organization page. In the <strong>Create new project</strong> dialog box, enter the project name <em>Flask Pipelines</em>, and select <strong>Create</strong>.</p>
<p>From the new project page, select <strong>Project settings</strong> from the left navigation.</p>
<p style="clear: both"><img src="https://docs.microsoft.com/en-us/azure/devops/pipelines/media/python/project-settings.png" /></p>
<p>On the <strong>Project Settings</strong> page, select <strong>Pipelines</strong> > <strong>Service connections</strong>, then select <strong>New service connection</strong>, and then select <strong>Azure Resource Manager</strong> from the dropdown.</p>
<p style="clear: both"><img src="https://docs.microsoft.com/en-us/azure/devops/pipelines/media/python/service-connection-01.png" /></p>
<p>In the <strong>Add an Azure Resource Manager service connection</strong> dialog box:</p>
<p><ol>
<li>Give the connection a name. Make note of the name to use later in the pipeline.</li>
<li>For <strong>Scope level</strong>, select <strong>Subscription</strong>.</li>
<li>Select the subscription for your App Service from the <strong>Subscription</strong> drop-down list.</li>
<li>Under <strong>Resource Group</strong>, select your resource group from the dropdown.</li>
<li>Make sure the option <strong>Allow all pipelines to use this connection</strong> is selected, and then select <strong>OK</strong>.</li>
</ol>
</p>
<p style="clear: both"><img src="https://docs.microsoft.com/en-us/azure/devops/pipelines/media/python/service-connection-02.png" /></p>
<p>The new connection appears in the <strong>Service connections</strong> list, and is ready for Azure Pipelines to use from the project.</p>
<p>If you need to use an Azure subscription from a different email account, follow the instructions on Create an Azure Resource Manager service connection with an existing service principal.</p>
<h2>Create a Python-specific pipeline to deploy to App Service</h2>
<p>From your project page left navigation, select <strong>Pipelines</strong>.</p>
<p style="clear: both"><img src="https://docs.microsoft.com/en-us/azure/devops/pipelines/media/python/select-pipelines.png" /></p>
<p>Select <strong>New pipeline</strong>:</p>
<p style="clear: both"><img src="https://docs.microsoft.com/en-us/azure/devops/pipelines/media/python/new-pipeline.png" /></p>
<p>On the <strong>Where is your code</strong> screen, select <strong>GitHub</strong>. You may be prompted to sign into GitHub.</p>
<p style="clear: both"><img src="https://docs.microsoft.com/en-us/azure/devops/pipelines/media/python/where-is-your-code.png" /></p>
<p>On the <strong>Select a repository</strong> screen, select the repository that contains your app, such as your fork of the example app.</p>
<p style="clear: both"><img src="https://docs.microsoft.com/en-us/azure/devops/pipelines/media/python/select-repository.png" /></p>
<p>You may be prompted to enter your GitHub password again as a confirmation, and then GitHub prompts you to install the <strong>Azure Pipelines</strong> extension:</p>
<p style="clear: both"><img src="https://docs.microsoft.com/en-us/azure/devops/pipelines/media/python/github-pipelines-install-01.png" /></p>
<p>On this screen, scroll down to the <strong>Repository access</strong> section, choose whether to install the extension on all repositories or only selected ones, and then select <strong>Approve and install</strong>:</p>
<p style="clear: both"><img src="https://docs.microsoft.com/en-us/azure/devops/pipelines/media/python/github-pipelines-install-02.png" /></p>
<p>On the <strong>Configure your pipeline</strong> screen, select <strong>Python to Linux Web App on Azure</strong>.</p>
<p>Your new pipeline appears. When prompted, select the Azure subscription in which you created your Web App.</p>
<p><ul>
<li>Select the Web App</li>
<li>Select Validate and configure</li>
</ul>
</p>
<p>Azure Pipelines creates an <strong>azure-pipelines.yml</strong> file that defines your CI/CD pipeline as a series of <em>stages</em>, <em>Jobs</em>, and <em>steps</em>, where each step contains the details for different <em>tasks</em> and <em>scripts</em>. Take a look at the pipeline to see what it does. Make sure all the default inputs are appropriate for your code.</p>
<h3>YAML pipeline explained</h3>
<p>The YAML file contains the following key elements:</p>
<p>The trigger at the top indicates the commits that trigger the pipeline, such as commits to the master branch.</p>
<p>The variables that parameterize the YAML template</p>
<p>To avoid hard-coding specific variable values in your YAML file, you can define variables in the pipeline's web interface instead. For more information, see Variables - Secrets.</p>
<p><ul>
<li>Build stage , which builds your project, and a Deploy stage, which deploys it to Azure as a Linux web app.</li>
<li>Deploy stage that also creates an Environment with default name same as the Web App. You can choose to modify the environment name.</li>
</ul></p>
<p>Each stage has a pool element that specifies one or more virtual machines (VMs) in which the pipeline runs the steps . By default, the pool element contains only a single entry for an Ubuntu VM. You can use a pool to run tests in multiple environments as part of the build, such as using different Python versions for creating a package.</p>
<p>The steps element can contain children like task , which runs a specific task as defined in the Azure Pipelines task reference, and script , which runs an arbitrary set of commands.</p>
<p>The first task under Build stage is UsePythonVersion, which specifies the version of Python to use on the build agent. The @ suffix indicates the version of the task. The @0 indicates preview version. Then we have script-based task that creates a virtual environment and installs dependencies from file (requirements.txt).</p>
<p>Next step creates the <em>.zip</em> file that the steps under deploy stage of the pipeline deploys. To create the <em>.zip</em> file, add an ArchiveFiles task to the end of the YAML file:</p>
<p>You use $() in a parameter value to reference variables. The built-in Build.SourcesDirectory variable contains the location on the build agent where the pipeline cloned the app code. The archiveFile parameter indicates where to place the <em>.zip</em> file. In this case, the archiveFile parameter uses the built-in variable Build.ArtifactsStagingDirectory .</p>
<p>When deploying to Azure App Service, be sure to use includeRootFolder: false . Otherwise, the contents of the <em>.zip</em> file are put in a folder named <em>s</em>, for "sources," which is replicated on the App Service. The App Service on Linux container then can't find the app code.</p>
<p>Then we have the task to upload the artifacts.</p>
<p>In the Deploy stage, we use the deployment keyword to define a deployment job targeting an environment. By using the template, an environment with same name as the Web app is automatically created if it doesn't already exist. Alternatively you can pre-create the environment and provide the environmentName</p>
<p>Within the deployment job, first task is UsePythonVersion, which specifies the version of Python to use on the build agent.</p>
<p>We then use the AzureWebApp task to deploy the <em>.zip</em> file to the App Service you identified by the azureServiceConnectionId and webAppName variables at the beginning of the pipeline file. Paste the following code at the end of the file:</p>
<p>The StartupCommand parameter shown here is specific to the <em>python-vscode-flask-tutorial</em> example code, which defines the app in the <em>startup.py</em> file. By default, Azure App Service looks for the Flask app object in a file named <em>app.py</em> or <em>application.py</em>. If your code doesn't follow this pattern, you need to customize the startup command. Django apps may not need customization at all. For more information, see How to configure Python on Azure App Service - Customize startup command.</p>
<p>Also, because the <em>python-vscode-flask-tutorial</em> repository contains the same startup command in a file named <em>startup.txt</em>, you could specify that file in the StartupCommand parameter rather than the command, by using StartupCommand: 'startup.txt' .</p>
<h2>Run the pipeline</h2>
<p>You're now ready to try it out!</p>
<p>Select <strong>Save</strong> at upper right in the editor, and in the pop-up window, add a commit message and select <strong>Save</strong>.</p>
<p>Select <strong>Run</strong> on the pipeline editor, and select <strong>Run</strong> again in the <strong>Run pipeline</strong> dialog box. Azure Pipelines queues another pipeline run, acquires an available build agent, and has that build agent run the pipeline.</p>
<p>The pipeline takes a few minutes to complete, especially the deployment steps. You should see green checkmarks next to each of the steps.</p>
<p>If there's an error, you can quickly return to the YAML editor by selecting the vertical dots at upper right and selecting <strong>Edit pipeline</strong>:</p>
<p style="clear: both"><img src="https://docs.microsoft.com/en-us/azure/devops/pipelines/media/python/edit-pipeline-command.png" /></p>
<p>From the build page, select the <strong>Azure Web App task</strong> to display its output. To visit the deployed site, hold down the <strong>Ctrl</strong> key and select the URL after <strong>App Service Application URL</strong>.</p>
<p>If you're using the Flask example, the app should appear as follows:</p>
<p style="clear: both"><img src="https://docs.microsoft.com/en-us/azure/devops/pipelines/media/python/app-results.png" /></p>
<p>If your app fails because of a missing dependency, then your <em>requirements.txt</em> file was not processed during deployment. This behavior happens if you created the web app directly on the portal rather than using the az webapp up command as shown in this article.</p>
<p>The az webapp up command specifically sets the build action SCM_DO_BUILD_DURING_DEPLOYMENT to true . If you provisioned the app service through the portal, however, this action is not automatically set.</p>
<p>The following steps set the action:</p>
<p><ol>
<li>Open the Azure portal, select your App Service, then select <strong>Configuration</strong>.</li>
<li>Under the <strong>Application Settings</strong> tab, select <strong>New Application Setting</strong>.</li>
<li>In the popup that appears, set <strong>Name</strong> to SCM_DO_BUILD_DURING_DEPLOYMENT , set <strong>Value</strong> to true , and select <strong>OK</strong>.</li>
<li>Select <strong>Save</strong> at the top of the <strong>Configuration</strong> page.</li>
<li>Run the pipeline again. Your dependencies should be installed during deployment.</li>
</ol>
</p>
<h2>Run a post-deployment script</h2>
<p>A post-deployment script can, for example, define environment variables expected by the app code. Add the script as part of the app code and execute it using startup command.</p>
<p>To avoid hard-coding specific variable values in your YAML file, you can instead define variables in the pipeline's web interface and then refer to the variable name in the script. For more information, see Variables - Secrets.</p>
<h2>Considerations for Django</h2>
<p>As noted earlier in this article, you can use Azure Pipelines to deploy Django apps to Azure App Service on Linux, provided that you're using a separate database. You can't use a SQLite database, because App Service locks the <em>db.sqlite3</em> file, preventing both reads and writes. This behavior doesn't affect an external database.</p>
<p>As described in Configure Python app on App Service - Container startup process, App Service automatically looks for a <em>wsgi.py</em> file within your app code, which typically contains the app object. If you need to customize the startup command in any way, use the StartupCommand parameter in the AzureWebApp@1 step of your YAML pipeline file, as described in the previous section.</p>
<p>When using Django, you typically want to migrate the data models using manage.py migrate after deploying the app code. You can add startUpCommand with post-deployment script for this purpose:</p>
<h2>Run tests on the build agent</h2>
<p>As part of your build process, you may want to run tests on your app code. Tests run on the build agent, so you probably need to first install your dependencies into a virtual environment on the build agent computer. After the tests run, delete the virtual environment before you create the <em>.zip</em> file for deployment. The following script elements illustrate this process. Place them before the ArchiveFiles@2 task in the <em>azure-pipelines.yml</em> file. For more information, see Run cross-platform scripts.</p>
<p>You can also use a task like PublishTestResults@2 to make test results appear in the pipeline results screen. For more information, see Build Python apps - Run tests.</p>
<h2>Provision an App Service with single commands</h2>
<p>The az webapp up command used earlier in this article is a convenient method to provision the App Service and initially deploy your app in a single step. If you want more control over the deployment process, you can use single commands to accomplish the same tasks. For example, you might want to use a specific name for the resource group, or create an App Service within an existing App Service Plan.</p>
<p>The following steps perform the equivalent of the az webapp up command:</p>
<p>Create a resource group.</p>
<p>A resource group is a collection of related Azure resources. Creating a resource group makes it easy to delete all those resources at once when you no longer need them. In the Cloud Shell, run the following command to create a resource group in your Azure subscription. Set a location for the resource group by specifying the value of . JSON output appears in the Cloud Shell when the command completes successfully.</p>
<p>Create an App Service Plan.</p>
<p>An App Service runs inside a VM defined by an App Service Plan. Run the following command to create an App Service Plan, substituting your own values for and . The --is-linux is required for Python deployments. If you want a pricing plan other than the default F1 Free plan, use the sku argument. The --sku B1 specifies the lower-price compute tier for the VM. You can easily delete the plan later by deleting the resource group.</p>
<p>Again, you see JSON output in the Cloud Shell when the command completes successfully.</p>
<p>Create an App Service instance in the plan.</p>
<p>Run the following command to create the App Service instance in the plan, replacing with a name that's unique across Azure. Typically, you use a personal or company name along with an app identifier, such as -flaskpipelines . The command fails if the name is already in use. By assigning the App Service to the same resource group as the plan, it's easy to clean up all the resources at once.</p>
<p>If you want to deploy your code at the same time you create the app service, you can use the --deployment-source-url and --deployment-source-branch arguments with the az webapp create command. For more information, see az webapp create.</p>
<p>If you see the error message "The plan (name) doesn't exist", and you're sure that the plan name is correct, check that the resource group specified with the -g argument is also correct, and the plan you identify is part of that resource group. If you misspell the resource group name, the command doesn't find the plan in that nonexistent resource group, and gives this particular error.</p>
<p>If your app requires a custom startup command, use the az webapp config set command, as described earlier in Provision the target Azure App Service. For example, to customize the App Service with your resource group, app name, and startup command, run:</p>
<p>The App Service at this point contains only default app code. You can now use Azure Pipelines to deploy your specific app code.</p>
<h2>Clean up resources</h2>
<p>To avoid incurring ongoing charges for any Azure resources you created in this walkthrough, such as a B1 App Service Plan, delete the resource group that contains the App Service and the App Service Plan. To delete the resource group from the Azure portal, select <strong>Resource groups</strong> in the left navigation. In the resource group list, select the <strong>.</strong> to the right of the resource group you want to delete, select <strong>Delete resource group</strong>, and follow the prompts.</p>
<p>You can also use az group delete in the Cloud Shell to delete resource groups.</p>
<p>To delete the storage account that maintains the file system for Cloud Shell, which incurs a small monthly charge, delete the resource group that begins with <strong>cloud-shell-storage-</strong>.</p>
<h2>Django devops</h2>
<h3>Django devops</h3>
<p>[youtube]</p>
Django devops <a href="http://remmont.com">News headlines in english</a> Django devops
<h4>Django devops</h4>
Use CI/CD with Azure Pipelines to automatically build, test, and deploy Python web apps to Azure App Service on Linux
<h5>Django devops</h5>
Django devops <a href="http://remmont.com">Django devops</a> Django devops
SOURCE: <h6>Django devops</h6> <a href="https://dev-ops.engineer/">Django devops</a> Django devops
#tags#[replace: -,-Django devops] Django devops#tags#
https://ssylki.info/?who=unsecured-loans.remmont.com https://ssylki.info/?who=realestate.remmont.com/1681 https://ssylki.info/?who=loft-apartment.remmont.com https://ssylki.info/?who=advance-auto-c ... emmont.com https://ssylki.info/?who=car-insurance.remmont.com
