简体   繁体   English

在 GitHub 操作中运行 Postgres 以测试我的 Go API

[英]Running Postgres in GitHub Actions to test my Go API

I've set up a GitHub workflow to start a Postgres instance inside a docker container.我已经设置了 GitHub 工作流来在 docker 容器内启动 Postgres 实例。 I then execute my web API to simply prove it connects.然后我执行我的 web API 来简单地证明它连接。 This satisfies the workflow.这满足了工作流程。

My workflow我的工作流程

name: Build

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

jobs:

  build:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:10.8
        env:
          POSTGRES_USER: test
          POSTGRES_PASSWORD: test
          POSTGRES_DB: test
          POSTGRES_PORT: 5432
        ports:
          - 5432/tcp
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

    steps:
      - uses: actions/checkout@v2

      - name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.15

      - name: Run
        env:
          # These are the expected envs in my code
          DB_HOST: localhost
          DB_USER: test
          DB_PASSWORD: test
          DB_NAME: test
          DB_PORT: 5432
          DB_DIALECT: postgres
          PORT: 8080
        run: make run

For brevity I've trimmed irrelavent bits of code out, this is my database package:为简洁起见,我删除了无关紧要的代码,这是我的数据库 package:

// Config Model
type Config struct {
    Host     string
    Name     string
    User     string
    Password string
    Port     string
    Dialect  string
}

// Open a new connection to a database
func Open(c Config) (*gorm.DB, error) {

    dsn := fmt.Sprintf("host=%s port=%s dbname=%s password=%s user=%s sslmode=disable",
        c.Host,
        c.Port,
        c.Name,
        c.Password,
        c.User,
    )

    db, err := gorm.Open(postgres.New(postgres.Config{
        DriverName: c.Dialect,
        DSN:        dsn,
    }), &gorm.Config{})

    if err != nil {
        return nil, fmt.Errorf("sql.Open: %v", err)
    }

    return db, nil
}

And this is called from my main package这是从我的主要 package 调用的

func main() {
    // Create Database Configuration
    dbConfig := database.Config{
        Host:     os.Getenv("DB_HOST"),
        Name:     os.Getenv("DB_NAME"),
        User:     os.Getenv("DB_USER"),
        Port:     os.Getenv("DB_PORT"),
        Password: os.Getenv("DB_PASSWORD"),
        Dialect:  os.Getenv("DB_DIALECT"),
    }

    // Connect to database
    db, err := database.Open(dbConfig)
    if err != nil {
        log.Fatalf("failed to connect to database: %s", err)
    }
}

The code fails to connect during the workflow with the given error:代码在工作流期间无法连接,并出现给定错误:

go run -ldflags "-X main.Version=afc0042" cmd/api/main.go
2021/02/10 09:55:12 Running Version: afc0042
2021/02/10 09:55:12 failed to connect to database: sql.Open: dial tcp [::1]:5432: connect: connection refused

2021/02/10 09:55:12 /home/runner/work/database/database.go:32
[error] failed to initialize database, got error dial tcp [::1]:5432: connect: connection refused

you can use localhost when only you are trying to connect your server locally but as docker is not local in your machine so you can't use localhost except you are inside docker container.当您仅尝试在本地连接服务器时,您可以使用 localhost 但由于 docker 在您的机器中不是本地的,因此您不能使用 localhost,除非您在 docker 容器内。

try with port forward .尝试使用port forward

ports:
  - 5432:5432 //update this one

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM