使用源代码管理集成Use source control integration

Azure 自动化中的源代码管理集成支持源代码管理存储库中的单向同步。Source control integration in Azure Automation supports single-direction synchronization from your source control repository. 通过源代码管理,可以使用 GitHub 或 Azure Repos 源代码管理存储库中的脚本使自动化帐户中的 Runbook 保持最新。Source control allows you to keep your runbooks in your Automation account up to date with scripts in your GitHub or Azure Repos source control repository. 此功能可以轻松地将已在开发环境中测试过的代码提升到生产自动化帐户。This feature makes it easy to promote code that has been tested in your development environment to your production Automation account.

使用源代码管理集成可轻松与团队协作、跟踪更改,以及回退到旧版 Runbook。Source control integration lets you easily collaborate with your team, track changes, and roll back to earlier versions of your runbooks. 例如,通过源代码管理可以将源代码管理中的不同分支同步到开发、测试和生产自动化帐户。For example, source control allows you to synchronize different branches in source control with your development, test, and production Automation accounts.

源代码管理类型Source control types

Azure 自动化支持三种类型的源代码管理:Azure Automation supports three types of source control:

  • GitHubGitHub
  • Azure Repos (Git)Azure Repos (Git)
  • Azure Repos (TFVC)Azure Repos (TFVC)

先决条件Prerequisites

  • 源代码管理存储库(GitHub 或 Azure Repos)A source control repository (GitHub or Azure Repos)
  • 一个运行方式帐户A Run As account
  • 自动化帐户中最新的 Azure 模块包括 Az.Accounts 模块(相当于 AzureRM.Profile 的 Az 模块)The latest Azure modules in your Automation account, including the Az.Accounts module (Az module equivalent of AzureRM.Profile)

备注

源代码管理同步作业以自动化帐户用户身份运行,并且按与其他自动化作业相同的费率计费。Source control synchronization jobs are run under the user's Automation account and are billed at the same rate as other Automation jobs.

配置源代码管理Configure source control

本部分介绍如何为自动化帐户配置源代码管理。This section tells how to configure source control for your Automation account. 可以使用 Azure 门户或 PowerShell。You can use either the Azure portal or PowerShell.

在 Azure 门户中配置源代码管理Configure source control in Azure portal

使用此过程通过 Azure 门户配置源代码管理。Use this procedure to configure source control using the Azure portal.

  1. 在自动化帐户中,选择“源代码管理”,然后单击“添加” 。In your Automation account, select Source Control and click Add.

    选择“源代码管理”

  2. 选择“源代码管理类型”,然后单击“身份验证” 。Choose Source Control type, then click Authenticate.

  3. 随即将打开一个浏览器窗口,并提示你登录。A browser window opens and prompts you to sign in. 根据提示完成身份验证。Follow the prompts to complete authentication.

  4. 在“源代码管理摘要”页上,使用字段填写下面定义的源代码管理属性。On the Source Control Summary page, use the fields to fill in the source control properties defined below. 完成后单击“保存”。Click Save when finished.

    propertiesProperty 说明Description
    源代码管理名称Source control name 源代码管理的友好名称。A friendly name for the source control. 该名称只能包含字母和数字。This name must contain only letters and numbers.
    源代码管理类型Source control type 源代码管理机制的类型。Type of source control mechanism. 可用选项包括:Available options are:
    * GitHub* GitHub
    * Azure Repos (Git)* Azure Repos (Git)
    * Azure Repos (TFVC)* Azure Repos (TFVC)
    存储库Repository 存储库或项目的名称。Name of the repository or project. 检索前 200 个存储库。The first 200 repositories are retrieved. 要搜索存储库,请在字段中键入名称,然后单击“在 GitHub 上搜索”。To search for a repository, type the name in the field and click Search on GitHub.
    分支Branch 要从源文件中提取的分支。Branch from which to pull the source files. 分支目标确定不适用于 TFVC 源代码管理类型。Branch targeting isn't available for the TFVC source control type.
    文件夹路径Folder path 包含要同步的 Runbook 的文件夹,例如 /Runbook。Folder that contains the runbooks to synchronize, for example, /Runbooks. 仅同步指定文件夹中的 Runbook。Only runbooks in the specified folder are synchronized. 不支持递归。Recursion isn't supported.
    自动同步1Auto Sync1 在源代码管理存储库中提交时打开或关闭自动同步的设置。Setting that turns on or off automatic synchronization when a commit is made in the source control repository.
    发布 RunbookPublish Runbook 如果从源代码管理同步后自动发布 Runbook,则设置为“开启”,否则设为“关闭”。Setting of On if runbooks are automatically published after synchronization from source control, and Off otherwise.
    说明Description 指定有关源代码管理的其他详细信息的文本。Text specifying additional details about the source control.

    1 要在配置与 Azure Repos 的源代码管理集成时启用自动同步,你必须是项目管理员。1 To enable Auto Sync when configuring source control integration with Azure Repos, you must be a Project Administrator.

    源代码管理摘要

备注

源代码管理存储库的登录名可能与 Azure 门户的登录不同。The login for your source control repository might be different from your login for the Azure portal. 配置源代码管理时,请确保使用正确的源代码管理存储库的帐户登录。Ensure that you are logged in with the correct account for your source control repository when configuring source control. 如果有疑问,请在浏览器中打开新的选项卡并从 dev.azure.com、visualstudio.com 或 github.com 中注销,然后尝试连接源代码管理 。If there is a doubt, open a new tab in your browser, log out from dev.azure.com, visualstudio.com, or github.com, and try reconnecting to source control.

在 PowerShell 中配置源代码管理Configure source control in PowerShell

还可以使用 PowerShell 在 Azure 自动化中配置源代码管理。You can also use PowerShell to configure source control in Azure Automation. 要使用 PowerShell cmdlet 执行此操作,需要个人访问令牌 (PAT)。To use the PowerShell cmdlets for this operation, you need a personal access token (PAT). 使用 New-AzAutomationSourceControl cmdlet 创建源代码管理连接。Use the New-AzAutomationSourceControl cmdlet to create the source control connection. 此 cmdlet 需要 PAT 的安全字符串。This cmdlet requires a secure string for the PAT. 要了解如何创建安全字符串,请参阅 ConvertTo-SecureStringTo learn how to create a secure string, see ConvertTo-SecureString.

以下小节说明如何使用 PowerShell 创建适用于 GitHub、Azure Repos (Git) 和 Azure Repos (TFVC) 的源代码管理连接。The following subsections illustrate PowerShell creation of the source control connection for GitHub, Azure Repos (Git), and Azure Repos (TFVC).

创建适用于 GitHub 的源代码管理连接Create source control connection for GitHub

New-AzAutomationSourceControl -Name SCGitHub -RepoUrl https://github.com/<accountname>/<reponame>.git -SourceType GitHub -FolderPath "/MyRunbooks" -Branch master -AccessToken <secureStringofPAT> -ResourceGroupName <ResourceGroupName> -AutomationAccountName <AutomationAccountName>

创建适合于 Azure Repos (Git) 的源代码管理连接Create source control connection for Azure Repos (Git)

备注

Azure Repos (Git) 使用的 URL 属于早期格式,只可访问 dev.azure.com,但不可访问 visualstudio.com 。Azure Repos (Git) uses a URL that accesses dev.azure.com instead of visualstudio.com, used in earlier formats. 早期的 URL 格式 https://<accountname>.visualstudio.com/<projectname>/_git/<repositoryname> 已被弃用,但仍受支持。The older URL format https://<accountname>.visualstudio.com/<projectname>/_git/<repositoryname> is deprecated but still supported. 首选新格式。The new format is preferred.

New-AzAutomationSourceControl -Name SCReposGit -RepoUrl https://dev.azure.com/<accountname>/<adoprojectname>/_git/<repositoryname> -SourceType VsoGit -AccessToken <secureStringofPAT> -Branch master -ResourceGroupName <ResourceGroupName> -AutomationAccountName <AutomationAccountName> -FolderPath "/Runbooks"

创建适用于 Azure Repos (TFVC) 的源代码管理连接Create source control connection for Azure Repos (TFVC)

备注

Azure Repos (TFVC) 使用的 URL 属于早期格式,只可访问 dev.azure.com,但不可访问 visualstudio.com 。Azure Repos (TFVC) uses a URL that accesses dev.azure.com instead of visualstudio.com, used in earlier formats. 早期的 URL 格式 https://<accountname>.visualstudio.com/<projectname>/_versionControl 已被弃用,但仍受支持。The older URL format https://<accountname>.visualstudio.com/<projectname>/_versionControl is deprecated but still supported. 首选新格式。The new format is preferred.

New-AzAutomationSourceControl -Name SCReposTFVC -RepoUrl https://dev.azure.com/<accountname>/<adoprojectname>/_git/<repositoryname> -SourceType VsoTfvc -AccessToken <secureStringofPAT> -ResourceGroupName <ResourceGroupName> -AutomationAccountName <AutomationAccountName> -FolderPath "/Runbooks"

个人访问令牌 (PAT) 权限Personal access token (PAT) permissions

源代码管理需要一些 PAT 的最低权限。Source control requires some minimum permissions for PATs. 以下小节包含 GitHub 和 Azure Repos 所需的最低权限。The following subsections contain the minimum permissions required for GitHub and Azure Repos.

GitHub 的最低 PAT 权限Minimum PAT permissions for GitHub

下表定义了 GitHub 所需的最低 PAT 权限。The following table defines the minimum PAT permissions required for GitHub. 如需详细了解如何在 GitHub 中创建 PAT,请参阅创建命令行的个人访问令牌For more information about creating a PAT in GitHub, see Creating a personal access token for the command line.

范围Scope 说明Description
repo
repo:status 访问提交状态Access commit status
repo_deployment 访问部署状态Access deployment status
public_repo 访问公共存储库Access public repositories
admin:repo_hook
write:repo_hook 写入存储库挂钩Write repository hooks
read:repo_hook 读取存储库挂钩Read repository hooks
Azure Repos 的最低 PAT 权限Minimum PAT permissions for Azure Repos

下表定义了 Azure Repos 所需的最低 PAT 权限。The following list defines the minimum PAT permissions required for Azure Repos. 如需详细了解如何在 Azure Repos 中创建 PAT,请参阅使用个人访问令牌进行身份验证访问For more information about creating a PAT in Azure Repos, see Authenticate access with personal access tokens.

范围Scope 访问类型Access Type
Code 读取Read
Project and team 读取Read
Identity 读取Read
User profile 读取Read
Work items 读取Read
Service connections 读取、查询、管理1Read, query, manage1

1只有在启用了自动同步时才需要 Service connections 权限。1 The Service connections permission is only required if you have enabled autosync.

与源代码管理同步Synchronize with source control

请按照以下步骤与源代码管理同步。Follow these steps to synchronize with source control.

  1. 请从“源代码管理”页面上的表中选择源。Select the source from the table on the Source control page.

  2. 单击“开始同步”以开始同步过程。Click Start Sync to start the sync process.

  3. 单击“同步作业”选项卡,查看当前同步作业或之前的同步作业的状态。View the status of the current sync job or previous ones by clicking the Sync jobs tab.

  4. 在“源代码管理”下拉列表中,选择一个源代码管理机制。On the Source Control dropdown menu, select a source control mechanism.

    同步状态

  5. 单击某个作业可以查看作业输出。Clicking on a job allows you to view the job output. 以下示例是源代码管理同步作业的输出。The following example is the output from a source control sync job.

    ===================================================================
    
    Azure Automation Source Control.
    Supported runbooks to sync: PowerShell Workflow, PowerShell Scripts, DSC Configurations, Graphical, and Python 2.
    
    Setting AzEnvironment.
    
    Getting AzureRunAsConnection.
    
    Logging in to Azure...
    
    Source control information for syncing:
    
    [Url = https://ContosoExample.visualstudio.com/ContosoFinanceTFVCExample/_versionControl] [FolderPath = /Runbooks]
    
    Verifying url: https://ContosoExample.visualstudio.com/ContosoFinanceTFVCExample/_versionControl
    
    Connecting to VSTS...
    
    Source Control Sync Summary:
    
    2 files synced:
     - ExampleRunbook1.ps1
     - ExampleRunbook2.ps1
    
    ==================================================================
    
    
  6. 通过在“源代码管理同步作业摘要”页上选择“所有日志”,可以获取附加日志记录。Additional logging is available by selecting All Logs on the Source Control Sync Job Summary page. 这些附加日志条目有助于你解决使用源代码管理时可能出现的问题。These additional log entries can help you troubleshoot issues that might arise when using source control.

断开连接源代码管理Disconnect source control

断开与源代码管理存储库的连接:To disconnect from a source control repository:

  1. 在自动化帐户中的“帐户设置”下打开源代码管理 。Open Source control under Account Settings in your Automation account.

  2. 选择要删除的源代码管理机制。Select the source control mechanism to remove.

  3. 在“源代码管理摘要”页面上,单击“删除”。On the Source Control Summary page, click Delete.

处理编码问题Handle encoding issues

如果多人使用不同的编辑器在源代码管理存储库中编辑 Runbook,则可能发生编码问题。If multiple people are editing runbooks in your source control repository using different editors, encoding issues can occur. 如需详细了解此情况,请参阅编码问题的常见原因To learn more about this situation, see Common causes of encoding issues.

更新 PATUpdate the PAT

目前,不能使用 Azure 门户在源代码管理中更新 PAT。Currently, you can't use the Azure portal to update the PAT in source control. 当 PAT 过期或吊销时,可以通过以下方式之一使用新的访问令牌更新源代码管理:When your PAT is expired or revoked, you can update source control with a new access token in one of these ways: