如何构建和推送Docker映像?

编程入门 行业动态 更新时间:2024-10-11 05:31:32
本文介绍了如何构建和推送Docker映像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在GitHub Actions中构建和推送Docker映像.

I'm trying to build and push a Docker image in GitHub Actions.

在YAML文件中,我还有其他步骤,可以正常工作.但是,当我尝试构建Docker映像时,GitHub Action失败.错误是:

In the YAML file I have other steps as well, which work fine. But when I tried to build a Docker image, the GitHub Action fails. The error is:

Invalid workflow file The workflow is not valid. Job package depends on unknown job test.

我在VS Code中安装了YAML扩展,它没有显示与缩进相关的错误.如果我删除了Docker build part的代码段(在arg命令之后),则动作测试成功运行.

I have a YAML extension installed in VS Code and it shows no errors related to indentation. If I remove the snippet of Docker build part (after arg command), action test runs successfully.

GitHub Action错误不能正确描述操作失败的原因,以便我进行调试.

The GitHub Action error doesn't describe the reason of action fail properly so that I could debug.

name: Netlify workflow on: push: branches: [master, develop] pull_request: branches: [master, develop] jobs: build: runs-on: ubuntu-latest strategy: matrix: node: [10.x, 12.x] steps: - name: Setup node uses: actions/setup-node@v1 with: node-version: ${{matrix.node}} - name: Checkout uses: actions/checkout@v2 - name: Setup cache uses: actions/cache@v1 with: path: ~/.npm key: ${{runner.os}}-modules-${{hashFiles('**/package-lock.json') }} restore-keys: | ${{runner.os}}-modules- ${{runner.os}}- - name: Install run: npm ci - name: Lint run: npm run lint - name: Build run: npm run build - name: Deploy uses: netlify/actions/cli@master env: NETLIFY_SITE_ID: ${{secrets.NETLIFY_SITE_ID}} NETLIFY_AUTH_TOKEN: ${{secrets.NETLIFY_AUTH_TOKEN}} with: args: deploy --dir=build --prod package: runs-on: ubuntu-latest needs: test steps: - name: Checkout code uses: actions/checkout@v2 - name: Build docker image run: docker builder build -t dockerHubUsername/repoName:latest . - name: Login to docker hub run: docker login --username ${{ secrets.DOCKER_USERNAME }} --password ${{ secrets.DOCKER_PASSWORD }} - name: Push docker image to docker hub run: docker push dockerHubUsername/repoName:latest

推荐答案

每个 jobs.< job_id> 是在其中的地图:

The jobs map in a GitHub Workflow, per jobs.<job_id>, is a map where:

键 job_id 是一个字符串,其值是作业的映射配置数据.

The key job_id is a string and its value is a map of the job's configuration data.

从YAML中剥离所有其他内容,以专注于该地图:

Stripping all of the other content out of the YAML to focus on that map:

jobs: build: # ... package: # ...

在顶层已​​定义了两个作业,其ID为 build 和 package .现在,让我们看一下这些作业的某些内容:

At the top level, two jobs have been defined, with the IDs build and package. Now let's look at some of the content of those jobs:

jobs: build: runs-on: ubuntu-latest # ... package: runs-on: ubuntu-latest needs: test # ...

每 job.< job_id> ;.需要 ,需要配置:

标识在此作业之前必须成功完成的所有作业会跑.它可以是一个字符串或字符串数​​组.

Identifies any jobs that must complete successfully before this job will run. It can be a string or array of strings.

尽管没有明确说明,但该示例显示作业由其ID标识,因此它必须是与定义的作业ID相对应的字符串或字符串数​​组.

Although it's not stated explicitly, the example shows that the jobs are identified by their IDs, so it needs to be a string or array of strings corresponding with defined job IDs.

在这里我们已经说过,要使用ID为 package 的代码运行作业,它需要"ID为 test 的作业已成功完成.但是,我们定义的唯一其他作业的ID是 build ,因此出现错误:

Here we've said that, to run the job with ID package, it "needs" the job with ID test to have successfully completed. The ID of the only other job we've defined is build, though, hence the error:

Job package depends on unknown job test. // ^~~~~~~ ^~~~~~~~~~ ^~~~ // job_id "needs" job_id

鉴于您只有两项工作,并且要做可能希望第二项依赖第一项,您要么需要:

Given that you have only two jobs and likely do want the second to depend on the first, you either need to:

  • 将 build 作业重命名为 test ;或
  • 将依赖项更改为"需要:构建".
  • Rename the build job to test; or
  • Change the dependency to needs: build.
  • 无论哪种方式,这两个ID都需要相对应,以使其成为一个语义有效的工作流(即使它已经在语法上有效的YAML).另一种选择是通过删除 needs:test 行来完全删除依赖项,尽管随后 build 和 package 将并行运行(工人允许).

    Either way, the two IDs need to correspond for this to be a semantically valid workflow (even though it's already syntactically valid YAML). An alternative would be to remove the dependency entirely, by deleting the needs: test line, although then build and package would be run in parallel (workers permitting).

    更多推荐

    如何构建和推送Docker映像?

    本文发布于:2023-10-27 04:26:35,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1532310.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:映像   Docker

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!