在 Azure 自动化中使用源代码管理集成 - 传统Use source control integration in Azure Automation - Legacy

备注

有一种新的源代码管理体验。There is a new experience for source control. 若要了解有关新体验的更多信息,请参阅源代码管理(预览)To learn more about the new experience, see Source Control (Preview).

源代码管理集成可让你将自动化帐户中的 Runbook 关联到 GitHub 源代码管理存储库。Source Control integration allows you to associate runbooks in your Automation account to a GitHub source control repository. 使用源代码管理可轻松与团队协作、跟踪更改,以及回退到旧版 Runbook。Source control allows you to easily collaborate with your team, track changes, and roll back to earlier versions of your runbooks. 例如,源代码管理可让你将源代码管理中的不同分支同步到开发、测试或生产自动化帐户,以轻松地将已在开发环境中测试过的代码提升到生产自动化帐户。For example, source control allows you to sync different branches in source control to your development, test or production Automation accounts, making it easy to promote code that has been tested in your development environment to your production Automation account.

源代码管理可让你将代码从 Azure 自动化推送到源代码管理,或将 Runbook 从源代码管理拉取到 Azure 自动化。Source Control allows you to push code from Azure Automation to source control or pull your runbooks from source control to Azure Automation. 本文介绍如何在 Azure 自动化环境中设置源代码管理。This article describes how to set up source control in your Azure Automation environment. 我们将首先配置 Azure 自动化以访问 GitHub 存储库,并演练可使用源代码管理集成完成的不同操作。We start by configuring Azure Automation to access your GitHub repository and walk through different operations that can be done using source control integration.

备注

源代码管理支持拉取和推送 PowerShell 工作流 Runbook 以及 PowerShell RunbookSource Control supports pulling and pushing PowerShell Workflow runbooks as well as PowerShell runbooks. 目前不支持图形 RunbookGraphical runbooks are not yet supported.

配置源代码管理Configure source control

为自动化帐户设置源代码管理时,需要执行两个简单的步骤;如果已有 GitHub 帐户,则只需要执行一个步骤。There are two simple steps required to configure source control for your Automation account, and only one if you already have a GitHub account.

创建 GitHub 存储库Create a GitHub repository

如果已有想要链接到 Azure 自动化的 GitHub 帐户和存储库,请登录到现有帐户并从下面的步骤 2 开始。If you already have a GitHub account and a repository that you want to link to Azure Automation, then sign into your existing account and start from step 2 below. 否则,请导航到 GitHub,注册新帐户并创建新的存储库Otherwise, navigate to GitHub, sign up for a new account, and create a new repository.

设置源代码管理Set up source control

  1. 在 Azure 门户的“自动化帐户”页上,单击“帐户设置”下的“源代码管理” 。From the Automation Account page in the Azure portal, under Account Settings, click Source Control.

  2. “源代码管理”页面随即打开,可以在其中配置 GitHub 帐户详细信息。The Source Control page opens, where you can configure your GitHub account details. 下面是要配置的参数的列表:Below is the list of parameters to configure:

    ParameterParameter 说明Description
    选择源Choose Source 选择源。Select the source. 目前仅支持 GitHubCurrently, only GitHub is supported.
    授权Authorization 单击“授权”按钮,授予 GitHub 存储库的 Azure 自动化访问权限。Click the Authorize button to grant Azure Automation access to your GitHub repository. 如果已在不同的窗口中登录 GitHub 帐户,将使用该帐户的凭据。If you are already logged in to your GitHub account in a different window, then the credentials of that account are used. 成功授权之后,页面上的“授权属性”下将显示你的 GitHub 用户名。Once authorization is successful, the page shows your GitHub user name under Authorization Property.
    选择存储库Choose repository 从可用存储库列表中选择 GitHub 存储库。Select a GitHub repository from the list of available repositories.
    选择分支Choose branch 从可用分支列表中选择分支。Select a branch from the list of available branches. 如果尚未创建任何分支,则只显示 master 分支。Only the master branch is shown if you haven’t created any branches.
    Runbook 文件夹路径Runbook folder path Runbook 文件夹路径可指定 GitHub 存储库中的路径,以便从中推送或提取代码。The runbook folder path specifies the path in the GitHub repository from which you want to push or pull your code. 必须以 /foldername/subfoldername 格式输入路径。It must be entered in the format /foldername/subfoldername. 只有 Runbook 文件夹路径中的 Runbook 才同步到自动化帐户。Only runbooks in the runbook folder path will be synced to your Automation account. Runbook 文件夹路径的子文件夹中的 Runbook 不会同步。Runbooks in the subfolders of the runbook folder path will NOT be synced. 使用 / 来同步存储库下的所有 Runbook。Use / to sync all the runbooks under the repository.
  3. 例如,如果有名为 PowerShellScripts 的存储库,其中包含名为 RootFolder 的文件夹,而该文件夹包含名为 SubFolder 的文件夹。For example, if you have a repository named PowerShellScripts that contains a folder named RootFolder, which contains a folder named SubFolder. 那么,可以使用以下字符串来同步每个文件夹级别:You can use the following strings to sync each folder level:

    1. 若要从存储库同步 Runbook,则 Runbook 文件夹路径为 /To sync runbooks from repository, runbook folder path is /.
    2. 若要从 RootFolder 同步 Runbook,则 Runbook 文件夹路径为 /RootFolderTo sync runbooks from RootFolder, runbook folder path is /RootFolder.
    3. 若要从 SubFolder 同步 Runbook,则 Runbook 文件夹路径为 /RootFolder/SubFolderTo sync runbooks from SubFolder, runbook folder path is /RootFolder/SubFolder.
  4. 配置参数后,它们会显示在“设置源代码管理”页面上。After you configure the parameters, they are displayed on the Set Up Source Control page.

    显示设置的“源代码管理”页

  5. 单击“确定”后,随即会针对你的自动化帐户配置源代码管理集成,并且应会使用你的 GitHub 信息更新该集成。Once you click OK, source control integration is now configured for your Automation account and should be updated with your GitHub information. 现在,可以单击此部分来查看所有源代码管理同步作业历史记录。You can now click on this part to view all your source control sync job history.

    当前配置的源代码管理配置的值

  6. 设置源代码管理后,将在自动化帐户中创建两个变量资产After you set up source control, Two variable assets are created in your Automation account. 此外,会将一个已授权的应用程序添加到你的 GitHub 帐户。Additionally, an authorized application is added to your GitHub account.

    • 变量 Microsoft.Azure.Automation.SourceControl.Connection 包含连接字符串的值,如下所示。The variable Microsoft.Azure.Automation.SourceControl.Connection contains the values of the connection string, as shown below.

      ParameterParameter Value
      Name Microsoft.Azure.Automation.SourceControl.ConnectionMicrosoft.Azure.Automation.SourceControl.Connection
      Type 字符串String
      Value {"Branch":<Your branch name>,"RunbookFolderPath":<Runbook folder path>,"ProviderType":<has a value 1 for GitHub>,"Repository":<Name of your repository>,"Username":<Your GitHub user name>}{"Branch":<Your branch name>,"RunbookFolderPath":<Runbook folder path>,"ProviderType":<has a value 1 for GitHub>,"Repository":<Name of your repository>,"Username":<Your GitHub user name>}
    • 变量 Microsoft.Azure.Automation.SourceControl.OAuthToken 包含 OAuthToken 的安全加密值。The variable Microsoft.Azure.Automation.SourceControl.OAuthToken, contains the secure encrypted value of your OAuthToken.

      ParameterParameter Value
      Name Microsoft.Azure.Automation.SourceControl.OAuthToken
      Type Unknown(Encrypted)
      Value <Encrypted OAuthToken>

      显示了源代码管理变量的窗口

    • 自动化源代码管理已作为已授权的应用程序添加到 GitHub 帐户。Automation Source Control is added as an authorized application to your GitHub account. 若要查看应用程序,请从 GitHub 主页导航到“配置文件” > “设置” > “应用程序”。To view the application, from your GitHub home page, navigate to profile > Settings > Applications. 此应用程序可让 Azure 自动化将 GitHub 存储库同步到自动化帐户。This application allows Azure Automation to sync your GitHub repository to an Automation account.

      GitHub 中的应用程序设置

在自动化中使用源代码管理Use source control in Automation

Runbook 签入可让你将对 Azure 自动化中的 Runbook 所做的更改推送到源代码管理存储库。Runbook check in allows you to push the changes you have made to a runbook in Azure Automation into your source control repository. 下面是签入 Runbook 的步骤:Below are the steps to check in a runbook:

  1. 从自动化帐户中创建新的文本 Runbook,或编辑现有的文本 RunbookFrom your Automation account, create a new textual runbook, or edit an existing, textual runbook. 此 Runbook 可以是 PowerShell 工作流或 PowerShell 脚本 Runbook。This runbook can be either a PowerShell Workflow or a PowerShell script runbook.

  2. 编辑 Runbook 之后,将其保存,然后单击“编辑”页上的“签入”。After you edit your runbook, save it and click Check-in on the edit page.

    显示了“签入 GitHub”按钮的窗口

    备注

    从 Azure 自动化签入会覆盖源代码管理中当前存在的代码。Check-in from Azure Automation overwrites the code that currently exists in your source control. 用于签入的 Git 等效命令行指令为 git add + git commit + git pushThe Git equivalent command-line instruction to check-in is git add + git commit + git push

  3. 单击“签入”时,会通过一条确认消息进行提示,请单击“是”以继续操作。 When you click check-in, you are prompted with a confirmation message, click Yes to continue.

    一个要求确认签入到源代码管理的对话框

  4. 签入操作会启动源代码管理 runbook:Sync-MicrosoftAzureAutomationAccountToGitHubV1Check-in starts the source control runbook: Sync-MicrosoftAzureAutomationAccountToGitHubV1. 此 Runbook 将连接到 GitHub 并将 Azure 自动化中的更改推送到存储库。This runbook connects to GitHub and pushes changes from Azure Automation to your repository. 若要查看已签入的作业历史记录,请返回到“源代码管理集成”选项卡,单击以打开“存储库同步”页面。To view the checked in job history, go back to the Source Control Integration tab and click to open the Repository Synchronization page. 此页将显示你的所有源代码管理作业。This page shows all your source control jobs. 选择要查看的作业,并单击以查看详细信息。Select the job you want to view and click to view the details.

    显示了同步作业结果的窗口

    备注

    源代码管理 Runbook 是特殊的自动化 Runbook,无法查看或编辑。Source control runbooks are special Automation runbooks that you cannot view or edit. 虽然它们不显示在 Runbook 列表上,但你可以看到显示在作业列表中的同步作业。While they do not show up on your runbook list, you see sync jobs showing in your jobs list.

  5. 修改后的 Runbook 的名称将发送为已签入 Runbook 的输入参数。The name of the modified runbook is sent as an input parameter for the checked in runbook. 可以通过在“存储库同步”页中展开 Runbook 来查看作业详细信息You can view the job details by expanding runbook on the Repository Synchronization page.

    显示了同步作业的输入的窗口

  6. 在作业完成时刷新 GitHub 存储库可以查看更改。Refresh your GitHub repository once the job completes to view the changes. 存储库中应有一个提交项,其提交消息为:“已在 Azure 自动化中更新 Runbook 名称。”There should be a commit in your repository with a commit message: Updated Runbook Name in Azure Automation.

将源代码管理中的 Runbook 同步到 Azure 自动化Sync runbooks from source control to Azure Automation

使用“存储库同步”页面上的“同步”按钮,可以将存储库的 Runbook 文件夹路径中的所有 Runbook 提取到自动化帐户。The sync button on the Repository Synchronization page allows you to pull all the runbooks in the runbook folder path of your repository to your Automation account. 同一个存储库可以同步到多个自动化帐户。The same repository can be synced to more than one Automation account. 以下是同步 Runbook 的步骤:Below are the steps to sync a runbook:

  1. 从设置源代码管理的自动化帐户中,打开“源代码管理集成/存储库同步”页面,然后单击“同步”。在出现确认消息提示时,单击“是”以继续操作。From the Automation account where you set up source control, open the Source Control Integration/Repository Synchronization page and click Sync. You're prompted with a confirmation message, click Yes to continue.

    “同步”按钮,以及确认将要同步所有 Runbook 的消息

  2. 同步功能将启动 Sync-MicrosoftAzureAutomationAccountFromGitHubV1 Runbook,此 Runbook 将连接到 GitHub 并将存储库中的更改拉取到 Azure 自动化。Sync starts the Sync-MicrosoftAzureAutomationAccountFromGitHubV1 runbook, which connects to GitHub and pulls the changes from your repository to Azure Automation. 此操作的“存储库同步”页中应该会显示一个新作业。You should see a new job on the Repository Synchronization page for this action. 若要查看同步作业的详细信息,请单击以打开作业详细信息页面。To view details about the sync job, click to open the job details page.

    显示了针对 GitHub 存储库执行同步作业后的同步结果的窗口

    备注

    从源代码管理进行的同步针对当前在源代码管理中的所有 Runbook,覆盖当前存在于自动化帐户中的 Runbook 草稿版本。A sync from source control overwrites the draft version of the runbooks that currently exist in your Automation account for ALL runbooks that are currently in source control. 用于同步的 Git 等效命令行指令为 git pullThe Git equivalent command-line instruction to sync is git pull

显示了已挂起源代码管理同步作业中的所有日志的窗口

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

若要断开与 GitHub 帐户的连接,请打开“存储库同步”页面,并单击“断开连接”。To disconnect from your GitHub account, open the Repository Synchronization page and click Disconnect. 断开与源代码管理的连接后,前面同步的 Runbook 仍会保留在自动化帐户中,但不会启用“存储库同步”页面。Once you disconnect source control, runbooks that were synced earlier still remain in your Automation account but the Repository Synchronization page will not be enabled.

显示了“断开连接”按钮的窗口,该按钮用于断开与源代码管理的连接