gpt4 book ai didi

docker - 使用Github Action部署Docker镜像

转载 作者:行者123 更新时间:2023-12-02 19:14:06 25 4
gpt4 key购买 nike

我有一个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 action,deploy部分取决于build,带有2个不同的文件。
链接: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#creating-dependent-jobs

最佳答案

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

  • 如果构建失败,管道将自动中止
    (可以肯定的是,您可以添加if: ${{ success() }}!
  • 没有重复的步骤,docker build仅定义一次。
  • 步骤仍然只能通过使用条件在pushpull_request上执行:
  • if: ${{ github.event_name == 'push' }} // OR
    if: ${{ github.event_name == 'pull_request' }}
  • 更少的管道维护!
  • 关于docker - 使用Github Action部署Docker镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64626463/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com