当我写一年前的答案时,Azure DevOps 没有用于构建管道的 Web 应用程序部署任务,因此必须使用发布管道来完成。通过构建管道进行部署要好得多,我强烈推荐它,因为它允许您将所有 CI/CD 作业提交到存储库。 (构建管道在侧栏中标记为“管道”,发布管道位于“发布”中。)
因此,这个答案适用于通过构建管道部署 Web 应用程序。如果您想使用发布管道,请参阅我的旧答案。
- 创建服务连接。 https://stackoverflow.com/questions/59259493/how-to-create-a-service-connection-for-azure-in-azure-devops-with-pictures
- 假设您的 Web 应用程序有一个服务连接,并且在 DevOps 中有一个 Node.js 项目,请创建一个
package.json
and 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
。 YAML 架构文档是.
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://learn.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://learn.microsoft.com/en-us/azure/devops/pipelines/targets/webapp?view=azure-devops&tabs=yaml
- 替换中的项目特定值
AzureWebApp@1
任务并将此文件推送到您的存储库。
- 转到管道页面并单击新建管道。管道选项(非常简单):
- “你的密码在哪里?” Azure 存储库 Git
- “选择存储库”选择您的存储库
- “配置管道”现有 Azure Pipelines YAML 文件
- “选择现有的 YAML 文件”路径
azure-pipelines.yml
- 单击运行
- 第一次运行时,可能会提示服务连接未经授权。单击“授权资源”,然后使用“队列”按钮再次手动运行构建将解决此问题。
- 要运行构建作业,请创建一个标签并推送它。返回到构建列表,您将看到部署作业正在运行。
- 要通过 Web 界面执行此操作: 转至 Repos 下的 Tags 页面,然后单击 New Tag。由于某种原因它需要标签描述,所以我只复制标签名称。
- 构建成功完成后,转到您的站点,您应该会看到 hello world 消息。
- 如果您的站点显示应用程序错误消息,您可以通过转到 Azure 门户中的 Web 应用程序,然后转到侧边栏中的“日志流”页面来检查错误日志。请注意,应用程序容器仅在有人访问网页后才会启动。因此,要测试应用程序初始化,您必须首先访问您的网页。