【问题标题】:How to run multiple commands in one Github Actions Docker如何在一个 Github Actions Docker 中运行多个命令
【发布时间】:2019-11-05 15:53:16
【问题描述】:

在一个action 中运行多个命令的正确方法是什么?

例如:

我想以action 的身份运行python 脚本。在运行此脚本之前,我需要安装 requirements.txt

我能想到几个选项:

  • 创建一个Dockerfile,其中包含命令RUN pip install -r requirements.txt
  • 使用python:3 映像,并在entrypoint.sh 文件中运行pip install -r requirements.txt,然后在main.workflow 中运行来自args 的参数。
  • 同时使用pip installpython myscript.py 作为args

另一个例子:

我想运行一个存在于我的存储库中的脚本,然后比较 2 个文件(它的输出和一个已经存在的文件)。

这是一个包含两个命令的过程,而在第一个示例中,pip install 命令可以被视为构建命令而不是测试命令。

问题:

我可以为另一个命令创建另一个 Docker,其中将包含以前 Docker 的输出吗?

我正在Dockerfileentrypointargs 中寻找有关命令位置的指南。

【问题讨论】:

    标签: docker dockerfile docker-entrypoint github-actions


    【解决方案1】:

    您可以在run 属性上使用管道| 运行多个命令。看看这个:

    name: My Workflow
    
    on: [push]
    
    jobs:
      runMultipleCommands:
        runs-on: ubuntu-latest
        steps:
         - uses: actions/checkout@v1
         - run: |
            echo "A initial message"
            pip install -r requirements.txt
            echo "Another message or command"
            python myscript.py
            bash some-shell-script-file.sh -xe
         - run: echo "One last message"
    

    在我的测试中,运行像 ./myscript.sh 这样的 shell 脚本会返回一个 ``.但是像bash myscript.sh -xe 一样运行它的效果与预期一样。

    My workflow file | Results

    如果你想在 docker 机器内运行它,可以在run 子句上运行类似这样的选项:

    docker exec -it pseudoName /bin/bash -c "cd myproject; pip install -r requirements.txt;"
    

    关于“为另一个命令创建另一个 Docker,它将包含以前的 Docker 的输出”,你可以在你的 dockerfile 上使用multistage-builds。有些人喜欢:

    ## First stage (named "builder")
    ## Will run your command (using add git as sample) and store the result on "output" file
    FROM alpine:latest as builder
    RUN apk add git > ./output.log
    
    ## Second stage
    ## Will copy the "output" file from first stage
    FROM alpine:latest
    COPY --from=builder ./output.log .
    RUN cat output.log
    # RUN your checks
    CMD []
    

    这样apk add git 结果被保存到一个文件中,并且这个文件被复制到第二阶段,可以对结果进行任何检查。

    【讨论】:

    • 如果遇到奇怪的错误,请验证空格。删除它并使用制表符再次缩进。
    • 请注意,如果您为同一个 name 标题指定两次 run,则会收到错误消息:“工作流无效......‘运行’已定义”。虽然您的示例没有使用steps 下的name 键。
    猜你喜欢
    • 2023-02-21
    • 1970-01-01
    • 2021-03-20
    • 2021-07-17
    • 2021-06-17
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多