【问题标题】:How to target container in GitHub actions?如何在 GitHub 操作中定位容器?
【发布时间】:2020-11-04 13:57:17
【问题描述】:

我正在尝试使用 GitHub Actions 为我的测试启动 Postgres 容器。我有一个名为 build.sh 的脚本,当通过 GitHub 操作调用 npm run build 时会调用该脚本。该脚本调用restore-schema.sh(如下所示)。

这里的问题是当restore-schema.sh 运行时,我不断收到Error: no such container: postgres。 GitHub 操作将容器命名为任意字符串。有没有办法可以在图像上运行 docker exec 或以某种方式命名 GitHub 操作正在创建的 postgres 容器?我已经查看了这两个文档,但无济于事。

我应该怎么做?我注意到在 Docker run ps 屏幕截图中,它显示了命令 docker-entrypoint.sh。我应该改用这个吗?我是否在 .github/workflows/ 中指定 Dockerfile?

我尝试包含尽可能多的相关信息 - 如果您需要任何其他信息,请发表评论。

来自 GitHub Actions 的屏幕截图

build.sh

#!/bin/sh

# Import core db schema
./.deploy/postgres/restore-schema.sh

.deploy/postgres/restore-schema.sh

#!/bin/sh


docker exec -it postgres psql \
--username postgres \
--password dev \
coredb < .deploy/postgres/db-schema.sql

.github/workflows/test-api-gateway.yml

name: API Gateway CI

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master, develop ]

jobs:
  build:
    runs-on: ubuntu-latest
    services: # Serivce containers to run with `container-job`
      # Label used to access the service container
      postgres:
        # Docker Hub image
        image: postgres
        # Provide the password for postgres
        env:
          POSTGRES_USER: postgres
          POSTGRES_DB: coredb
          POSTGRES_PASSWORD: dev
        # Set health checks to wait until postgres has started
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5 
        ports:
          - 5432:5432

    strategy:
      matrix:
        node-version: [14.x]

    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}
    - run: docker ps
    - run: chmod +x build.sh .deploy/postgres/restore-schema.sh
    - run: npm ci
    - run: npm run build --if-present
    - run: npm test

【问题讨论】:

    标签: docker github-actions


    【解决方案1】:

    试试 --name 选项

    options: >-
      --health-cmd pg_isready
      --health-interval 10s
      --health-timeout 5s
      --health-retries 5
      --name postgres 
    

    https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idservices

    jobs..services.options:其他 Docker 容器资源选项。有关选项列表,请参阅“docker create options”。

    我见过的另一个解决方案是使用最后创建的容器

    docker exec -it $(docker ps --latest --quiet) bash
    

    【讨论】:

    • 我最终选择了一条不同的路线,基本上只有一个包装容器。两种解决方案都足够了,但我很欣赏这项研究。
    猜你喜欢
    • 1970-01-01
    • 2020-07-06
    • 2021-10-24
    • 2020-12-07
    • 2020-01-24
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    相关资源
    最近更新 更多