使用部署按钮从 GitHub 存储库部署模板Use a deployment button to deploy templates from GitHub repository

本文介绍了如何使用“部署到 Azure”按钮从 GitHub 存储库部署模板。This article describes how to use the Deploy to Azure button to deploy templates from a GitHub repository. 可直接将此按钮添加到 GitHub 存储库中的 README.md 文件,You can add the button directly to the README.md file in your GitHub repository. 也可将其添加到引用该存储库的网页中。Or, you can add the button to a web page that references the repository.

部署范围由模板架构确定。The deployment scope is determined by the template schema. 有关详细信息,请参阅:For more information, see:

使用常用图像Use common image

若要将此按钮添加到网页或存储库,请使用以下图像:To add the button to your web page or repository, use the following image:

![Deploy to Azure](https://aka.ms/deploytoazurebutton)
<img src="https://aka.ms/deploytoazurebutton"/>

此图像显示为:The image appears as:

“部署到 Azure”按钮

创建用于部署模板的 URLCreate URL for deploying template

若要为模板创建 URL,请从存储库中模板的原始 URL 开始。To create the URL for your template, start with the raw URL to the template in your repo. 若要查看原始 URL,请选择“原始”。To see the raw URL, select Raw.

选择“原始”

URL 的格式为:The format of the URL is:

https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-storage-account-create/azuredeploy.json

然后,将 URL 转换为 URL 编码的值。Then, convert the URL to a URL-encoded value. 可以使用联机编码器,也可以运行一个命令。You can use an online encoder or run a command. 以下 PowerShell 示例展示了如何对值进行 URL 编码。The following PowerShell example shows how to URL encode a value.

$url = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-storage-account-create/azuredeploy.json"
[uri]::EscapeDataString($url)

进行 URL 编码后,示例 URL 具有以下值。The example URL has the following value when URL encoded.

https%3A%2F%2Fraw.githubusercontent.com%2FAzure%2Fazure-quickstart-templates%2Fmaster%2F101-storage-account-create%2Fazuredeploy.json

每个链接都以相同的基 URL 开头:Each link starts with the same base URL:

https://portal.azure.cn/#create/Microsoft.Template/uri/

将进行 URL 编码后的模板链接添加到基 URL 的末尾。Add your URL-encoded template link to the end of the base URL.

https://portal.azure.cn/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure%2Fazure-quickstart-templates%2Fmaster%2F101-storage-account-create%2Fazuredeploy.json

你已具有该链接的完整 URL。You have your full URL for the link.

通常,将模板托管在公共存储库中。Typically, you host the template in a public repo. 如果使用专用存储库,则必须包含一个令牌来访问模板的原始内容。If you use a private repo, you must include a token to access the raw contents of the template. GitHub 生成的令牌仅在短时间内有效。The token generated by GitHub is valid for only a short time. 需要经常更新链接。You would need to update the link often.

如果使用包含 Azure Repos 的 Git 而不是 GitHub 存储库,那么仍可使用“部署到 Azure”按钮。If you're using Git with Azure Repos instead of a GitHub repo, you can still use the Deploy to Azure button. 请确保存储库是公共的。Make sure your repo is public. 使用项操作获取模板。Use the Items operation to get the template. 请求应采用以下格式:Your request should be in the following format:

https://dev.azure.com/{organization-name}/{project-name}/_apis/git/repositories/{repository-name}/items?scopePath={url-encoded-path}&api-version=6.0

对此请求 URL 进行编码。Encode this request URL.

创建“部署到 Azure”按钮Create Deploy to Azure button

最后,将链接和图像放在一起。Finally, put the link and image together.

若要向 GitHub 存储库中的 README.md 文件或者向网页中添加带 Markdown 的按钮,请使用:To add the button with Markdown in the README.md file in your GitHub repository or a web page, use:

[![Deploy to Azure](https://aka.ms/deploytoazurebutton)](https://portal.azure.cn/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure%2Fazure-quickstart-templates%2Fmaster%2F101-storage-account-create%2Fazuredeploy.json)

对于 HTML,请使用:For HTML, use:

<a href="https://portal.azure.cn/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure%2Fazure-quickstart-templates%2Fmaster%2F101-storage-account-create%2Fazuredeploy.json" target="_blank">
  <img src="https://aka.ms/deploytoazurebutton"/>
</a>

对于包含 Azure 存储库的 Git,该按钮的格式为:For Git with Azure repo, the button is in the format:

[![Deploy to Azure](https://aka.ms/deploytoazurebutton)](https://portal.azure.cn/#create/Microsoft.Template/uri/https%3A%2F%2Fdev.azure.com%2Forgname%2Fprojectname%2F_apis%2Fgit%2Frepositories%2Freponame%2Fitems%3FscopePath%3D%2freponame%2fazuredeploy.json%26api-version%3D6.0)

部署模板Deploy the template

若要测试整个解决方案,请选择以下按钮:To test the full solution, select the following button:

“部署到 Azure”Deploy to Azure

门户会显示一个窗格,你可以在其中轻松地提供参数值。The portal displays a pane that allows you to easily provide parameter values. 这些参数预先填充了来自模板的默认值。The parameters are pre-filled with the default values from the template.

使用门户进行部署

后续步骤Next steps