Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One dir manage multi images . #346

Closed
HiColinn opened this issue Apr 17, 2021 · 2 comments
Closed

One dir manage multi images . #346

HiColinn opened this issue Apr 17, 2021 · 2 comments

Comments

@HiColinn
Copy link

Troubleshooting

This is not a BUG issue,but for a help.
I have problem that has bothering me for a long time,I am hoping to get some help!
The problem is I user one directory to manage all docker images,and when I push code to the github,
I will sign the image name(directory) to the git commit message.The build.yml will get the infomation from
the git commit message ,like if need build new image,or push to DockerHub. This way is effective on action V1,but when I update the build.yml to V2,It doesn't work.
______

name: Build Docker Image
on:
  push:
    branches:
        - master
jobs:
    build:
        # 命名工作
        name: Build docker image
        # 指定工作环境
        runs-on: ubuntu-latest
        if: "!contains(github.event.head_commit.message, '[skip ci]')"
        steps:
            -   name: Filter image name
                #获取提交日志中的变量并设置到env中
                run: |
                  ...
            # 显示变量值,方便调试
            -   name: Show var
                run: |
                    echo IMAGE_NAME=${{ env.IMAGE_NAME }}
                    echo IMAGE_RNAME=$IMAGE_RNAME
                    echo TAG_NAMES=$TAG_NAMES
            # 设置QEMU(多平台构建的时候需要)
            -   name: Set up QEMU
                uses: docker/setup-qemu-action@v1
            # 设置Buildx
            -   name: Set up Docker Buildx
                id: buildx
                uses: docker/setup-buildx-action@v1
            # 登录Dockerhub
            -   name: Login to DockerHub
                uses: docker/login-action@v1 
                with:
                    username: ${{ secrets.DOCKER_USERNAME }}
                    password: ${{ secrets.DOCKER_PASSWORD }}
            # 构建镜像并推送到Docker Hub
            -   name: Build and push
                if: ${{ env.IMAGE_NAME != '' }}
                id: docker_build
                uses: docker/build-push-action@v2
                with:
                    builder: ${{ steps.buildx.outputs.name }}
                    context: ./${{ env.IMAGE_NAME }}
                    file: ./${{ env.IMAGE_NAME }}/Dockerfile
                    target: prod
                    push: true
                    tags: ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:${{ env.TAG_NAMES }}
            # 显示构建的镜像的摘要
            -   name: Image digest
                run: echo "镜像摘要:"${{ steps.docker_build.outputs.digest }}      
            # 推送指定镜像的README到dockerhub
            -   name: Push Docker Hub Description
                # 当IMAGE_RNAME不为空的时候执行此任务 
                if: ${{ env.IMAGE_RNAME != ''}}
                uses: peter-evans/dockerhub-description@v2
                with:
                    username: ${{ secrets.DOCKERHUB_USERNAME }}
                    password: ${{ secrets.DOCKERHUB_PASSWORD }}
                    repository: ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_RNAME }}
                    readme-filepath: ${{ env.IMAGE_RNAME }}/README.md
@crazy-max
Copy link
Member

@HiColinn Would need logs and a link to your repo.

@crazy-max
Copy link
Member

In addition to the solution submitted by @CremaLuca (#365 (comment)), here is another using a matrix strategy:

name: Build Docker Image

on:
  push:
    branches:
      - master

jobs:
  build:
    name: Build docker image
    runs-on: ubuntu-latest
    if: "!contains(github.event.head_commit.message, '[skip ci]')"
    strategy:
      matrix:
        context:
          - alpine-base
          - alpine-full
    steps:
      -
        name: Checkout
        uses: actions/checkout@v2
      -
        name: Filter image name
        run: |
          ...
      -
        name: Show var
        run: |
          echo IMAGE_NAME=${{ env.IMAGE_NAME }}
          echo IMAGE_RNAME=$IMAGE_RNAME
          echo TAG_NAMES=$TAG_NAMES
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      -
        name: Login to DockerHub
        uses: docker/login-action@v1 
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      -
        name: Build and push
        if: ${{ env.IMAGE_NAME != '' }}
        id: docker_build
        uses: docker/build-push-action@v2
        with:
          context: ./${{ matrix.context }}
          target: prod
          push: true
          tags: ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:${{ env.TAG_NAMES }}
      -
        name: Push Docker Hub Description
        if: ${{ env.IMAGE_RNAME != ''}}
        uses: peter-evans/dockerhub-description@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}
          repository: ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_RNAME }}
          readme-filepath: ${{ env.IMAGE_RNAME }}/README.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants