当我在一年前写下上一个答案时,azuredevops没有用于构建管道的web应用部署任务,因此必须使用发布管道来完成。通过构建管道进行部署要好得多,我强烈建议使用它,因为它允许您将所有CI/CD作业提交到repo(构建管道在侧栏中标记为“管道”,发布管道在“发布”中标记为“管道”。)
因此,这个答案适用于通过构建管道部署web应用程序。如果您想使用发布管道,请参阅我以前的答案。
-
Create a service connection.
-
假设您有一个web应用的服务连接,并且在DevOps中有一个Node.js项目,那么创建一个
package.json
和
main.js
{
"name": "test-project",
"version": "0.0.0",
"scripts": {
"start": "node main.js",
"test": ""
},
"dependencies": {
"express": "^4.17.1"
}
}
const express = require('express');
const app = express();
const port = process.env.PORT || 3000; // You can see your app's env variables in Kudu: https://<your app>.scm.azurewebsites.net/
app.get('/', (req, res) => res.send('Hello World!'));
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
-
现在需要管道的YAML文件。命名此文件
azure-pipelines.yml
here
.
trigger:
- '*' # Run pipeline when a commit is pushed to any branch
- 'refs/tags/*' # Run pipeline when a tag is pushed
jobs:
- job: test
pool:
vmImage: ubuntu-latest
steps:
- script: npm install
displayName: npm install
- script: npm run test
displayName: npm run test
- job: deploy
condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/') # Run deploy job only if triggered by tag
pool:
vmImage: ubuntu-latest
steps:
- script: npm install
displayName: npm install
# - script: npm run build # If you are using TypeScript
# displayName: npm run build
- task: AzureWebApp@1 # https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-rm-web-app?view=azure-devops
inputs:
azureSubscription: <service connection name> # Replace this with the service connection name
appName: test-project # Replace this with the web app name
package: $(Build.SourcesDirectory)
customWebConfig: -Handler iisnode -NodeStartFile main.js -appType node # https://docs.microsoft.com/en-us/azure/devops/pipelines/targets/webapp?view=azure-devops&tabs=yaml
-
AzureWebApp@1
-
转到“管道”页面,然后单击“新建管道”。管道选项(非常简单):
-
“你的代码在哪里?”Azure Repos Git
-
-
“配置管道”现有Azure管道YAML文件
-
“选择现有YAML文件”路径
-
单击Run
-
在第一次运行时,可能会说服务连接未经授权。单击“Authorize resources”,然后使用Queue按钮手动再次运行构建,将解决此问题。
-
-
要通过web界面执行此操作,请转到Repos下的Tags页,然后单击New Tag。出于某种原因,它需要一个标记描述,所以我只是复制标记名。
-
-
如果您的站点显示应用程序错误消息,您可以通过转到Azure门户中的web应用程序,然后在侧栏中登录流页面来检查错误日志。请注意,应用程序容器仅在有人访问网页后启动。因此,要测试应用程序初始化,您必须首先访问您的网页。