This is an automated email from the ASF dual-hosted git repository.

klesh pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/main by this push:
     new 6ef33f792 Adjust ci to build image on main updated (#6095)
6ef33f792 is described below

commit 6ef33f792bf057f6ede64e65d7d9c39c400a8424
Author: ZhangNing10 <[email protected]>
AuthorDate: Tue Sep 19 11:11:05 2023 +0800

    Adjust ci to build image on main updated (#6095)
    
    * adjust ci to build image on pr merged into main or release branches
    
    * remove if condition on get-pr-number job
    
    * add outputs
    
    * adjust ci
    
    * add if condition for build image ci
    
    * remove if condition for build image ci
---
 .github/workflows/build.yml | 47 +++++++++++++++++++++++++++------------------
 1 file changed, 28 insertions(+), 19 deletions(-)

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 2f970c18b..61550d088 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -23,13 +23,12 @@ on:
   push:
     tags:
       - "v*"
-  pull_request:
-    types:
-      - closed
+    branches:
+      - main
+      - release-*
 
 jobs:
   build-and-push-builder:
-    if: ${{ (github.event.pull_request.merged == true) || (github.ref_type == 
'tag') }}
     name: Build and Push devlake builder
     runs-on: ubuntu-latest
     steps:
@@ -54,7 +53,6 @@ jobs:
           cache-from: ${{ secrets.DOCKERHUB_OWNER }}/devlake:amd64-builder
           cache-to: ${{ secrets.DOCKERHUB_OWNER }}/devlake:amd64-builder
   build-and-push-base:
-    if: ${{ (github.event.pull_request.merged == true) || (github.ref_type == 
'tag') }}
     name: Build and Push devlake base
     runs-on: ubuntu-latest
     steps:
@@ -79,7 +77,6 @@ jobs:
           cache-from: ${{ secrets.DOCKERHUB_OWNER }}/devlake:base
           cache-to: ${{ secrets.DOCKERHUB_OWNER }}/devlake:base
   build-devlake:
-    if: ${{ (github.event.pull_request.merged == true) || (github.ref_type == 
'tag') }}
     needs: build-and-push-builder
     name: Build and cache devlake
     runs-on: ubuntu-latest
@@ -117,9 +114,26 @@ jobs:
           build-args: |
             TAG=${{ github.ref_name }}
             SHA=${{ steps.get_short_sha.outputs.SHORT_SHA }}
+  get-pr-number:
+    name: Get pr number and build timestamp
+    runs-on: ubuntu-latest
+    outputs:
+      pull_request_number: ${{ steps.pr.outputs.pull_request_number }}
+      timestamp: ${{ steps.ts.outputs.timestamp }}
+    steps:
+      - uses: actions/checkout@v3
+        with:
+          fetch-depth: 0
+      - name: Get pull request number
+        id: pr
+        run: echo "pull_request_number=$(gh pr view --json number -q .number 
|| echo "")" >> $GITHUB_OUTPUT
+        env:
+          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+      - name: Get build timestamp
+        id: ts
+        run: echo "timestamp=$(date '+%y%m%d_%H%M')" >> $GITHUB_OUTPUT
   build-and-push-devlake:
-    if: ${{ (github.event.pull_request.merged == true) || (github.ref_type == 
'tag') }}
-    needs: [build-devlake, build-and-push-base]
+    needs: [build-devlake, build-and-push-base, get-pr-number]
     name: Build and Push devlake image
     runs-on: ubuntu-latest
     steps:
@@ -148,16 +162,12 @@ jobs:
         id: get_push_tags
         run: |
           image_name=${{ secrets.DOCKERHUB_OWNER }}/devlake
-          export DATE_TAG=$(date '+%y%m%d_%H%M')
-          export VALID_DOCKER_TAG=$(echo "${{ 
github.event.pull_request.head.ref }}" | sed 's/[^a-zA-Z0-9]/-/g')
           if printf ${{ github.ref }} |grep -Pq '^refs/tags/' && printf ${{ 
github.ref_name }} | grep -Pq '^v(\d+).(\d+).(\d+)$'; then
               echo "TAGS=${image_name}:latest,${image_name}:${{ 
github.ref_name }}" >> $GITHUB_OUTPUT
           elif printf ${{ github.ref }} |grep -Pq '^refs/tags/'; then
               echo "TAGS=${image_name}:${{ github.ref_name }}" >> 
$GITHUB_OUTPUT
-          elif printf ${{ github.ref_name }} | egrep -q '^main$|^release-.+$'; 
then
-              echo "TAGS=${image_name}:${{ github.ref_name }}_${DATE_TAG}_${{ 
github.sha }}" >> $GITHUB_OUTPUT
           else
-              echo "TAGS=${image_name}:MR-${{ github.event.pull_request.number 
}}_${VALID_DOCKER_TAG}_${DATE_TAG}_${{ github.sha }}" >> $GITHUB_OUTPUT
+              echo "TAGS=${image_name}:${{ github.ref_name }}_${{ 
needs.get-pr-number.outputs.timestamp }}_${{ 
steps.get_short_sha.outputs.SHORT_SHA }}_MR-${{ 
needs.get-pr-number.outputs.pull_request_number }}" >> $GITHUB_OUTPUT
           fi
       - name: Build and push lake image
         uses: docker/build-push-action@v3
@@ -193,7 +203,7 @@ jobs:
             }
 
   build-and-push-other-image:
-    if: ${{ (github.event.pull_request.merged == true) || (github.ref_type == 
'tag') }}
+    needs: [get-pr-number]
     name: Build and Push ${{ matrix.build.name }} image
     runs-on: ubuntu-latest
     strategy:
@@ -207,6 +217,9 @@ jobs:
             context: grafana
     steps:
       - uses: actions/checkout@v3
+      - name: Get short sha
+        id: get_short_sha
+        run: echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
       - name: Set up QEMU
         uses: docker/setup-qemu-action@v2
       - name: Set up Docker Buildx
@@ -220,16 +233,12 @@ jobs:
         id: get_push_tags
         run: |
           image_name=${{ secrets.DOCKERHUB_OWNER }}/${{ matrix.build.image }}
-          export DATE_TAG=$(date '+%y%m%d_%H%M')
-          export VALID_DOCKER_TAG=$(echo "${{ 
github.event.pull_request.head.ref }}" | sed 's/[^a-zA-Z0-9]/-/g')
           if printf ${{ github.ref }} |grep -Pq '^refs/tags/' && printf ${{ 
github.ref_name }} | grep -Pq '^v(\d+).(\d+).(\d+)$'; then
               echo "TAGS=${image_name}:latest,${image_name}:${{ 
github.ref_name }}" >> $GITHUB_OUTPUT
           elif printf ${{ github.ref }} |grep -Pq '^refs/tags/'; then
               echo "TAGS=${image_name}:${{ github.ref_name }}" >> 
$GITHUB_OUTPUT
-          elif printf ${{ github.ref_name }} | egrep -q '^main$|^release-.+$'; 
then
-              echo "TAGS=${image_name}:${{ github.ref_name }}_${DATE_TAG}_${{ 
github.sha }}" >> $GITHUB_OUTPUT
           else
-              echo "TAGS=${image_name}:MR-${{ github.event.pull_request.number 
}}_${VALID_DOCKER_TAG}_${DATE_TAG}_${{ github.sha }}" >> $GITHUB_OUTPUT
+              echo "TAGS=${image_name}:${{ github.ref_name }}_${{ 
needs.get-pr-number.outputs.timestamp }}_${{ 
steps.get_short_sha.outputs.SHORT_SHA }}_MR-${{ 
needs.get-pr-number.outputs.pull_request_number }}" >> $GITHUB_OUTPUT
           fi
       - name: Build and push ${{ matrix.build.name }} image
         uses: docker/build-push-action@v3

Reply via email to