Home > database >  GitHub Actions MSSQL: Resource temporarily unavailable
GitHub Actions MSSQL: Resource temporarily unavailable

Time:01-31

I am building a CI workflow using GitHub Actions. Goal is to build and test a .NET C# application using a MSSQL database.

The database can successfully start and the database is created. In the logs of the DB container I can see the DB was created and is running.

In the testing step all tests fail with this error:

System.Net.Internals.SocketExceptionFactory ExtendedSocketException: System.Net.Internals.SocketExceptionFactory ExtendedSocketException: Resource temporarily unavailable. Aborting test execution.

The database name is correctly given to the program. I have tested this by printing the connection string to the console. Also it actually connects to the DB (because when the database server name is incorrent I get a error that reflects that).

The CI workflow:

name: .NET Backend Build and run Unit Tests

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]
  workflow_dispatch:

jobs:
  build_and_test:
    runs-on: ubuntu-latest

    services:
      sqlserver:
        image: mcr.microsoft.com/mssql/server:2022-latest
        ports:
          - 1433:1433
        env:
          ACCEPT_EULA: "Y"
          SA_PASSWORD: "redacted"
          MSSQL_PID: "Express"
          MSSQL_COLLATION: "SQL_Latin1_General_CP1_CI_AS"

    steps:
    - name: get Container ID
      run: echo "DATABASE_SERVER=$(docker ps --all --filter status=running --format "{{.ID}}")" >> $GITHUB_ENV

    - name: create database
      run: docker exec $DATABASE_SERVER /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'redacted' -Q 'CREATE DATABASE dbname'

    - uses: actions/checkout@v3

    - name: Setup .NET
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: 6.0.x

    - name: Restore dependencies
      run: dotnet restore

    - name: Build
      run: dotnet build --no-restore

    - name: Test
      env:
        DATABASE_PORT: 1433
        DATABASE_NAME: dbname
        DATABASE_USER: sa
        DATABASE_PASSWORD: redacted
      run: dotnet test --no-build --verbosity normal

CodePudding user response:

I found out the Docker in GitHub Actions does not support DNS and behaves fundamentally different than regular Docker on Linux. All network communication must be made over the host network. So I had to specify localhost as the database server.

This would be the correct workflow file:

name: .NET Backend Build and run Unit Tests

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]
  workflow_dispatch:

env:
  CI: true

jobs:
  build_and_test:
    runs-on: ubuntu-latest

    services:
      sqlserver:
        image: mcr.microsoft.com/mssql/server:2022-latest
        ports:
          - 1433:1433
        env:
          ACCEPT_EULA: "Y"
          MSSQL_SA_PASSWORD: "redacted"
          MSSQL_PID: "Express"
          MSSQL_COLLATION: "SQL_Latin1_General_CP1_CI_AS"

    steps:
    - name: get Container ID
      run: echo "DATABASE_ID=$(docker ps --all --filter status=running --format "{{.ID}}")" >> $GITHUB_ENV

    - name: create database
      run: docker exec $DATABASE_ID /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'redacted' -Q 'CREATE DATABASE dbname'

    - uses: actions/checkout@v3

    - name: Setup .NET
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: 6.0.x

    - name: Restore dependencies
      run: dotnet restore

    - name: Build
      run: dotnet build --no-restore

    - name: Test
      env:
        DATABASE_SERVER: localhost
        DATABASE_PORT: 1433
        DATABASE_NAME: dbname
        DATABASE_USER: sa
        DATABASE_PASSWORD: redacted
      run: dotnet test --no-build --verbosity normal
  • Related