fix: reduce SAM2 video backend resource usage (#924) #477
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: "Build and Push" | |
| on: | |
| push: | |
| branches: | |
| - master | |
| paths: | |
| - ".github/workflows/build.yml" | |
| - "label_studio_ml/examples/**" | |
| workflow_dispatch: | |
| env: | |
| DOCKER_BUILD_CONFIG_BRANCH: "master" | |
| DOCKER_BUILD_CONFIG_PATH: ".github/docker-build-config.yml" | |
| DOCKER_EXAMPLES_DIRECTORY: "label_studio_ml/examples" | |
| SLACK_NOTIFICATION_CHANNEL_ID: "CKL2D6D2P" | |
| jobs: | |
| build-image: | |
| name: Build image | |
| if: needs.calculate_matrix.outputs.matrix-include != '[]' | |
| needs: calculate_matrix | |
| runs-on: ${{ matrix.runs_on || 'ubuntu-latest' }} | |
| timeout-minutes: 45 | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| include: ${{ fromJson(needs.calculate_matrix.outputs.matrix-include) }} | |
| env: | |
| IMAGE_NAME: heartexlabs/label-studio-ml-backend | |
| backend_dir_name: ${{ matrix.backend_dir_name }} | |
| backend_tag_prefix: ${{ matrix.backend_tag_prefix }} | |
| steps: | |
| - uses: hmarr/debug-action@v3.0.0 | |
| - name: Check user's membership | |
| uses: actions/github-script@v8 | |
| id: actor-membership | |
| env: | |
| ACTOR: ${{ github.actor }} | |
| with: | |
| github-token: ${{ secrets.GIT_PAT }} | |
| script: | | |
| const { repo, owner } = context.repo; | |
| const actor = process.env.ACTOR; | |
| const { data: membership } = await github.rest.orgs.getMembershipForUser({ | |
| org: owner, | |
| username: actor, | |
| }); | |
| core.setOutput("state", membership.state); | |
| core.setOutput("active", membership.state == "active"); | |
| - uses: actions/checkout@v6 | |
| with: | |
| ref: "${{ env.GITHUB_SHA }}" | |
| fetch-depth: 0 | |
| - name: Calculate version | |
| id: version | |
| env: | |
| BRANCH_NAME: "${{ github.event.pull_request.head.ref || github.ref_name }}" | |
| PREFIX: "${{ env.backend_tag_prefix }}" | |
| run: | | |
| set -xueo pipefail | |
| MAX_TAG_LENGTH=50 | |
| pretty_branch_name="$(echo -n "${BRANCH_NAME#refs/heads/}" | sed -E 's#[/_\.-]+#-#g' | tr '[:upper:]' '[:lower:]' | cut -c1-25 | sed -E 's#-$##g')" | |
| echo "pretty_branch_name=${pretty_branch_name}" >> "${GITHUB_OUTPUT}" | |
| timestamp="$(date +'%Y%m%d.%H%M%S')" | |
| echo "timestamp=$timestamp" >> $GITHUB_OUTPUT | |
| short_sha="$(git rev-parse --short HEAD)" | |
| echo "short_sha=$short_sha" >> $GITHUB_OUTPUT | |
| long_sha="$(git rev-parse HEAD)" | |
| echo "sha=$long_sha" >> $GITHUB_OUTPUT | |
| short_sha_length="$(echo $short_sha | awk '{print length}')" | |
| timestamp_length="$(echo $timestamp | awk '{print length}')" | |
| prefix_length="$(echo $PREFIX | awk '{print length}')" | |
| short_branch="$(echo $pretty_branch_name | cut -c1-$((MAX_TAG_LENGTH - 2 - short_sha_length - timestamp_length - prefix_length)))" | |
| echo "short_branch=$short_branch" >> $GITHUB_OUTPUT | |
| image_version="${PREFIX}${timestamp}-${short_branch}-${short_sha}" | |
| echo "image_version=$image_version" >> $GITHUB_OUTPUT | |
| image_branch_version="${PREFIX}${short_branch}" | |
| echo "image_branch_version=$image_branch_version" >> $GITHUB_OUTPUT | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v4.0.0 | |
| - name: Login to DockerHub | |
| if: ${{ !github.event.pull_request.head.repo.fork }} | |
| uses: docker/login-action@v4.1.0 | |
| with: | |
| username: ${{ vars.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Calculate Docker tags | |
| id: calculate-docker-tags | |
| uses: actions/github-script@v8 | |
| env: | |
| IMAGE_NAME: "${{ env.IMAGE_NAME }}" | |
| TAGS: "${{ steps.version.outputs.image_version }},${{ steps.version.outputs.image_branch_version }}" | |
| with: | |
| script: | | |
| const raw_tags_input = process.env.TAGS; | |
| const image_name = process.env.IMAGE_NAME; | |
| const tags = raw_tags_input | |
| .split(',') | |
| .map(x => x.trim()) | |
| .map(x => `${image_name}:${x}`) | |
| .join(','); | |
| core.notice(`tags='${tags}'`) | |
| core.setOutput("tags", tags); | |
| - name: Push Docker image | |
| uses: docker/build-push-action@v7.0.0 | |
| id: docker_build_and_push | |
| env: | |
| DOCKER_BUILD_CHECKS_ANNOTATIONS: "false" | |
| DOCKER_BUILD_SUMMARY: "false" | |
| DOCKER_BUILD_RECORD_UPLOAD: "false" | |
| with: | |
| context: "${{ env.DOCKER_EXAMPLES_DIRECTORY }}/${{ env.backend_dir_name }}" | |
| push: ${{ steps.actor-membership.outputs.active }} | |
| tags: "${{ steps.calculate-docker-tags.outputs.tags }}" | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| calculate_matrix: | |
| name: "Calculate build matrix" | |
| runs-on: ubuntu-latest | |
| outputs: | |
| matrix-include: ${{ steps.matrix.outputs.matrix-include }} | |
| steps: | |
| - uses: hmarr/debug-action@v3.0.0 | |
| - run: npm install js-yaml | |
| - name: Build matrix | |
| id: matrix | |
| uses: actions/github-script@v8 | |
| env: | |
| BASE_REF: "${{ github.event.before }}" | |
| HEAD_REF: "${{ github.event.after || github.sha }}" | |
| EVENT_NAME: "${{ github.event_name }}" | |
| with: | |
| script: | | |
| const yaml = require('js-yaml'); | |
| const {owner, repo} = context.repo; | |
| const default_branch = process.env.DOCKER_BUILD_CONFIG_BRANCH; | |
| const docker_build_config_path = process.env.DOCKER_BUILD_CONFIG_PATH; | |
| const docker_examples_directory = process.env.DOCKER_EXAMPLES_DIRECTORY; | |
| const base_ref = process.env.BASE_REF; | |
| const head_ref = process.env.HEAD_REF; | |
| const event_name = process.env.EVENT_NAME; | |
| const docker_build_config_blob = await github.rest.repos.getContent({ | |
| owner: owner, | |
| repo: repo, | |
| ref: default_branch, | |
| path: docker_build_config_path, | |
| }); | |
| const docker_build_config_content = Buffer.from(docker_build_config_blob.data.content, docker_build_config_blob.data.encoding).toString("utf8"); | |
| const docker_build_config = yaml.load(docker_build_config_content); | |
| let backends; | |
| if (event_name === 'workflow_dispatch') { | |
| // Manual trigger: (re)build every configured backend. | |
| backends = new Set(docker_build_config.map(e => e.backend_dir_name)); | |
| } else { | |
| // Push: only build backends whose files changed in this push. | |
| const {data: compare} = await github.rest.repos.compareCommits({ | |
| owner, | |
| repo, | |
| base: base_ref, | |
| head: head_ref, | |
| }); | |
| backends = new Set(compare.files.filter(e => e.filename.startsWith(docker_examples_directory)).map(e => e.filename.split('/')[2])); | |
| } | |
| let matrixInclude = []; | |
| for (const backend of backends) { | |
| const config = docker_build_config.find(e => e.backend_dir_name === backend); | |
| if (!config) { | |
| console.log(`Could not find config for ${backend}, skipping`); | |
| continue; | |
| } | |
| matrixInclude.push({ | |
| "backend_dir_name": backend, | |
| "backend_tag_prefix": config.backend_tag_prefix, | |
| "runs_on": config.runs_on ?? "ubuntu-latest", | |
| }); | |
| } | |
| console.log(`Matrix calculation result:`); | |
| console.log(matrixInclude); | |
| core.setOutput("matrix-include", matrixInclude); | |
| notification: | |
| name: "Send Slack Notification" | |
| if: failure() | |
| runs-on: ubuntu-latest | |
| needs: | |
| - build-image | |
| steps: | |
| - name: Send Slack Notification | |
| if: always() | |
| uses: slackapi/slack-github-action@v2.1.1 | |
| env: | |
| WORKFLOW: "<https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}|workflow>" | |
| with: | |
| method: chat.postMessage | |
| token: ${{ secrets.SLACK_LSE_BOT_TOKEN }} | |
| payload: | | |
| channel: "${{ env.SLACK_NOTIFICATION_CHANNEL_ID }}" | |
| text: >+ | |
| :x: Build workflow failed | |
| [ ${{ env.WORKFLOW }} ] |