在 github actions 中设置多个容器

Gro*_*led 6 postgresql go docker 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
Run Code Online (Sandbox Code Playgroud)

以及我的应用程序的 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" ]
Run Code Online (Sandbox Code Playgroud)

我在本地所做的基本上是 make start_db make migrate_db make start_server make test

在 github actions 上我使用的是:

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
Run Code Online (Sandbox Code Playgroud)

实际的问题是我无法将 postgres 连接到创建的网络,但即使有效,我觉得我不应该像在 yml 文件中那样构建我的 docker 容器。

我现在很迷失,我已经寻找了很长一段时间,所以我希望我能在这里得到帮助。

感谢您抽出宝贵的时间,祝您有美好的一天。

小智 2

根据这个,你不需要任何花哨的docker网络设置来访问服务容器,在这个例子中,如果你的测试假设本地主机,postgres主机就变成“postgres”,你可以考虑命令行标志或环境变量设置 postgres 主机而不是对其进行硬编码