使用 Dockerfile 在 Docker 映像上运行 GitHub 工作流程?
我想在 Docker 镜像上运行我的 CI。我应该如何编写我的 .github/workflow/main.yml
?
name: CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
name: build
runs:
using: 'docker'
image: '.devcontainer/Dockerfile'
steps:
- uses: actions/checkout@v2
- name: Build
run: make
我收到错误:
The workflow is not valid. .github/workflows/main.yml
(Line: 11, Col: 5): Unexpected value 'runs'
我设法让它工作,但有一个丑陋的解决方法:
build:
name: Build Project
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v1
- name: Build docker images
run: >
docker build . -t foobar
-f .devcontainer/Dockerfile
- name: Build exam
run: >
docker run -v
$GITHUB_WORKSPACE:/srv
-w/srv foobar make
附带问题:我在哪里可以找到有关此问题的文档?我发现的只是如何编写动作。
I would like to run my CI on a Docker image. How should I write my .github/workflow/main.yml
?
name: CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
name: build
runs:
using: 'docker'
image: '.devcontainer/Dockerfile'
steps:
- uses: actions/checkout@v2
- name: Build
run: make
I get the error:
The workflow is not valid. .github/workflows/main.yml
(Line: 11, Col: 5): Unexpected value 'runs'
I managed to make it work but with an ugly workaround:
build:
name: Build Project
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v1
- name: Build docker images
run: >
docker build . -t foobar
-f .devcontainer/Dockerfile
- name: Build exam
run: >
docker run -v
$GITHUB_WORKSPACE:/srv
-w/srv foobar make
Side question: where can I find the documentation about this? All I found is how to write actions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想使用容器来运行您的操作,您可以使用如下内容:
这是一个示例。
如果您想了解有关
jobs..container
及其子字段的更多详细信息,您可以查看 官方文档。请注意,您还可以在步骤级别使用 docker 映像: 示例。
If you want to use a container to run your actions, you can use something like this:
Here is an example.
If you want more details about the
jobs.<job_id>.container
and its sub-fields, you can check the official documentation.Note that you can also use docker images at the step level: Example.
我正在将我的答案重新发布到另一个问题 ,以便确保在 Google 搜索时找到它。
最好的解决方案是基于您的
Dockerfile
构建、发布和重用 Docker 映像。我建议按照 Github 文档创建自定义
build-and-publish-docker.yml
操作:发布 Docker 镜像。假设您的存储库是公共的,您应该能够自动将图像上传到 ghcr.io ,而无需任何必要的配置。作为替代方案,也可以将映像发布到 Docker Hub。
构建并发布图像后(基于之前创建的操作的
on
事件,也可以手动触发),您只需更新您的main.yml
操作,以便它使用自定义 Docker 映像。同样,这里有一个关于container
选项的非常好的文档页面:在容器中运行作业。作为示例,我正在分享我在个人存储库中使用的内容:
Dockerfile
:要构建的 Docker 镜像CIdocker.yml
:构建 Docker 镜像的操作lint.yml
:使用构建的 Docker 镜像的操作I am reposting my answer to another question, in order to be sure to find it while Googling it.
The best solution is to build, publish and re-use a Docker image based on your
Dockerfile
.I would advise to create a custom
build-and-publish-docker.yml
action following the Github documentation: Publishing Docker images.Assuming your repository is public, you should be able to automatically upload your image to
ghcr.io
without any required configuration. As an alternative, it's also possible to publish the image to Docker Hub.Once your image is built and published (based on the
on
event of the action previously created, which can be triggered manually also), you just need to update yourmain.yml
action so it uses the custom Docker image. Again, here is a pretty good documentation page about thecontainer
option: Running jobs in a container.As an example, I'm sharing what I used in a personal repository:
Dockerfile
: the Docker image to be built on CIdocker.yml
: the action to build the Docker imagelint.yml
: the action using the built Docker image