【问题标题】:Run GitHub workflow on Docker image with a Dockerfile?使用 Dockerfile 在 Docker 映像上运行 GitHub 工作流?
【发布时间】:2022-08-10 01:45:43
【问题描述】:

我想在 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

附带问题:我在哪里可以找到有关此的文档?我发现的只是如何编写动作。

标签: docker continuous-integration github-actions


【解决方案1】:

如果你想使用容器来运行你的动作,你可以使用这样的东西:

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

Here is an example。

如果您想了解有关jobs.<job_id>.container 及其子字段的更多详细信息,可以查看the official documentation。

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

【讨论】:

    【解决方案2】:

    我将我的答案转发给another question,以确保在谷歌搜索时找到它。


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

    我建议按照 Github 文档创建自定义 build-and-publish-docker.yml 操作:Publishing Docker images。

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

    构建并发布映像后(基于先前创建的操作的 on 事件,也可以手动触发),您只需更新您的 tests.yml 操作,以便它使用自定义 Docker 映像。同样,这里有一个关于container 选项的非常好的文档页面:Running jobs in a container。

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

    • Dockerfile:要在 CI 上构建的 Docker 镜像
    • docker.yml: 构建 Docker 镜像的动作
    • lint.yml:使用构建的 Docker 镜像的操作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-15
      • 1970-01-01
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-05
      • 2014-08-15
      • 2021-12-05
      相关资源
      最近更新 更多