自定义部署到Azure网站 [英] Custom Deployment to Azure Websites

查看:202
本文介绍了自定义部署到Azure网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用 Gulp.js 将我所有的CSS和JavaScript打包成单个文件,然后将其包含在我的网络应用程序中。我的网络应用程序是用Python编写的(使用 Flask )。



我显然不想跟踪Gulp输出的CSS和JS文件git(因为它们构建输出文件)。



我使用push来部署我的网站到Azure网站。也就是说,我简单地运行 git push azure master ,Azure自动计算出我使用Python,设置 virtualenv ,安装 pip 依赖关系等等。 本文介绍如何设置此功能



此过程非常好,但现在我已经开始使用Gulp,我想确保连接的JavaScript和CSS文件也在服务器端生成每当我部署网站时,



此外,在将来,我希望Azure在部署时运行所有测试,只有在所有测试都通过后才能成功部署。 / p>

令人遗憾的是,我还没有找到一个令人满意的解决方案,因为我无法为Azure的自动部署过程添加自定义步骤。



我已经尝试使用 Kudu 编写自定义部署脚本(由这篇博客文章),但是这样做会使Azure通常无法执行所有的自动步骤S;运行 azure site deploymentscript --python 只生成一个非常基本的Kudu部署文件,它不处理 web.config 文件,设置 virtualenv 或安装依赖关系。我没有找到关于如何自己做的文件;我使用默认的自动化Azure部署脚本(当我推送代码时,它会生成服务器端,所以我无法自己访问),因为没有处理像virtualenv和pip依赖关系的东西。



是否有任何解决方法可用,以便我可以自定义我的部署脚本(例如运行Gulp),同时仍然正确地部署Flask?

解决方案

由于Kudu是开源的,可以在GitHub上使用,我在问题跟踪器中提出了这个问题(链接,任何有兴趣的人)。代码所有者vas非常有帮助,并指出我的解决方案。


  1. 访问网站的Kudu服务: yourwebsite。 scm.azurewebsites.net

  2. 单击工具>下载部署脚本,并获取为您的代码生成的部署脚本Azure(这不限于Flask应用程序) 。

  3. (可选)脚本是Windows批处理脚本。我把它移植到Bash,因为我更熟悉它,Azure网站也支持它。

  4. 添加自定义的东西,你的喜好:使用Gulp / Grunt构建东西,运行测试和如果出现任何失败,您将不能部署(退出非零错误代码)等。您还应该记住首先检查是否使用 npm 安装了Gulp / Grunt之类的内容, code> package.json 文件。请注意,只有在KuduSync有机会从存储库中复制新文件之前,您才应将该部署标记为失败,否则将导致部署失败。您的Web根文件夹,即使例如您的测试失败。


这是我的代码处理Gulp.js的片段对任何有兴趣的人查看由 azure site deploymentscript --node 生成的脚本,了解如何选择正确的Node和npm版本的示例:

  selectNodeVersion 
echo调用\$ NPM_CMD install\...
eval $ NPM_CMD install
exitWithMessageOnError无法运行'npm install'你有必要的权限吗?
echo完成npm安装。

#路径似乎没有设置好。使用这个黑客来运行gulp。
GULP =node_modules / gulp / bin / gulp.js

echo运行gulp ...
$ GULPproduction
exitWithMessageOnError无法运行'gulp'。'npm install'运行正常吗?
echo已完成的gulp。

不要忘记实际添加一个 package.json 文件包含所有必要的Gulp依赖项目。 Azure提供了Node.js(和npm),而不是Gulp.js.希望这有帮助!



PS:请注意,这个整个过程有点恶作剧,完全正确的方法是使用连续集成代理。 p>

I recently started using Gulp.js to package all my CSS and JavaScript into single files, which I then include in my web app. My web app is written in Python (using Flask).

I obviously don't want to track the Gulp output CSS and JS files using git (since they're build output files).

I deploy my website to Azure Websites using push to deploy. That is, I simply run git push azure master and Azure automatically figures out that I'm using Python, sets up virtualenv, installs pip dependencies and so on. This article describes how to set this up.

This process works great, but now that I've started using Gulp, I want to ensure that the concatenated JavaScript and CSS files are also produced on the server side whenever I deploy the website.

Moreover, in the future, I would like to have Azure run all the tests upon deployment, and only successfully deploy if they all pass.

Sadly, I have yet to find a satisfying solution for this workflow, since I cannot add custom steps to Azure's automatic deployment process.

I have tried writing a custom deployment script using Kudu (as suggested by this blog post), but doing so disables all the automatic steps Azure normally does; running azure site deploymentscript --python only generates a very basic Kudu deployment file which doesn't handle reading in the web.config file, setting up virtualenv or installing the dependencies. I found no documentation regarding how to do this myself; I have use the default, automatic Azure deployment script (which gets generated server-side when I push the code, so I cannot access it myself) because otherwise stuff like virtualenv and pip dependencies aren't handled.

Is there any workaround available so that I may customize my deployment script (e.g. to run Gulp) while still correctly deploying Flask?

解决方案

Since Kudu is open source and available on GitHub, I have brought up this problem in its issue tracker (link, for anyone interested). The code owner vas very helpful and pointed me towards a solution.

  1. Access the site's Kudu Services at: yourwebsite.scm.azurewebsites.net.
  2. Click Tools > Download Deployment Script and get the deployment script Azure generated for your code (this isn't limited to Flask apps).
  3. (Optional) The script is a Windows batch script. I ported it to Bash, because I'm more familiar with it and Azure Websites supports it as well.
  4. Add custom stuff to your liking: build stuff using Gulp/Grunt, run tests and fail the deployment (exiting with nonzero error code) if any fail, etc. You should also remember to first check whether stuff like Gulp/Grunt is installed using npm and the appropriate package.json file.
    • note that you should only mark the deployment as failed before KuduSync has a chance to copy your new files from the repository, since otherwise they'll end up in your web root folder even though e.g. your tests have failed.

Here's the fragment of my code handling Gulp.js, for anyone who's interested. Look at the script generated by azure site deploymentscript --node for examples on how to select the correct Node and npm versions:

selectNodeVersion
echo "Invoking \"$NPM_CMD install\"..."
eval $NPM_CMD install
exitWithMessageOnError "Could not run 'npm install'.  Do you have the necessary privileges?"
echo "Finished npm install."

# The path doesn't seem to get set OK.  Use this hack to run gulp.
GULP="node_modules/gulp/bin/gulp.js"

echo "Running gulp..."
"$GULP" production
exitWithMessageOnError "Could not run 'gulp'.  Did 'npm install' run OK?"
echo "Finished gulp."

Don't forget to actually add a package.json file containing all the necessary Gulp dependencies to your project. Azure provides Node.js (and npm) but not Gulp.js. Hope this helps!

P.S.: Do note that this whole process is a bit hacky and that the completely correct way to do this is by using a continuous integration agent.

这篇关于自定义部署到Azure网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆