【问题标题】:Deploy Docker Image With Github Action使用 Github Action 部署 Docker 镜像
【发布时间】:2021-02-13 23:36:09
【问题描述】:

我有一个 Github Action,其中包括标记、构建和部署 docker 映像。 当有拉取请求时,我只需使用此文件进行构建工作:build.yml

# This is a basic workflow to help you get started with Actions

name: Build

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  pull_request:
    branches: [ master ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  docker:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    env:
      DOCKERHUB_REPOSITORY: ${{ secrets.DOCKERHUB_REPOSITORY }}

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      - name: docker build
        run: | # Tag the image with the commit hash
          docker build -t $DOCKERHUB_REPOSITORY .
          docker tag $DOCKERHUB_REPOSITORY:latest $DOCKERHUB_REPOSITORY:$(git log -1 --pretty=%h)

对于部署,我必须使用此文件构建和部署:deploy.yml

# This is a basic workflow to help you get started with Actions

name: Deploy

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    branches: [ master ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  docker:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    env:
      DOCKERHUB_REPOSITORY: ${{ secrets.DOCKERHUB_REPOSITORY }}

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      - name: docker login
        env:
          DOCKERHUB_USER: ${{ secrets.DOCKERHUB_USER }}
          DOCKERHUB_ACCESS_TOKEN: ${{ secrets.DOCKERHUB_ACCESS_TOKEN }}
        run: |
          docker login -u $DOCKERHUB_USER -p $DOCKERHUB_ACCESS_TOKEN

      - name: docker build
        run: | # Tag the image with the commit hash
          docker build -t $DOCKERHUB_REPOSITORY .
          docker tag $DOCKERHUB_REPOSITORY:latest $DOCKERHUB_REPOSITORY:$(git log -1 --pretty=%h)

      - name: docker push
        run: |
          docker push $DOCKERHUB_REPOSITORY

对我来说,构建部分有重复,但我没有发现如何在不同文件的作业中使用依赖项。它不工作。

我如何告诉 github 操作部署部分取决于构建?,有 2 个不同的文件。

链接:https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#creating-dependent-jobs

【问题讨论】:

    标签: docker github github-actions


    【解决方案1】:

    您可以将它们合并到一个 CI/CD 管道中。这应该由 master 中的 pushes 和 pull_requests 触发。这有几个优点;

    • 如果构建失败,管道将自动中止
      (绝对确定,您可以添加if: ${{ success() }}
    • 没有重复的步骤,docker build 只定义一次。
    • 仍然只能通过使用条件pushpull_request 上执行步骤:
    if: ${{ github.event_name == 'push' }} // OR
    if: ${{ github.event_name == 'pull_request' }}
    
    • 需要维护的管道更少!

    【讨论】:

      猜你喜欢
      • 2020-05-26
      • 2020-08-19
      • 2021-09-30
      • 1970-01-01
      • 2020-04-18
      • 2021-11-22
      • 2021-12-03
      • 1970-01-01
      • 2019-08-28
      相关资源
      最近更新 更多