如何让 MSSQL 服务容器在 Azure DevOps 管道中工作?

las*_*ink 5 sql-server automated-tests docker azure-devops azure-pipelines

描述

我正在azure devops 管道中尝试用于集成数据库测试服务容器

根据这个开源的虚拟 ci cd 管道项目https://dev.azure.com/funktechno/_git/dotnet%20ci%20pipelines。我正在尝试使用 azure devops服务容器进行集成管道测试。我让 postgress 和 mysql 工作。我在使用microsoft sql server 时遇到问题。

.yml 文件

resources:
  containers:
  - container: mssql
    image: mcr.microsoft.com/mssql/server:2017-latest
    ports: 
      # - 1433
      - 1433:1433
    options: -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=yourStrong(!)Password' -e 'MSSQL_PID=Express'

- job: unit_test_db_mssql
  # condition: eq('${{ variables.runDbTests }}', 'true')
  # continueOnError: true
  pool:
    vmImage: 'ubuntu-latest'
  services:
    localhostsqlserver: mssql
  steps:
    - task: UseDotNet@2
      displayName: 'Use .NET Core sdk 2.2'
      inputs:
        packageType: sdk
        version: 2.2.207
        installationPath: $(Agent.ToolsDirectory)/dotnet
    - task: NuGetToolInstaller@1
    - task: NuGetCommand@2
      inputs:
        restoreSolution: '$(solution)'
    - task: Bash@3
      inputs:
        targetType: 'inline'
        script: 'env | sort'

        #  echo Write your commands here...
        #   echo ${{agent.services.localhostsqlserver.ports.1433}}
        #   echo Write your commands here end...
    - task: CmdLine@2
      displayName: 'enabledb'
      inputs:
        script: |
          cp ./MyProject.Repository.Test/Data/appSettings.devops.mssql.json ./MyProject.Repository.Test/Data/AppSettings.json
    - task: DotNetCoreCLI@2
      inputs:
        command: 'test'
        workingDirectory: MyProject.Repository.Test
        arguments: '--collect:"XPlat Code Coverage"'

    - task: PublishCodeCoverageResults@1
      inputs:
        codeCoverageTool: 'Cobertura'
        summaryFileLocation: '$(Agent.TempDirectory)\**\coverage.cobertura.xml'
Run Code Online (Sandbox Code Playgroud)

数据库连接字符串

{
    "sqlserver": {
        "ConnectionStrings": {
            "Provider": "sqlserver",
            "DefaultConnection": "User ID=sa;Password=yourStrong(!)Password;Server=localhost;Database=mockDb;Pooling=true;"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

调试

  1. 我能说的最多的是,当我运行 Bash@3 来检查环境变量 postgres 和 mysql 打印类似于

    resources:
      containers:
      - container: mssql
        image: mcr.microsoft.com/mssql/server:2017-latest
        ports: 
          # - 1433
          - 1433:1433
        options: -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=yourStrong(!)Password' -e 'MSSQL_PID=Express'
    
    - job: unit_test_db_mssql
      # condition: eq('${{ variables.runDbTests }}', 'true')
      # continueOnError: true
      pool:
        vmImage: 'ubuntu-latest'
      services:
        localhostsqlserver: mssql
      steps:
        - task: UseDotNet@2
          displayName: 'Use .NET Core sdk 2.2'
          inputs:
            packageType: sdk
            version: 2.2.207
            installationPath: $(Agent.ToolsDirectory)/dotnet
        - task: NuGetToolInstaller@1
        - task: NuGetCommand@2
          inputs:
            restoreSolution: '$(solution)'
        - task: Bash@3
          inputs:
            targetType: 'inline'
            script: 'env | sort'
    
            #  echo Write your commands here...
            #   echo ${{agent.services.localhostsqlserver.ports.1433}}
            #   echo Write your commands here end...
        - task: CmdLine@2
          displayName: 'enabledb'
          inputs:
            script: |
              cp ./MyProject.Repository.Test/Data/appSettings.devops.mssql.json ./MyProject.Repository.Test/Data/AppSettings.json
        - task: DotNetCoreCLI@2
          inputs:
            command: 'test'
            workingDirectory: MyProject.Repository.Test
            arguments: '--collect:"XPlat Code Coverage"'
    
        - task: PublishCodeCoverageResults@1
          inputs:
            codeCoverageTool: 'Cobertura'
            summaryFileLocation: '$(Agent.TempDirectory)\**\coverage.cobertura.xml'
    
    Run Code Online (Sandbox Code Playgroud)
    • 而 mssql 无法打印 id
    {
        "sqlserver": {
            "ConnectionStrings": {
                "Provider": "sqlserver",
                "DefaultConnection": "User ID=sa;Password=yourStrong(!)Password;Server=localhost;Database=mockDb;Pooling=true;"
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
    • 更新使用 ubuntu-latest 时不会发生这种情况,但仍然存在 mssql 连接问题
  2. 数据库错误日志记录。

    • 我目前在管道中收到此错误
    /bin/bash --noprofile --norc /home/vsts/work/_temp/b9ec7d77-4bc2-47ab-b767-6a5e95ec3ea6.sh
        "id": "b294d39b9cc1f0d337bdbf92fb2a95f0197e6ef78ce28e9d5ad6521496713708"
      "pg11": {
      }
    
    Run Code Online (Sandbox Code Playgroud)
    • 在 TestDbContext.cs 中,我的错误消息是这样的。如果人们有获取更多详细信息的指针,将不胜感激。
    ========================== Starting Command Output ===========================
    /bin/bash --noprofile --norc /home/vsts/work/_temp/70ae8517-5199-487f-9067-aee67f8437bb.sh
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)

示例管道:https : //dev.azure.com/funktechno/dotnet%20ci%20pipelines/_build/results?buildId=73&view=logs&j=ce03965c-7621-5e79-6882-94ddf3daf982&t=a73693a5e-33094a722

笔记

  • 我已经知道 azure devops 管道 mssql 服务器在 windows 代理 b/c 中不起作用,它们是 windows server 2019,并且 mssql server 的 windows 容器版本没有得到很好的支持,仅适用于 windows server 2016。它在初始化时失败当我这样做时,容器步骤。

  • 我为 unit_test_db_mssql 尝试了几件事,更改 ubuntu 版本,更改参数,更改 mssql 服务器版本,都给出了相同的错误。

  • 如果人们知道在 linux 中工作的命令行方法来测试 mssql docker 实例是否准备好,这也可能有所帮助。

进度更新

  • 得到了MSSQL在工作github上的行动gitlab管道至今。
  • postgresmysqlazure devopsbitbucket 管道上工作得很好。
  • 仍然没有运气让mssqlazure devops上工作.bitbucket虽然它确实提供了许多正在运行的数据库的 docker 详细信息,但它不会工作得很好,但是再一次没有人真正关心 bitbucket 管道,因为它们只有 50 分钟。您可以在引用的存储库中看到所有这些管道,它们都是公开的,拉取请求也是可能的,因为它是开源的。

las*_*ink 2

根据Starain Chen [MSFT]https://developercommunity.visualstudio.com/content/problem/1159426/working-examples-using-service-container-of-sql-se.html的帮助。看起来需要延迟 10 秒才能等待容器准备就绪。

添加

  - task: PowerShell@2
    displayName: 'delay 10'
    inputs:
      targetType: 'inline'
      script: |
        # Write your PowerShell commands here.
        
        start-sleep -s 10
Run Code Online (Sandbox Code Playgroud)

使数据库连接正常工作。我假设那时 mssql docker 容器可能已经准备好了。