如何构建和推送Docker映像? [英] How to build and push a Docker image?
问题描述
我正在尝试在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
# ...