如何解决如何使MSSQL服务容器在Azure DevOps管道中工作?
说明
我正在使用服务容器进行Azure的devops管道中的集成数据库测试。
根据此开源虚拟ci管道项目https://dev.azure.com/funktechno/_git/dotnet%20ci%20pipelines。我正在尝试使用Azure devops service containers进行集成管道测试。我有postgress和mysql工作。我与 Microsoft sql服务器有关。
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'
数据库连接字符串
{
"sqlserver": {
"ConnectionStrings": {
"Provider": "sqlserver","DefaultConnection": "User ID=sa;Password=yourStrong(!)Password;Server=localhost;Database=mockDb;Pooling=true;"
}
}
}
调试
-
我最多可以说的是,当我运行Bash @ 3来检查环境变量后,postgres和mysql打印出类似于
/bin/bash --noprofile --norc /home/vsts/work/_temp/b9ec7d77-4bc2-47ab-b767-6a5e95ec3ea6.sh "id": "b294d39b9cc1f0d337bdbf92fb2a95f0197e6ef78ce28e9d5ad6521496713708" "pg11": { }
- 当mssql无法显示ID时
========================== Starting Command Output =========================== /bin/bash --noprofile --norc /home/vsts/work/_temp/70ae8517-5199-487f-9067-aee67f8437bb.sh } }
- 更新,使用ubuntu-latest时不会发生,但仍然存在mssql连接问题
-
数据库错误记录。
- 我目前正在处理此错误
Error Message: Failed for sqlserver providername:Microsoft.EntityFrameworkCore.SqlServer m:A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider,error: 40 - Could not open a connection to SQL Server)
- 在TestDbContext.cs中,我的错误消息是这样。如果人们有获取更多详细信息的指针,将不胜感激。
catch (System.Exception e) { var assertMsg = "Failed for " + connectionStrings.Provider + "\n" + " providername:" + dbContext.Database.ProviderName + " m:"; if (e.InnerException != null) assertMsg += e.InnerException.Message; else assertMsg += e.Message; _exceptionMessage = assertMsg; }
示例管道:https://dev.azure.com/funktechno/dotnet%20ci%20pipelines/_build/results?buildId=73&view=logs&j=ce03965c-7621-5e79-6882-94ddf3daf982&t=a73693a5-1de9-5e3d-a243-942c60ab4775
注释
-
我已经知道azure devops管道mssql服务器在Windows代理b / c中不起作用,它们是Windows Server 2019,并且mssql服务器的Windows容器版本不受很好的支持,仅适用于Windows Server 2016在执行初始化容器步骤时失败。
-
我为unit_test_db_mssql尝试了几件事,更改了ubuntu版本,更改了参数,更改了mssql服务器版本,所有这些都产生了相同的错误。
-
如果人们知道可在linux中使用的命令行方法来测试mssql docker实例是否准备就绪,那么这也可能会有所帮助。
进度更新
-
到目前为止,
- 已 mssql 在 github操作和 gitlab管道中工作。
- postgres 和 mysql 在 azure devops 和 bitbucket管道上正常工作。
- 让 mssql 用于 azure devops 仍然不走运。bitbucket不能很好地工作,尽管它确实提供了许多正在运行的数据库的docker详细信息,但是再说一次,没有人真正关心50分钟的比特桶管道。您可以在引用的存储库中看到所有这些管道,它们都是公共的,因为它是开源的,所以也可以进行拉取请求。
解决方法
根据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
使数据库连接正常工作。我假设到那时mssql docker容器已经准备好了。
,我在过去几天中遇到了这个问题,并看到了您的帖子。我得到了相同的行为,然后点击了一些东西。
重要提示:如果您在 Windows 上使用 PowerShell 来运行这些命令,请使用双引号而不是单引号。
那个笔记来自https://hub.docker.com/_/microsoft-mssql-server
我相信改变你的路线
options: -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=yourStrong(!)Password' -e 'MSSQL_PID=Express'
到
options: -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=yourStrong(!)Password" -e "MSSQL_PID=Express"
应该让它工作。我认为同样有趣的是,在您在上面发布的链接中,密码作为自己的行传入,这可能解决了问题,不一定是 10 秒延迟。 (下例)
- container: mssql
image: mcr.microsoft.com/mssql/server:2017-latest
env:
ACCEPT_EULA: Y
SA_PASSWORD: Password123
MSSQL_PID: Express
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。