简体   繁体   English

在 GitHub 操作中使用本地 Dockerfile?

[英]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.

相关问题 是否可以在 Dockerfile 中使用 GitHub Action 输入? - Is it possible to use GitHub Action inputs inside the Dockerfile? 如何在我的 github 操作中指定 dockerfile 位置? - How do I specify the dockerfile location in my github action? 从 github 操作中的一组 dockerfiles 构建特定的 Dockerfile - Build Specific Dockerfile from set of dockerfiles in github action 在 Dockerfile 中使用 Github 机密不适用于 Github 操作 - Use Github secrets in Dockerfile does not work with Github Actions 如何在 github repo 中使用 jfrog artifactory 到 Dockerfile 的 war 文件 - How to use war file from jfrog artifactory to Dockerfile in github repo 在Dockerfile中从Github捆绑 - Bundling from Github in a Dockerfile Dockerfile:无法使用添加将本地文件映射到容器 - Dockerfile : not able to use add to map local file to container 如何在Dockerfile中使用本地文件或远程(有条件地)检索它? - How to use a local file or retrieve it remotely (conditionally) in Dockerfile? 如何使用本地图像作为 dockerfile 的基础图像? - How can I use a local image as the base image with a dockerfile? 如何在 GitHub Actions 中内置的 Dockerfile 中使用 github 令牌并尝试克隆私有存储库? - How to use github token in Dockerfile that is built in GitHub Actions and trying to clone private repository?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM