练习 - 将更改推送到包

已完成

此时,你有两个管道。 其中一个将 Models 包发布到 Azure Artifacts。 另一个用于 Space Game Web 应用。 Web 应用的生成配置引用 Models 包,以便它可以访问模型类。

在这里,你将练习更新模型软件包,并在 Web 应用中应用该更改。

为此,首先将属性添加到其中一个模型类,然后升级包版本。 然后,将更改提交到 GitHub,以便管道可以生成包并将其发布到 Azure Artifacts。

然后更新 Web 应用以引用模型包的较新版本版本号,以便它可以使用添加的属性。

创建分支

首先创建保存你工作的分支。 创建一个名为 add-game-style 的分支,基于 main 分支。

此时,将打开两个 Visual Studio Code 副本,一个用于 Tailspin.SpaceGame.Web.Models 项目,一个用于 Space Game Web 应用项目 Tailspin.SpaceGame.Web。 使用“Tailspin.SpaceGame.Web.Models”项目的副本

  1. 在 Visual Studio Code 中打开集成终端。

  2. 若要创建名为 add-game-style的分支,请在 git checkout 终端中运行以下命令。

    git checkout -B add-game-style
    

将属性添加到 Models 包

向其中一个模型类添加一个名为 Score 的属性,该类提供游戏样式或难度,该 属性与评分相关联。

使用“Tailspin.SpaceGame.Web.Models”项目的 Visual Studio Code 副本

  1. 在 Visual Studio Code 中,打开 Tailspin.SpaceGame.Web.Models/models/Score.cs。 将以下突出显示的属性添加到已在那里的属性列表中。

    using System.Text.Json.Serialization;
    
    namespace TailSpin.SpaceGame.Web.Models
    {
        public class Score : Model
        {
            // The ID of the player profile associated with this score.
            [JsonPropertyName("profileId")]
            public string ProfileId { get; set; }
    
            // The score value.
            [JsonPropertyName("score")]
            public int HighScore { get; set; }
    
            // The game mode the score is associated with.
            [JsonPropertyName("gameMode")]
            public string GameMode { get; set; }
    
            // The game region (map) the score is associated with.
            [JsonPropertyName("gameRegion")]
            public string GameRegion { get; set; }
    
            // The game style (difficulty) the score is associated with.
            [JsonPropertyName("gameStyle")]
            public string GameStyle { get; set; }
        }
    }
    

    注释

    您正在修改项目中的文件,以演示如何提升版本号。 但是,不要更新 Web 应用以使用新属性。

  2. 保存文件。

  3. 若要验证工作,请生成项目:

    dotnet build --configuration Release
    

    在实际应用中,你可能会执行更多验证步骤,例如运行测试或者使用一个使用新包的应用程序进行测试。

生成并发布包

将新属性添加到 Score 类并验证项目生成成功后,可以更新包版本。 然后,可以将更改推送到 GitHub,以便 Azure Pipelines 可以生成和发布更新的包。

  1. 打开azure-pipelines.yml,将minorVersion0更改为1,然后保存文件。

    minorVersion: '1'
    

    在这里,你将版本从 1.0.0 升级到 1.1.0,以明确更改。 在实践中,你将遵循正在使用的包类型的版本控制方案。

    例如,根据语义版本控制,将次要版本提升到 1(1.1.0)会告知其他人,该包与使用该包版本 1.0.0 的应用向后兼容。 然后,使用包的用户可能会修改其应用以使用新功能。

    常用的开源项目以 变更日志 的形式提供文档,其中介绍了每个版本中所做的更改以及如何从一个主要版本迁移到下一个主版本。

  2. 暂存、提交和推送更改:

    git add .
    git commit -m "Add GameStyle property"
    git push origin add-game-style
    
  3. 从 Microsoft Azure Pipelines 转到 Tailspin.SpaceGame.Web.Models 项目,并观看构建过程。

  4. 打开“ 项目 ”选项卡并记下新版本。 不用担心;对于仍引用旧版本的任何项目,你的旧版本仍然存在。

    Azure Artifacts 中包的屏幕截图,其中显示了包版本 1.1。

  5. 如同之前一样,请写下下一个单元的新版本。

请参照模型包的新版本

现在,将 Tailspin.SpaceGame.Web 项目更改为使用 Tailspin.SpaceGame.Web.Models 包的新版本。

在这里,请从用于 Space Game 的 Web 应用项目 Tailspin.SpaceGame.Web 的 Visual Studio Code 副本开始工作。

  1. 在 Visual Studio Code 中,打开 Tailspin.SpaceGame.Web.csproj,并更改为 PackageReference 在 Azure Artifacts 中创建的 Tailspin.SpaceGame.Web.Models 包的版本号。 然后保存文件。

    下面是一个示例:

    <PackageReference Include="Tailspin.SpaceGame.Web.Models" Version="1.1.0-CI-20210528-202436" />
    

    如果 Visual Studio Code 要求还原包,则可以安全地忽略该消息。 为了简洁起见,我们不会在本地生成 Web 应用。

  2. 从终端,暂存、提交和推送更改。

    git add .
    git commit -m "Bump Models package to 1.1.0"
    git push origin models-package
    
  3. 从 Azure Pipelines 中,转到“mslearn-tailspin-spacegame-web”项目并观察生成运行。

    从生成输出中可以看到,它获取最新的依赖项,构建应用并发布用于 Web 应用的工件。