注: GitHub 托管的运行器目前在 GitHub Enterprise Server 上不受支持。 您可以在 GitHub 公共路线图 上查看有关未来支持计划的更多信息。
概览
Use jobs.<job_id>.container to create a container to run any steps in a job that don't already specify a container. 如有步骤同时使用脚本和容器操作,则容器操作将运行为同一网络上使用相同卷挂载的同级容器。
若不设置 container,所有步骤将直接在 runs-on 指定的主机上运行,除非步骤引用已配置为在容器中运行的操作。
Example: Running a job within a container
name: CI
on:
push:
branches: [ main ]
jobs:
container-test-job:
runs-on: ubuntu-latest
container:
image: node:14.16
env:
NODE_ENV: development
ports:
- 80
volumes:
- my_docker_volume:/volume_mount
options: --cpus 1
steps:
- name: Check for dockerenv file
run: (ls /.dockerenv && echo Found dockerenv) || (echo No dockerenv)只指定容器� 像时,可以忽略 image 关键词。
jobs:
container-test-job:
runs-on: ubuntu-latest
container: node:14.16
定义容器� 像
Use jobs.<job_id>.container.image to define the Docker image to use as the container to run the action. The value can be the Docker Hub image name or a registry name.
定义容器注册表的凭据
如果� 像的容器注册表需要身份验证才能拉取� 像,可以使用 jobs.<job_id>.container.credentials 设置 username 和 password 的 map。 凭据与您提供给 Docker 登录 命令的值相同。
Example: Defining credentials for a container registry
container:
image: ghcr.io/owner/image
credentials:
username: ${{ github.actor }}
password: ${{ secrets.github_token }}
将环境变量与容器一起使用
Use jobs.<job_id>.container.env to set a map of environment variables in the container.
公开容器上的网络端口
Use jobs.<job_id>.container.ports to set an array of ports to expose on the container.
在容器中装载卷
Use jobs.<job_id>.container.volumes to set an array of volumes for the container to use. 您可以使用卷分享作业中服务或其他步骤之间的数据。 可以指定命名的 Docker 卷、匿名的 Docker 卷或主机上的绑定挂载。
要指定卷,需指定来源和目� �路径:
<source>:<destinationPath>.
<source> 是主机上的卷名称或绝对路径,<destinationPath> 是容器中的绝对路径。
Example: Mounting volumes in a container
volumes:
- my_docker_volume:/volume_mount
- /data/my_data
- /source/directory:/destination/directory
设置容器资源选项
Use jobs.<job_id>.container.options to configure additional Docker container resource options. 有关选项列表,请参阅“docker create options”。
警告:不支持 --network 选项。