如何在一个 Github Actions Docker 中运行多个命令

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

例如:

我想运行一个 Python 脚本作为 action。在运行这个脚本之前,我需要安装 requirements.txt

我能想到几种选择:

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

另一个例子:

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

这是一个包含 两个命令的进程,而在第一个示例中,可以将 pip install命令视为 建筑指挥部,而不是一个测试命令。

问题是:

我是否可以为另一个命令创建另一个 Docker,它将包含前一个 Docker 的输出?

我正在寻找指导方针的位置指令在 Dockerfile,在 entrypoint或在 args

70080 次浏览

You can run multiple commands using a pipe | on the run attribute. Check this out:

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"

On my tests, running a shell script like ./myscript.sh returns a ``. But running it like bash myscript.sh -xe worked like expected.

My workflow file | Results

If you want to run this inside the docker machine, an option could be run some like this on you run clause:

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

Regard to the "create another Docker for another command, which will contain the output of the previous Docker", you could use multistage-builds on your dockerfile. Some like:

## 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 []

This way the apk add git result was saved to a file, and this file was copied to the second stage, that can run any check on the results.