postgresql - 在 github 操作中设置多个容器

标签 postgresql docker go github-actions

我正在尝试使用 github 操作设置我们的 CI。在尝试了几天自己做之后,我来这里寻求帮助。
实际上我有一个使用 postgres 数据库的 Go API,我的测试直接调用 API,所以我需要 API 和数据库来运行测试。
在本地我没有任何问题,我的 API 在我使用 Makefile 中的命令构建的 docker 容器中运行。但是我无法让我的测试在 github 操作上工作。
这是我的 Makefile:

GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
BINARY_NAME=ATS-user

all: test build

build: 
    $(GOBUILD) -o $(BINARY_NAME) -v
test: 
    $(GOTEST) -v ./...

clean: 
    $(GOCLEAN)
    rm -f $(BINARY_NAME)
run:
    $(GOBUILD) -o $(BINARY_NAME) -v ./...
    ./$(BINARY_NAME)

start_db:
    sudo docker network create network_app
    sudo docker run --name postgresdb --net network_app -p5432:5432 -e POSTGRES_PASSWORD=***** -e POSTGRES_DB=userdb -d postgres

migrate_db:
    migrate -path tests/migrate/ -database postgres://postgres:*****@localhost:5432/userdb?sslmode=disable -verbose up

start_server:
    sudo docker build . -t app
    sudo docker run -p 8080:8080 --net network_app app

remove_db:
    sudo docker container stop postgresdb
    CONTAINER_ID=$(shell sudo docker ps -aqf "name=postgresdb");\
    sudo docker container rm $$CONTAINER_ID

remove_server:
    sudo docker container stop app
    CONTAINER_ID=$(shell sudo docker ps -aqf "name=app");\
    sudo docker container rm $$CONTAINER_ID
和我的应用程序的 Dockerfile
FROM golang:1.16-alpine

WORKDIR /app

COPY go.mod ./
COPY go.sum ./

RUN go mod download

COPY . ./

RUN go build -o /mdr

EXPOSE 8080

CMD [ "/mdr" ]
我在本地做的基本上是
制作 start_db
使 migrate_db
启动服务器
做测试
在 github 操作上,我使用它:
name: build and test

# Controls when the action will run. 
on:
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "test"
  test:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    #Add services, aka Docker containers that runs in paralell
    services:
      #Name the service
      postgres:
        #Set the Docker image to use, find images on Dockerhub.com
        image: postgres
        # Set environment variables
        env: 
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: *****
          POSTGRES_DB: userdb
        # Expose ports
        ports: 
          - 5432:5432
        # Add some health options to make sure PostgreSQL is running fully before moving on
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
          
          
    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2
      # Sets up and installs Golang
      - name: Setup Go environment
        uses: actions/setup-go@v2.1.3
        with:
          go-version: 1.16
      - name: Get migrate
        run: go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
      - name: migrate DB
        run: make migrate_db
      - name: create network
        run: docker network create network_app
      - name: Connect Postgresdb to network
        run: docker network connect network_app postgres
      - name: Build docker
        run: docker build . --file Dockerfile -t app
      - name: Run docker
        run: docker run -p 8080:8080 app
      # Run tests that are available
      - name: Test
        run: |
          go test -v ./tests/test_routes/
          echo Complete
实际问题是我无法将 postgres 连接到创建的网络,但即使这样有效,我觉得我不应该像在 yml 文件中那样构建我的 docker 容器。
我现在很迷茫,我一直在寻找很长一段时间,所以我希望我能在这里得到帮助。
谢谢你的时间,祝你有美好的一天。

最佳答案

根据 this您不需要任何花哨的 docker 网络设置即可访问服务容器,在 this 中例如,如果您的测试假设 localhost,则 postgres 主机将变为“postgres”,您可以考虑使用命令行标志或 env 变量来设置 postgres 主机,而不是对其进行硬编码

关于postgresql - 在 github 操作中设置多个容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68903256/

相关文章:

go - go版本1.12中的 'http.Client'和 '&http.Client'有什么区别

postgresql - 如何在 azure postgresql 服务上创建数据库模板

django - PostgreSQL 日期格式

sql - 如何使用 AT TIME ZONE 选择带有偏移量的时间戳

.net - 软件包安装在 docker 内,但实际命令提供异常

docker - 如何使用 docker-compose 在 laradock 中使用 zsh 而不是 bash

ruby - 无法向使用 docker 和 docker-compose 运行的 sinatra 应用程序发送请求

postgresql - Postgres 在数据库 : Could not detect default username 上运行查询时出错

google-app-engine - GAE/Go 在本地开发服务器挂起

postgresql - 当存在事件连接时删除数据库