如何在一个Github Actions Docker中运行多个命令 [英] How to run multiple commands in one Github Actions Docker
本文介绍了如何在一个Github Actions Docker中运行多个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在一个action
中运行多个命令的正确方式是什么?
例如:
我想以action
的身份运行一个python脚本。在运行此脚本之前,我需要安装requirements.txt
。
我能想到几个选项:
- 使用其中的
RUN pip install -r requirements.txt
命令创建Dockerfile
。 - 使用
python:3
镜像,运行entrypoint.sh
文件中的pip install -r requirements.txt
,然后运行main.workflow
中的args
参数。 - 同时使用
pip install
和python myscript.py
作为args
另一个示例:
我要运行存储库中存在的脚本,然后比较两个文件(其输出和一个已存在的文件)。
这是一个包含两个命令的过程,而在第一个示例中,pip install
命令可以被视为构建命令而不是测试命令。
问题:
我是否可以为另一个命令创建另一个Docker,该命令将包含上一个Docker的输出?
我正在查找Dockerfile
、entrypoint
或args
中命令位置的指导原则。
推荐答案
您可以在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
的外壳脚本会返回一个``。但以bash myscript.sh -xe
的方式运行它的效果与预期一致。
如果要在停靠站计算机中运行此,可以在您身上运行如下选项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
结果将保存到一个文件,并将此文件复制到第二个阶段,该阶段可以对结果运行任何检查。
这篇关于如何在一个Github Actions Docker中运行多个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文