Cannot connect to SQL Server from GitHub action

Hello,

I have a github action that does build and deploy and after that it runs a task to update the database. It looks like this:

name: .NET

on:
  push:
    branches: [develop, master]

env:
  WORKING_DIRECTORY: src
  ASPNETCORE_ENVIRONMENT: Staging
  CONFIGURATION: Staging
  DEPLOYMENT_DIR: mywebsite.com/wwwroot/
  SMTP_USERNAME: ${{ secrets.SMTP_USERNAME }}
  SMTP_PASSWORD: ${{ secrets.SMTP_PASSWORD }}

jobs:
  build-and-deploy:
    name: Build and deploy
    runs-on: ubuntu-latest
    defaults:
     run:
        shell: bash
        working-directory: ${{ env.WORKING_DIRECTORY }}

    steps:
    - uses: actions/checkout@v2
    - name: Setup .NET
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: '5.0.x'

    - name: Restore dependencies
      run: dotnet restore

    - name: Build
      run: dotnet build --configuration ${{ env.CONFIGURATION }} --no-restore

    - name: Test
      run: dotnet test --no-restore --verbosity normal

    - name: Publish
      run: dotnet publish --configuration ${{ env.CONFIGURATION }} --no-restore --output 'smileonwebapp' -p:BlazorEnableCompression=false --framework net5.0 --runtime win-x86 --self-contained

    - name: FTP Deploy
      uses: airvzxf/ftp-deployment-action@master
      with: 
        server: ${{ secrets.FTP_SERVER_URL }}
        user: ${{ secrets.FTP_USERNAME }}
        password: ${{ secrets.FTP_PASSWORD }}
        local_dir: ${{ env.WORKING_DIRECTORY }}/
        remote_dir: ${{ env.DEPLOYMENT_DIR }}
        delete: "true"

    - name: Update Database
      working-directory: ${{ env.WORKING_DIRECTORY }}/MyApp.Web/Server
      run: |
        dotnet tool install --global dotnet-ef
        dotnet tool restore
        dotnet ef database update --configuration ${{ env.CONFIGURATION }} --prefix-output --verbose

The last task always fails with the following error:

info: Microsoft.Data.SqlClient.SqlException (0x80131904): 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: 35 - An internal exception was caught)

check_suite_focus=true#step:12:65)info: ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (00000005, 0xFFFDFFFF): Name or service not known

check_suite_focus=true#step:12:66)info: at System.Net.Dns.GetHostEntryOrAddressesCore(String hostName, Boolean justAddresses)

check_suite_focus=true#step:12:67)info: at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)

check_suite_focus=true#step:12:68)info: at Microsoft.Data.SqlClient.SNI.SNITCPHandle.Connect(String serverName, Int32 port, TimeSpan timeout, Boolean isInfiniteTimeout, String cachedFQDN, SQLDNSInfo& pendingDNSInfo)

check_suite_focus=true#step:12:69)info: at Microsoft.Data.SqlClient.SNI.SNITCPHandle..ctor(String serverName, Int32 port, Int64 timerExpire, Object callbackObject, Boolean parallel, String cachedFQDN, SQLDNSInfo& pendingDNSInfo)

check_suite_focus=true#step:12:70)info: at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)

check_suite_focus=true#step:12:71)info: at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)

check_suite_focus=true#step:12:72)info: at Microsoft.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover, SqlAuthenticationMethod authType)

check_suite_focus=true#step:12:73)info: at Microsoft.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)

check_suite_focus=true#step:12:74)info: at Microsoft.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)

check_suite_focus=true#step:12:75)info: at Microsoft.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)

check_suite_focus=true#step:12:76)info: at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken, DbConnectionPool pool)

check_suite_focus=true#step:12:77)info: at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)

check_suite_focus=true#step:12:78)info: at Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)

check_suite_focus=true#step:12:79)info: at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)

check_suite_focus=true#step:12:80)info: at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)

check_suite_focus=true#step:12:81)info: at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)

check_suite_focus=true#step:12:82)info: at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)

check_suite_focus=true#step:12:83)info: at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)

check_suite_focus=true#step:12:84)info: at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)

check_suite_focus=true#step:12:85)info: at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)

check_suite_focus=true#step:12:86)info: at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)

check_suite_focus=true#step:12:87)info: at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides)

check_suite_focus=true#step:12:88)info: at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.OpenDbConnection(Boolean errorsExpected)

check_suite_focus=true#step:12:89)info: at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternal(Boolean errorsExpected)

check_suite_focus=true#step:12:90)info: at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)

check_suite_focus=true#step:12:91)info: at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.<>c__DisplayClass18_0.<Exists>b__0(DateTime giveUp)

check_suite_focus=true#step:12:92)info: at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass12_0`2.<Execute>b__0(DbContext c, TState s)

check_suite_focus=true#step:12:93)info: at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)

check_suite_focus=true#step:12:94)info: at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation, Func`2 verifySucceeded)

check_suite_focus=true#step:12:95)info: at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation)

check_suite_focus=true#step:12:96)info: at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists(Boolean retryOnNotExists)

check_suite_focus=true#step:12:97)info: at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists()

check_suite_focus=true#step:12:98)info: at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()

check_suite_focus=true#step:12:99)info: at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)

check_suite_focus=true#step:12:100)info: at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)

check_suite_focus=true#step:12:101)info: at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)

check_suite_focus=true#step:12:102)info: at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()

check_suite_focus=true#step:12:103)info: at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)

check_suite_focus=true#step:12:104)info: ClientConnectionId:00000000-0000-0000-0000-000000000000
error: 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: 35 - An internal exception was caught)
1 Like

Hello @vladislav-naydenov!

I think that I would expect this step to fail? Since you’re running it on a GitHub-hosted runner, you would need to be able to connect directly to your database from the internet. I hope that your database is nicely tucked away behind a firewall and not reachable from random internet hosts. :fearful:

You might want to run a self-hosted runner in this situation, so that you can do the database update step from a machine behind your firewall, near your database server (network-wise).