使用 Dockerfile 在 Docker 映像上运行 GitHub 工作流程?

发布于 2025-01-10 11:42:11 字数 1013 浏览 0 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻风 2025-01-17 11:42:11

如果您想使用容器来运行您的操作,您可以使用如下内容:

jobs:
  build:
      runs-on: ubuntu-latest
      container:
        image: docker://{host}/{image}:{tag}
      steps:
        ...

这是一个示例

如果您想了解有关 jobs..container 及其子字段的更多详细信息,您可以查看 官方文档

请注意,您还可以在步骤级别使用 docker 映像: 示例

If you want to use a container to run your actions, you can use something like this:

jobs:
  build:
      runs-on: ubuntu-latest
      container:
        image: docker://{host}/{image}:{tag}
      steps:
        ...

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.

幸福不弃 2025-01-17 11:42:11

我正在将我的答案重新发布到另一个问题 ,以便确保在 Google 搜索时找到它。


最好的解决方案是基于您的 Dockerfile 构建、发布和重用 Docker 映像。

我建议按照 Github 文档创建自定义 build-and-publish-docker.yml 操作:发布 Docker 镜像

假设您的存储库是公共的,您应该能够自动将图像上传到 ghcr.io ,而无需任何必要的配置。作为替代方案,也可以将映像发布到 Docker Hub。

构建并发布图像后(基于之前创建的操作的 on 事件,也可以手动触发),您只需更新您的 main.yml操作,以便它使用自定义 Docker 映像。同样,这里有一个关于 container 选项的非常好的文档页面:在容器中运行作业

作为示例,我正在分享我在个人存储库中使用的内容:

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 your main.yml action so it uses the custom Docker image. Again, here is a pretty good documentation page about the container 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 CI
  • docker.yml: the action to build the Docker image
  • lint.yml: the action using the built Docker image
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文