如何在github动作服务容器中运行命令? [英] How can I run a command in github action service containers?

查看:161
本文介绍了如何在github动作服务容器中运行命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用minio创建s3,例如对象存储服务器和
,我想在ci cd进程中针对该服务器测试一些代码。

使用Github动作,我试图将minio作为服务添加到工作流文件中,但是由于minio需要命令和一些参数,因此我无法使用此机制实际运行它。

这是ci.yml相关配置的一部分:

I am using minio to create an s3 like object-store server and I want to test some code against this server during my ci cd process.
Using Github actions, I tried to add minio as service in the workflow file but since minio requires a command and some arguments I can't actually run it using this mechanism.
This is the part of the relevant configuration from my ci.yml:

minio-container:
runs-on: ubuntu-latest
container: python:3.8.2

services:
  minio:
    image: minio/minio:latest
    ports:
      - 9000:9000
    env:
      MINIO_ACCESS_KEY: XXXX
      MINIO_SECRET_KEY: XXXXX

我读了一点,弄清楚场景github运行 docker板条箱服务[OPTIONS] IMAGE_NAME ,但我还需要能够运行 docker create service [OPTIONS] IMAGE_NAME COMMAND [ARGS]

I read a little bit and figured out that behind the scene github runs the docker crate service [OPTIONS] IMAGE_NAME but I need to also be able to run docker create service [OPTIONS] IMAGE_NAME COMMAND [ARGS]

如果这不是暗示记住了,还有什么我可以尝试的选择?

In case this is not implemented yet what are other options I can try?

谢谢,

或者

Thanks,
Or

推荐答案

仔细检查,有一种方法。但是在开始之前,我尝试了一些想法。
首先,我想我可以将带有源代码的目录装入容器
中,并通过指定-entrypoint 选项,
,但服务在 git clone 之前启动。
然后我想也许可以将命令传递给容器,但是没有,那是不可能的。
我考虑的第三个选项是通过环境
变量将命令传递给映像随附的某些可执行文件(假定是外壳程序)。
但是,shell可以使用脚本路径,而不是命令路径( ENV 变量)。
然后我想,让服务终止,克隆存储库后,我只需要重新启动容器
,但是与……相比,这并没有带来什么好处。

On a closer inspection, there is a way. But before I came to it, I tried a couple of ideas. First I thought I can mount the directory with the source code into the container and run one of the project files (a script) by specifying --entrypoint option, but services are started before git clone. Then I thought that maybe I can pass a command to the container, but no, that is not possible. The third option I considered is passing a command via an environment variable to some executable that comes with the image, supposedly a shell. But shells can take a path to a script, not a command (ENV variable). Then I thought, "let the service die," I just need to restart the container after I clone the repository. But that brings nothing to the table compared to...

只需​​手动创建容器即可。我做了什么:

"just create the container by hand." Which is what I did:

.github / workflows / django.yml

...
jobs:
    build:
        runs-on: ubuntu-latest
        container: python:3.5-alpine3.12
        steps:
            - uses: actions/checkout@v2
            - run: apk add expect && unbuffer ./create-cypress-container.sh
...

create- cypress-container.sh

#!/bin/sh -eux
apk add docker jq
network=$(docker inspect --format '{{json .NetworkSettings.Networks}}' `hostname` \
  | jq -r 'keys[0]')
docker pull -q cypress/base:12
docker run \
  -v /home/runner/work:/__w \
  -w "$GITHUB_WORKSPACE" \
  --name cypress \
  --network "$network" \
  -d \
  cypress/base:12 sh -xc 'ls && whoami && pwd'
sleep 10
docker ps
docker logs cypress

作业容器是从以下选项开始(请参阅初始化容器>启动作业容器):

The job container is started with the following options (see Initialize containers > Starting job containeir):

...
--workdir /__w/PROJECT_NAME/PROJECT_NAME
-v "/home/runner/work":"/__w"
...

和环境变量:

...
GITHUB_WORKSPACE='/__w/PROJECT_NAME/PROJECT_NAME'
...

/ __ w / PROJECT_NAME / PROJECT_NAME 是克隆存储库的位置。

/__w/PROJECT_NAME/PROJECT_NAME is where your repository is cloned.

PS话虽如此,我将在单独的作业中运行前端和后端测试,
应该可以简化事务,并可以消除手动启动容器的需要。

P.S. Having that said, I'm going to run front end and back end tests in separate jobs, which should simplify matters and might eliminate the need to manually start containers.

这篇关于如何在github动作服务容器中运行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆