[英]Use local Dockerfile in a GitHub action?
I would like to setup a GitHub Action to build my project and run tests locally.我想设置一个 GitHub Action 来构建我的项目并在本地运行测试。 As I use
.devcontainer
I have a Dockerfile
in .devcontainer/Dockerfile
that provide everything I need to build my project.当我使用
.devcontainer
,我在.devcontainer/Dockerfile
中有一个Dockerfile
,它提供了构建项目所需的一切。
Now I would like to write a GitHub Action to build the project on every push.现在我想写一个 GitHub Action 来在每次推送时构建项目。 Locally I would have done this:
在本地我会这样做:
docker build -t local - < .devcontainer/Dockerfile
docker run -it -v $(pwd):/srv -w/srv local make test
GitHub actions looks cumbersome, but I eventually wrote this: GitHub 动作看起来很麻烦,但我最终写了这个:
on: push
jobs:
build:
name: build
runs-on: ubuntu-latest
steps:
- name: check out repository
uses: actions/checkout@v2
- name: build project
container:
image: ".devcontainer/Dockerfile"
volumes:
- .:/srv
run: make test
Unfortunately it does not like the container
keyword.不幸的是,它不喜欢
container
关键字。
Any clues?有什么线索吗?
The container key is designed for running publicly available dockerized actions, and is available under the job.<job_id>
key, not the steps
key.容器密钥设计用于运行公开可用的 dockerized 操作,可在
job.<job_id>
密钥下使用,而不是steps
密钥。 You do not need it to accomplish your task.你不需要它来完成你的任务。
The GitHub runners have an extensive list of software installed already, including docker and docker compose. GitHub 跑步者已经安装了大量软件,包括 docker 和 docker 组合。
You can easily run the same commands you run locally in your GitHub workflow.您可以轻松运行在 GitHub 工作流程中本地运行的相同命令。
steps:
- name: Check out code
uses: actions/checkout@v2
- name: Build docker images
run: docker build -t local < .devcontainer/Dockerfile
- name: Run tests
run: docker run -it -v $PWD:/srv -w/srv local make test
I am running most of my workflows like this, only using docker-compose, so you end up with cleaner workflows:我的大部分工作流程都是这样运行的,只使用 docker-compose,所以你最终会得到更干净的工作流程:
steps:
- name: Check out code
uses: actions/checkout@v2
- name: Build docker images
run: docker-compose build
- name: Setup database
run: docker-compose run setup
- name: Run tests
run: docker-compose run test
The best solution is to build, publish and re-use a Docker image based on your Dockerfile
.最好的解决方案是基于您的
Dockerfile
构建、发布和重用 Docker 映像。
I would advise to create a custom build-and-publish-docker.yml
action following the Github documentation: Publishing Docker images .我建议按照 Github 文档创建自定义
build-and-publish-docker.yml
操作: 发布 Docker 图像。
Assuming your repository is public, you should be able to automatically upload your image to ghcr.io
without any required configuration.假设您的存储库是公开的,您应该能够自动将您的图像上传到
ghcr.io
而无需任何配置。 As an alternative, it's also possible to publish the image to Docker Hub.作为替代方案,也可以将映像发布到 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 your tests.yml
action so it uses the custom Docker image.构建并发布图像后(基于先前创建的操作的
on
事件,也可以手动触发),您只需更新您的tests.yml
操作,使其使用自定义 Docker 图像。 Again, here is a pretty good documentation page about the container
option: Running jobs in a container .同样,这里有一个关于
container
选项的非常好的文档页面:在容器中运行作业。
As an example, I'm sharing what I used in a personal repository:例如,我正在分享我在个人存储库中使用的内容:
Dockerfile
: the Docker image to be built on CI Dockerfile
:要在 CI 上构建的 Docker 映像docker.yml
: the action to build the Docker image docker.yml
:构建 Docker 映像的操作lint.yml
: the action using the built Docker image lint.yml
:使用内置 Docker 图像的操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.