Azure Web Apps - how to run script before deployment - azure

I'm trying to use Azure Web Apps (Linux) to host a basic static site. I configured everything so a new deployment happens with every Git push. I put my pre-built pages in my repo to confirm everything works fine with this setup.
Now I've removed the pre-built pages and kept only the templates and the build script (which is basically just an npm install and a mustatic 'compile') and I'd like to run this build script in my web app. I've scoured the internet but can't find anything.
How can I run a script upon first deployment and after each Git-push-triggered deployment?

How can I run a script upon first deployment and after each Git-push-triggered deployment?
First, you need to generate custom deployment script by using azure-cli tool.
1) Set the cli working mode to asm.
azure config mode asm
2) Run the custom deployment script generator command.
azure site deploymentscript --node -t bash
This will generate the files required to deploy your site.
.deployment - Contains the command to run for deploying your site.
deploy.sh - Contains the deployment script.
Now you can edit the deploy.cmd file and add your custom steps.
After that done, add the generated files to your repository (.deployment and deploy.sh) and push your repository to your Azure Web App and see your custom deployment running.
For more details, please refer to this blog post.

Related

How can I deploy arbitrary files from an Azure git repo to a Databricks workspace?

Databricks recently added support for "files in repos" which is a neat feature. It gives a lot more flexibility to the projects, since we can now add .json config files and even write custom python modules that exists solely in our closed environment.
However, I just noticed that the standard way of deploying from an Azure git repo to a workspace does not support arbitrary files. First off, all .py files are converted to notebooks, breaking the custom modules that we wrote for our project. Secondly, it intentionally skips files ending in one of the following: .scala, .py, .sql, .SQL, .r, .R, .ipynb, .html, .dbc, which means our .json config files are missing when the deployment is finished.
Is there any way to get around these issues or will we have to revert everything to use notebooks like we used to?
You need to stop doing deployment the old way as it depends on the Workspace REST API that doesn't support arbitrary files. Instead you need to have a Git checkout in your destination workspace, and update that checkout to a given branch/tag when doing release. This is could be done via Repos API, or databricks cli. Here is an example of how to do that with cli from DevOps pipeline.
- script: |
echo "Checking out the releases branch"
databricks repos update --path $(STAGING_DIRECTORY) --branch "$(Build.SourceBranchName)"
env:
DATABRICKS_HOST: $(DATABRICKS_HOST)
DATABRICKS_TOKEN: $(DATABRICKS_TOKEN)
displayName: 'Update Staging repository'

Proper way to set up a release pipeline in Azure Devops for Python based Azure Function

I've a working build pipeline in Azure Devops that essentially installs Python3.6, sets up a virtual environment (.env) and then executes all unit tests. It then uses as its final step, a copy operation to move all files, including the virtual environment to a drop folder.
My problem arises from creating a release pipe. I am running a bash script for the release pipeline that essentially installs the azure functions command tools, and then I activate the python virtual environment before I call the func azure publish instruction.
The error I get states that settings are encrypted and that I need to call func setting add to add settings, however, when run locally, the script executes without any error whatsoever.
Does anyone have a working release pipeline in Azure Devops for a python-based Azure Function that they'd be able to share with me, so I can perhaps see what I am doing wrong?
Here is the relevant bit of script that executes:
#!/usr/bin/env bash
FUNCTION_APP_NAME="secret"
FUNCTION_APP_FOLDER="evenMoreSecret"
# Install Azure Functions Core Tools
echo "--> Install Azure Functions Core Tools"
wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo apt-get update
sudo apt-get install azure-functions-core-tools -y
echo ">>>>>>>> Initialize Python Virtual Environment"
source .env/bin/activate
echo "--> Publish the function app to Azure Functions"
cd $FUNCTION_APP_FOLDER
func azure functionapp publish $FUNCTION_APP_NAME --build-native-deps
The script is executed using an Azure CLI, using a security principal which is tied to the azure account that it is targeting.
Usually with Azure DevOps you create several build steps that result in some build artifacts - these are defined in the azure-pipelines.yml file. You then do a release step to release the artifacts that you have created - this is created within the UI. This can involve deploying to a test server and then to production or however you want to configure it. What you are describing is doing the build and release step all in the one yaml file as the func publish is essentially doing a release and it seems to all be in the one script.
In the next release of the az cli there is a new command called az functionapp devops-build that will set up the DevOps pipeline with the seperate build and release steps. However, in the mean time, we have created a series of beta yaml files that we hope you can just drag and drop to do the build and release steps just within the build part (as you are doing).
The beta yaml files are here:
https://github.com/Azure/azure-functions-devops-build/wiki/Yaml-Samples
I must disclaim that they are not fully tested, nor are they supported yet.
I will answer myself as I've solved the problem.
To #Oliver Dolk: We do NOT want to publish as part of a build pipeline. The only thing I'm interested in is to set up a virtual environment and then run the unit tests.
The RELEASE stage is where we want to deploy the scripts copied over from the build step. These artifacts are then the basis for releasing into dev, test and prodution environment.
I was missing a very important step in my script; To create a local.settings.json file which contains encrypted settings for the functionapp.
In order to solve the problem, I only had to call the following:
func azure functionapp fetch-app-settings $FUNCTION_APP_NAME
This calls the azure functionApp, and retrieves it's settings into an encrypted local.settings.json which is then used during publishing.
For a complete script reference of both the build YAML script and the bash script that does the deployment, I've put both in an anonimized github repo:
https://github.com/digitaldias/Python-Examples

How to auto-generate deploy.cmd in new Azure CLI?

I'm following this guide to create a web app with a custom deploy.cmd file. The article suggests that I can get a copy of the current deploy.cmd file (which I'll then modify) using the following command:
azure site deploymentscript --python
Unfortunately, when I install the Azure CLI using the MSI linked in the article, there is no azure binary on my path. I do have az -- is this a newer version of the same CLI? -- but I can't find an equivalent deployment script generation command for that executable.
I found a deploy.cmd file using Kudu (under D:\home\site\deployments\tools) but am not sure if that's the appropriate file to use. Can anyone suggest the right Azure CLI command for deployment script generation, or confirm that the deploy.cmd file I found is the right one to modify? Thanks in advance!
Based on my knowledge, there is not an equivalent to azure site deploymentscript in azure cli(2.0). So, you could not do deploy custom script with Azure CLI 2.0.
You had better know the difference between Azure cli 2.0(az) with Azure cli 1.0(azure).
Azure CLI 2.0: Our next-generation CLI written in Python, for use with
the Resource Manager deployment model.
Azure CLI 1.0: Our CLI written in Node.js, for use with both the
classic and Resource Managerdeployment models.
For your scenario, if you could install Azure ClI 1.0, you could refer to this link to install Azure CLI 1.0.
Instead of using the command line to generate a starter deployment script, there is an alternative approach that is often easier:
Deploy your repo without any deployment scripts.
Go to the site's Kudu Console.
From the Tools menu, choose 'Download deployment script'. You'll get a zip with a .deployment and deploy.cmd files.
Commit both files at the root of your repo
Tweak them as needed
More information please refer to this link.
You can use kuduscript to generate the deployment script.
npm install -g kuduscript
kuduscript --python
Here is the list of options
Options:
-h, --help output usage information
-V, --version output the version number
-r, --repositoryRoot [dir path] The root path for the repository (default: .)
--aspWAP <projectFilePath> Create a deployment script for .NET web application, specify the project file path
--aspNetCore <projectFilePath> Create a deployment script for ASP.NET Core web application, specify the project file path
--aspWebSite Create a deployment script for basic website
--go Create a deployment script for Go website
--node Create a deployment script for node.js website
--ruby Create a deployment script for ruby website
--php Create a deployment script for php website
--python Create a deployment script for python website
--functionApp [projectFilePath] Create a deployment script for function App, specify the project file path if using msbuild
--basic Create a deployment script for any other website
--dotNetConsole <projectFilePath> Create a deployment script for .NET console application, specify the project file path
-s, --solutionFile <file path> The solution file path (sln)
-p, --sitePath <directory path> The path to the site being deployed (default: same as repositoryRoot)
-t, --scriptType <batch|bash|posh> The script output type (default: batch)
-o, --outputPath <output path> The path to output generated script (default: same as repository root)
-y, --suppressPrompt Suppresses prompting to confirm you want to overwrite an existing destination file.
--no-dot-deployment Do not generate the .deployment file.
--no-solution Do not require a solution file path (only for --aspWAP otherwise ignored).

How do I deploy to GAE from the Google Cloud Source Repository?

I'm considering moving my php Google App Engine project from Codenvy to the Google Source Repository and edit it there with the Source Editor, but I don't see how to cause it to deploy my project. How do I do that?
Here's what works for me, found through guesswork, trial, error, Billy and only a little docs.
I had set up GC Repositories to have a repository which is a mirror to bitbucket , auto-named default. Note: gcloud for default below can fail to recognise a repository that got that name by Rename. And can mistake a non-existent repository for an empty one.
Recipe 1
UPDATE: Now, after updating the bitbucket source, the deployed app does not show the update, despite "Deployment successful"]4. I don't know why - perhaps due to version number. Workaround: Use Recipe 2.
1 Ensure project's app.yaml file contains application: and version: e.g. this
2 Go to Google Cloud Patform and select the project
3 Click Activate Google Cloud Shell http://i.imgur.com/Axjy17q.png
4 In Google Cloud Shell, enter:
gcloud source repos clone default
appcfg.py update default
rm -rf default
This took ~20s to deploy and ~30s to complete.
Recipe 2
1 Ensure project's app.yaml file does not contain application: or version: (else you'll get an error like this) e.g. this
2 Go to Google Cloud Patform and select the project
3 Click Activate Google Cloud Shell http://i.imgur.com/Axjy17q.png
4 In Google Cloud Shell enter:
gcloud source repos clone default
gcloud --quiet app deploy default/app.yaml
rm -rf default
Warning: This can leave a previous version accessible.
This took ~65s to complete.
Re timing, compare this, taking ~20s . Timings are for a Hello World project.

Run PreSync/PostSync commands via WPP deploy.cmd

I'm trying to figure out how to run a pre/post command using the deploy.cmd generated by VS/MSBuild. I understand there are pre/postsync commands which can be set on the command line with msbuild but this is fixed within the web deploy package inside of the x.deploy.cmd.
How do I go about customizing the output of this file so that I can run the deploy command with specific parameters?
The intention is a non-developer will pick up the package zip file and import the application into IIS. We use IIS to host some windows services and so to be able to deploy we need to stop and uninstall the service before deployment and then install restart in the post deploy stage.
For certain servers we allow auto deployments from TFS and hook this pre/post command using the .targets file of the msbuild WPP pipeline. However, we want to this to be available to the manual deploy command files.
PreSync/PostSync are features of the msdeploy command line and are not supported by the package/manifest providers, or even the API. They are equivalent to running msdeploy a second time, so there's no way you'll be able to include their functionality while directly importing the package into IIS.
I'd recommend having a batch/powershell file on the server that the user runs after copying the package into the same directory.
The .cmd file that MSBuild generates is boilerplate script that you can simply change to call your pre/post powershell scripts. Just overwrite the one generated by the build with your custom one.

Resources