如何构建和推送Docker映像? [英] How to build and push a Docker image?

查看:52
本文介绍了如何构建和推送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 

推荐答案

每个

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
  # ...

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

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:

  1. build 作业重命名为 test ;或
  2. 将依赖项更改为"需要:构建".
  1. Rename the build job to test; or
  2. 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映像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆