问题描述
我正在使用自托管 github runners 来 vpn 访问某些软件,我正在尝试在自托管 runners 上使用 dockerized github 操作,但我遇到了问题,因为我需要在 github 操作时指定 --network 主机标志运行 docker run。有没有办法让github action使用宿主机的网络?
解决方法
据我所知,这是不可能的。它也不适用于步骤。不过,jobs 上提供了选项。唯一的另一种方法是创建一个复合操作并直接在其中运行 docker run ...
。这是我为自己的工作流程编写的一个。它稍微复杂一些,但它允许您根据变量名称前缀自动将环境变量从运行器传递到 docker 容器:
name: Docker start container
description: Start a detached container
inputs:
image:
description: The image to use
required: true
name:
description: The container name
required: true
options:
description: Additional options to pass to docker run
required: false
default: ''
command:
description: The command to run
required: false
default: ''
env_pattern:
description: The environment variable pattern to pass to the container
required: false
default: ''
outputs:
cid:
description: Container ID
value: ${{ steps.info.outputs.cid }}
runs:
using: composite
steps:
- name: Run
shell: bash
run: >
variables='';
for i in $(env | grep '${{ inputs.env_pattern }}' | awk -F '=' '{print $1}'); do
variables="--env ${i} ${variables}";
done;
docker run -d
--name ${{ inputs.name }}
--network host
--cidfile ${{ inputs.name }}.cid
${variables}
${{ inputs.options }}
${{ inputs.image }}
${{ inputs.command }}
- name: Info
id: info
shell: bash
run: echo "::set-output name=cid::$(cat ${{ inputs.name }}.cid)"
并使用它:
- name: Start app container
uses: ./.github/actions/docker-start-container
with:
image: myapp/myapp:latest
name: myapp
env_pattern: 'MYAPP_'
options: --entrypoint entrypoint.sh
command: >
--check
-v