Setting up multiple containers in github actions

huangapple go评论89阅读模式
英文:

Setting up multiple containers in github actions

问题

我正在尝试使用GitHub Actions设置我们的CI。在自己尝试了几天后,我来这里寻求帮助。

实际上,我有一个使用PostgreSQL数据库的Go API,我的测试直接调用该API,所以我需要API和数据库在测试时运行。

在本地,我没有任何问题,我的API在我使用Makefile中的命令构建的Docker容器中运行。但是我无法让我的测试在GitHub Actions上工作。

这是我的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"]

我在本地的操作基本上是:
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

实际的问题是我无法将PostgreSQL连接到创建的网络,但即使这样工作,我觉得我不应该在yml文件中构建我的Docker容器。

我现在感到非常迷茫,已经搜索了相当长的时间,所以希望能在这里得到帮助。

谢谢你的时间,祝你有愉快的一天。

英文:

I'm trying to set up our CI with github actions. After trying for a couple days to do it on my own, I come here looking for help.

Actually I have a Go API that uses a postgres database and my tests directly call the API, so I need the API and the database to be running for the tests.

Locally I don't have any problem, my API runs in a docker container I build using commands in my Makefile. But I can't get my tests to work on github actions.

Here's my 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

And my Dockerfile for the app

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" ]

What I do locally is basically
make start_db
make migrate_db
make start_server
make test

On github actions I use that:

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

The actual problem is I can't connect postgres to the created network, but even if that worked, I feel like I shouldn't build my docker container the way I do it in the yml file.

I'm quite lost right now and I've been searching for quite some time so I hope I can get help here.

Thank you for your time, have a nice day.

答案1

得分: 2

根据这个链接,你不需要任何复杂的Docker网络设置来使服务容器可访问。在这个例子中,如果你的测试假设使用localhost,你可以考虑使用命令行标志或环境变量来设置postgres主机,而不是硬编码它。

英文:

According to this you don't need any kind of fancy docker network setup for the service container to be reachable, in this example the postgres host just becomes "postgres" if your tests are assuming localhost you could consider a command line flag or env variable that sets the postgres host instead of having it hardcoded

huangapple
  • 本文由 发表于 2021年8月24日 15:15:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/68903256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定