Setting up multiple containers in github actions

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

Setting up multiple containers in github actions

问题

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

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

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

这是我的Makefile:

  1. GOCMD=go
  2. GOBUILD=$(GOCMD) build
  3. GOCLEAN=$(GOCMD) clean
  4. GOTEST=$(GOCMD) test
  5. GOGET=$(GOCMD) get
  6. BINARY_NAME=ATS-user
  7. all: test build
  8. build:
  9. $(GOBUILD) -o $(BINARY_NAME) -v
  10. test:
  11. $(GOTEST) -v ./...
  12. clean:
  13. $(GOCLEAN)
  14. rm -f $(BINARY_NAME)
  15. run:
  16. $(GOBUILD) -o $(BINARY_NAME) -v ./...
  17. ./$(BINARY_NAME)
  18. start_db:
  19. sudo docker network create network_app
  20. sudo docker run --name postgresdb --net network_app -p5432:5432 -e POSTGRES_PASSWORD=***** -e POSTGRES_DB=userdb -d postgres
  21. migrate_db:
  22. migrate -path tests/migrate/ -database postgres://postgres:*****@localhost:5432/userdb?sslmode=disable -verbose up
  23. start_server:
  24. sudo docker build . -t app
  25. sudo docker run -p 8080:8080 --net network_app app
  26. remove_db:
  27. sudo docker container stop postgresdb
  28. CONTAINER_ID=$(shell sudo docker ps -aqf "name=postgresdb");\
  29. sudo docker container rm $$CONTAINER_ID
  30. remove_server:
  31. sudo docker container stop app
  32. CONTAINER_ID=$(shell sudo docker ps -aqf "name=app");\
  33. sudo docker container rm $$CONTAINER_ID

以及我的应用程序的Dockerfile:

  1. FROM golang:1.16-alpine
  2. WORKDIR /app
  3. COPY go.mod ./
  4. COPY go.sum ./
  5. RUN go mod download
  6. COPY . ./
  7. RUN go build -o /mdr
  8. EXPOSE 8080
  9. CMD ["/mdr"]

我在本地的操作基本上是:
make start_db
make migrate_db
make start_server
make test

在GitHub Actions中,我使用以下内容:

  1. name: build and test
  2. # Controls when the action will run.
  3. on:
  4. workflow_dispatch:
  5. # A workflow run is made up of one or more jobs that can run sequentially or in parallel
  6. jobs:
  7. # This workflow contains a single job called "test"
  8. test:
  9. # The type of runner that the job will run on
  10. runs-on: ubuntu-latest
  11. #Add services, aka Docker containers that runs in paralell
  12. services:
  13. #Name the service
  14. postgres:
  15. #Set the Docker image to use, find images on Dockerhub.com
  16. image: postgres
  17. # Set environment variables
  18. env:
  19. POSTGRES_USER: postgres
  20. POSTGRES_PASSWORD: *****
  21. POSTGRES_DB: userdb
  22. # Expose ports
  23. ports:
  24. - 5432:5432
  25. # Add some health options to make sure PostgreSQL is running fully before moving on
  26. options: >-
  27. --health-cmd pg_isready
  28. --health-interval 10s
  29. --health-timeout 5s
  30. --health-retries 5
  31. # Steps represent a sequence of tasks that will be executed as part of the job
  32. steps:
  33. # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
  34. - uses: actions/checkout@v2
  35. # Sets up and installs Golang
  36. - name: Setup Go environment
  37. uses: actions/setup-go@v2.1.3
  38. with:
  39. go-version: 1.16
  40. - name: Get migrate
  41. run: go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
  42. - name: migrate DB
  43. run: make migrate_db
  44. - name: create network
  45. run: docker network create network_app
  46. - name: Connect Postgresdb to network
  47. run: docker network connect network_app postgres
  48. - name: Build docker
  49. run: docker build . --file Dockerfile -t app
  50. - name: Run docker
  51. run: docker run -p 8080:8080 app
  52. # Run tests that are available
  53. - name: Test
  54. run: |
  55. go test -v ./tests/test_routes/
  56. 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:

  1. GOCMD=go
  2. GOBUILD=$(GOCMD) build
  3. GOCLEAN=$(GOCMD) clean
  4. GOTEST=$(GOCMD) test
  5. GOGET=$(GOCMD) get
  6. BINARY_NAME=ATS-user
  7. all: test build
  8. build:
  9. $(GOBUILD) -o $(BINARY_NAME) -v
  10. test:
  11. $(GOTEST) -v ./...
  12. clean:
  13. $(GOCLEAN)
  14. rm -f $(BINARY_NAME)
  15. run:
  16. $(GOBUILD) -o $(BINARY_NAME) -v ./...
  17. ./$(BINARY_NAME)
  18. start_db:
  19. sudo docker network create network_app
  20. sudo docker run --name postgresdb --net network_app -p5432:5432 -e POSTGRES_PASSWORD=***** -e POSTGRES_DB=userdb -d postgres
  21. migrate_db:
  22. migrate -path tests/migrate/ -database postgres://postgres:*****@localhost:5432/userdb?sslmode=disable -verbose up
  23. start_server:
  24. sudo docker build . -t app
  25. sudo docker run -p 8080:8080 --net network_app app
  26. remove_db:
  27. sudo docker container stop postgresdb
  28. CONTAINER_ID=$(shell sudo docker ps -aqf "name=postgresdb");\
  29. sudo docker container rm $$CONTAINER_ID
  30. remove_server:
  31. sudo docker container stop app
  32. CONTAINER_ID=$(shell sudo docker ps -aqf "name=app");\
  33. sudo docker container rm $$CONTAINER_ID

And my Dockerfile for the app

  1. FROM golang:1.16-alpine
  2. WORKDIR /app
  3. COPY go.mod ./
  4. COPY go.sum ./
  5. RUN go mod download
  6. COPY . ./
  7. RUN go build -o /mdr
  8. EXPOSE 8080
  9. 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:

  1. name: build and test
  2. # Controls when the action will run.
  3. on:
  4. workflow_dispatch:
  5. # A workflow run is made up of one or more jobs that can run sequentially or in parallel
  6. jobs:
  7. # This workflow contains a single job called "test"
  8. test:
  9. # The type of runner that the job will run on
  10. runs-on: ubuntu-latest
  11. #Add services, aka Docker containers that runs in paralell
  12. services:
  13. #Name the service
  14. postgres:
  15. #Set the Docker image to use, find images on Dockerhub.com
  16. image: postgres
  17. # Set environment variables
  18. env:
  19. POSTGRES_USER: postgres
  20. POSTGRES_PASSWORD: *****
  21. POSTGRES_DB: userdb
  22. # Expose ports
  23. ports:
  24. - 5432:5432
  25. # Add some health options to make sure PostgreSQL is running fully before moving on
  26. options: >-
  27. --health-cmd pg_isready
  28. --health-interval 10s
  29. --health-timeout 5s
  30. --health-retries 5
  31. # Steps represent a sequence of tasks that will be executed as part of the job
  32. steps:
  33. # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
  34. - uses: actions/checkout@v2
  35. # Sets up and installs Golang
  36. - name: Setup Go environment
  37. uses: actions/setup-go@v2.1.3
  38. with:
  39. go-version: 1.16
  40. - name: Get migrate
  41. run: go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
  42. - name: migrate DB
  43. run: make migrate_db
  44. - name: create network
  45. run: docker network create network_app
  46. - name: Connect Postgresdb to network
  47. run: docker network connect network_app postgres
  48. - name: Build docker
  49. run: docker build . --file Dockerfile -t app
  50. - name: Run docker
  51. run: docker run -p 8080:8080 app
  52. # Run tests that are available
  53. - name: Test
  54. run: |
  55. go test -v ./tests/test_routes/
  56. 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:

确定