From 3ca35b99d9676d95db945cda37db426a5692e3ed Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Tue, 3 Dec 2024 14:42:18 +0800 Subject: [PATCH] new --- .github/workflows/docker-build.yml | 92 ++++++++++-------- .github/workflows/docker-new.yml | 151 +++++++++++++++++++++++++++++ 2 files changed, 200 insertions(+), 43 deletions(-) create mode 100644 .github/workflows/docker-new.yml diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml index e88b077..3d29acc 100644 --- a/.github/workflows/docker-build.yml +++ b/.github/workflows/docker-build.yml @@ -16,7 +16,9 @@ on: jobs: build-and-push: runs-on: ubuntu-latest - + strategy: + matrix: + image: [image1, image2, image3] steps: - name: Checkout repository uses: actions/checkout@v3 @@ -44,25 +46,25 @@ jobs: username: ${{ secrets.ALIREGISTRY_USERNAME }} password: ${{ secrets.ALIREGISTRY_TOKEN }} - - name: Extract version from tag - if: github.event_name == 'release' - id: extract_version_release - run: echo "version=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV + # - name: Extract version + # if: github.event_name == 'release' + # id: extract_version_release + # run: echo "version=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV - - name: Set version from workflow_dispatch input - if: github.event_name == 'workflow_dispatch' - id: extract_version_dispatch - run: echo "version=${{ github.event.inputs.tag }}" >> $GITHUB_ENV + # - name: Set version from workflow_dispatch input + # if: github.event_name == 'workflow_dispatch' + # id: extract_version_dispatch + # run: echo "version=${{ github.event.inputs.tag }}" >> $GITHUB_ENV - - name: Set version from branch name - if: github.event_name == 'push' - id: extract_version_push - run: | - if [[ "${GITHUB_REF}" == refs/heads/release-* ]]; then - BRANCH_NAME=${GITHUB_REF#refs/heads/} - VERSION=${BRANCH_NAME#release-} - echo "version=$VERSION" >> $GITHUB_ENV - fi + # - name: Set version from branch name + # if: github.event_name == 'push' + # id: extract_version_push + # run: | + # if [[ "${GITHUB_REF}" == refs/heads/release-* ]]; then + # BRANCH_NAME=${GITHUB_REF#refs/heads/} + # VERSION=${BRANCH_NAME#release-} + # echo "version=$VERSION" >> $GITHUB_ENV + # fi - name: Extract metadata for Docker (tags, labels) id: meta @@ -80,33 +82,37 @@ jobs: type=sha type=raw,value=${{ github.event.inputs.tag }} + - name: Display tags for debugging + run: echo "${{ steps.meta.outputs.tags }}" + - name: Build and push Docker images run: | ROOT_DIR="build/images" - for dir in "$ROOT_DIR"/*/; do - # Find Dockerfile or *.dockerfile in a case-insensitive manner - dockerfile=$(find "$dir" -maxdepth 1 -type f \( -iname 'dockerfile' -o -iname '*.dockerfile' \) | head -n 1) + IMAGE_NAME=${{ matrix.image }} + dir="$ROOT_DIR/$IMAGE_NAME" + + dockerfile=$(find "$dir" -maxdepth 1 -type f \( -iname 'dockerfile' -o -iname '*.dockerfile' \) | head -n 1) + + if [ -n "$dockerfile" ] && [ -f "$dockerfile" ]; then + echo "Building Docker image for $IMAGE_NAME with tags:" - if [ -n "$dockerfile" ] && [ -f "$dockerfile" ]; then - IMAGE_NAME=$(basename "$dir") - echo "Building Docker image for $IMAGE_NAME with tags:" - - # Initialize tag arguments - tag_args=() - - # Read each tag and append --tag arguments - while IFS= read -r tag; do - tag_args+=(--tag "${{ secrets.DOCKER_USERNAME }}/$IMAGE_NAME:$tag") - tag_args+=(--tag "ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME:$tag") - tag_args+=(--tag "registry.cn-hangzhou.aliyuncs.com/openimsdk/$IMAGE_NAME:$tag") - done <<< "${{ steps.meta.outputs.tags }}" - - # Build and push the Docker image with all tags - docker buildx build --platform linux/amd64,linux/arm64 \ - --file "$dockerfile" \ - "${tag_args[@]}" \ - --push "$dir" - else - echo "No valid Dockerfile found in $dir" + tag_args=() + + while IFS= read -r tag; do + tag_args+=(--tag "${{ secrets.DOCKER_USERNAME }}/$IMAGE_NAME:$tag") + tag_args+=(--tag "ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME:$tag") + tag_args+=(--tag "registry.cn-hangzhou.aliyuncs.com/openimsdk/$IMAGE_NAME:$tag") + done <<< "${{ steps.meta.outputs.tags }}" + + docker buildx build --platform linux/amd64,linux/arm64 \ + --file "$dockerfile" \ + "${tag_args[@]}" \ + --push "$dir" + + if [ $? -ne 0 ]; then + echo "Docker buildx build failed for $IMAGE_NAME" + exit 1 fi - done \ No newline at end of file + else + echo "No valid Dockerfile found in $dir" + fi \ No newline at end of file diff --git a/.github/workflows/docker-new.yml b/.github/workflows/docker-new.yml new file mode 100644 index 0000000..be57bfb --- /dev/null +++ b/.github/workflows/docker-new.yml @@ -0,0 +1,151 @@ +name: Build and release multiple Docker Images + +on: + push: + branches: + - release-* + release: + types: [published] + workflow_dispatch: + inputs: + tag: + description: "Tag version to be used for Docker image" + required: true + default: "v3.8.0" + +jobs: + find-images: + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.set-matrix.outputs.matrix }} + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Find Docker image directories + id: find + run: | + images=$(find build/images -mindepth 1 -maxdepth 1 -type d -printf "%f\n" | jq -R . | jq -s '{image: .}') + echo "matrix=$images" >> $GITHUB_OUTPUT + + - name: Set matrix + id: set-matrix + run: echo "matrix=${{ steps.find.outputs.matrix }}" >> $GITHUB_OUTPUT + + build-and-push: + needs: find-images + runs-on: ubuntu-latest + strategy: + matrix: ${{ fromJson(needs.find-images.outputs.matrix) }} + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Cache Docker layers + uses: actions/cache@v3 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- + + - name: Log in to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Log in to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Log in to Aliyun Container Registry + uses: docker/login-action@v2 + with: + registry: registry.cn-hangzhou.aliyuncs.com + username: ${{ secrets.ALIREGISTRY_USERNAME }} + password: ${{ secrets.ALIREGISTRY_TOKEN }} + + # - name: Extract version from tag + # if: github.event_name == 'release' + # id: extract_version_release + # run: echo "version=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV + + # - name: Set version from workflow_dispatch input + # if: github.event_name == 'workflow_dispatch' + # id: extract_version_dispatch + # run: echo "version=${{ github.event.inputs.tag }}" >> $GITHUB_ENV + + # - name: Set version from branch name + # if: github.event_name == 'push' + # id: extract_version_push + # run: | + # if [[ "${GITHUB_REF}" == refs/heads/release-* ]]; then + # BRANCH_NAME=${GITHUB_REF#refs/heads/} + # VERSION=${BRANCH_NAME#release-} + # echo "version=$VERSION" >> $GITHUB_ENV + # fi + + - name: Extract metadata for Docker (tags, labels) + id: meta + uses: docker/metadata-action@v5 + with: + tags: | + type=ref,event=tag + type=schedule + type=ref,event=branch + type=semver,pattern={{version}} + type=semver,pattern=v{{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + type=semver,pattern=release-{{raw}} + type=sha + type=raw,value=${{ github.event.inputs.tag }} + + - name: Display tags for debugging + run: echo "${{ steps.meta.outputs.tags }}" + + - name: Build and push Docker image + run: | + ROOT_DIR="build/images" + IMAGE_NAME=${{ matrix.image }} + dir="$ROOT_DIR/$IMAGE_NAME" + + dockerfile=$(find "$dir" -maxdepth 1 -type f \( -iname 'dockerfile' -o -iname '*.dockerfile' \) | head -n 1) + + if [ -n "$dockerfile" ] && [ -f "$dockerfile" ]; then + echo "Building Docker image for $IMAGE_NAME with tags:" + + tag_args=() + + while IFS= read -r tag; do + tag_args+=(--tag "${{ secrets.DOCKER_USERNAME }}/$IMAGE_NAME:$tag") + tag_args+=(--tag "ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME:$tag") + tag_args+=(--tag "registry.cn-hangzhou.aliyuncs.com/openimsdk/$IMAGE_NAME:$tag") + done <<< "${{ steps.meta.outputs.tags }}" + + docker buildx build --platform linux/amd64,linux/arm64 \ + --file "$dockerfile" \ + "${tag_args[@]}" \ + --cache-from type=local,src=/tmp/.buildx-cache \ + --cache-to type=local,dest=/tmp/.buildx-cache-new \ + --push "$dir" + + if [ $? -ne 0 ]; then + echo "Docker buildx build failed for $IMAGE_NAME" + exit 1 + fi + else + echo "No valid Dockerfile found in $dir" + fi + + - name: Update Docker cache + if: always() + run: | + mv /tmp/.buildx-cache-new /tmp/.buildx-cache \ No newline at end of file