ci: PLATE-837: Change Docker build to common approach #1
Workflow file for this run
This file contains 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: "CI/CD Pipeline" | |
on: | |
push: | |
branches: | |
- master | |
pull_request: | |
types: | |
- opened | |
- synchronize | |
- reopened | |
- ready_for_review | |
branches: | |
- master | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }} | |
jobs: | |
changed_files: | |
name: "Changed files" | |
runs-on: ubuntu-latest | |
outputs: | |
changes: ${{ steps.changes.outputs.changes }} | |
pretty_branch_name: ${{ steps.version.outputs.pretty_branch_name }} | |
image_version: ${{ steps.version.outputs.image_version }} | |
steps: | |
- uses: hmarr/[email protected] | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
ref: ${{ github.ref }} | |
- uses: dorny/paths-filter@v2 | |
id: changes | |
with: | |
filters: | | |
segment_anything_model: | |
- 'label_studio_ml/examples/segment_anything_model/**' | |
- '.github/workflows/cicd_pipeline.yml' | |
llm_interactive: | |
- 'label_studio_ml/examples/llm_interactive/**' | |
- '.github/workflows/cicd_pipeline.yml' | |
the_simplest_backend: | |
- 'label_studio_ml/examples/the_simplest_backend/**' | |
- '.github/workflows/cicd_pipeline.yml' | |
- name: Calculate version | |
id: version | |
env: | |
BRANCH_NAME: ${{ github.event.pull_request.head.ref || github.ref_name }} | |
run: | | |
set -x | |
pretty_branch_name="$(echo -n "${BRANCH_NAME#refs/heads/}" | sed 's#/#-#g' | sed 's#_#-#g'| sed 's#\.#-#g' | tr '[:upper:]' '[:lower:]')" | |
echo "pretty_branch_name=$pretty_branch_name" >> $GITHUB_OUTPUT | |
current_time="$(date +'%Y%m%d.%H%M%S')" | |
branch="-${pretty_branch_name}" | |
short_sha="$(git rev-parse --short HEAD)" | |
long_sha="$(git rev-parse HEAD)" | |
echo "sha=$long_sha" >> $GITHUB_OUTPUT | |
short_sha_length="$(echo $short_sha | awk '{print length}')" | |
current_time_length="$(echo $current_time | awk '{print length}')" | |
version="${current_time}$(echo $branch | cut -c1-$((50 - short_sha_length - current_time_length)))-${short_sha}" | |
echo "image_version=$version" >> $GITHUB_OUTPUT | |
build: | |
name: "Build" | |
runs-on: ubuntu-latest | |
needs: | |
- changed_files | |
timeout-minutes: 30 | |
strategy: | |
fail-fast: false | |
matrix: | |
image: ${{ fromJSON(needs.changed_files.outputs.changes) }} | |
env: | |
examples_dir: "label_studio_ml/examples" | |
steps: | |
- uses: hmarr/[email protected] | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
ref: ${{ github.sha }} | |
- name: Set up Docker Buildx | |
uses: docker/[email protected] | |
- name: Login to DockerHub | |
uses: docker/[email protected] | |
with: | |
username: ${{ vars.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Calculate Docker Context | |
id: calculate-docker-context | |
uses: actions/github-script@v7 | |
env: | |
NAME: "${{ matrix.image }}" | |
IMAGE_NAME: "${{ vars.DOCKERHUB_ORG }}/label-studio-ml-backend" | |
TAGS: "${{ needs.changed_files.outputs.image_version }},${{ needs.changed_files.outputs.pretty_branch_name }}" | |
TAG_PREFIX: "${{ matrix.image }}-" | |
CONTEXT: "${{ env.examples_dir }}/${{ matrix.image }}" | |
FILE: "${{ env.examples_dir }}/${{ matrix.image }}/Dockerfile" | |
with: | |
script: | | |
const name = process.env.NAME; | |
const raw_tags_input = process.env.TAGS; | |
const image_name = process.env.IMAGE_NAME; | |
let properties = { | |
"context": process.env.CONTEXT, | |
"file": process.env.FILE, | |
"tag_prefix": process.env.TAG_PREFIX, | |
}; | |
const overrides = { | |
"segment_anything_model": { | |
"tag_prefix": "sam-", | |
}, | |
"llm_interactive": { | |
"tag_prefix": "llm-", | |
}, | |
"the_simplest_backend": { | |
"tag_prefix": "simplebackend-", | |
}, | |
} | |
const override = overrides[name] || {}; | |
properties = { ...properties, ...override }; | |
const tags = raw_tags_input | |
.split(',') | |
.map(x => x.trim()) | |
.map(x => `${image_name}:${properties.tag_prefix}${x}`) | |
.join(','); | |
core.notice(`tags='${tags}'`) | |
core.setOutput("tags", tags); | |
core.setOutput("context", properties.context); | |
core.setOutput("file", properties.file); | |
- name: Push Docker image | |
uses: docker/[email protected] | |
with: | |
context: "${{ steps.calculate-docker-context.outputs.context }}" | |
file: "${{ steps.calculate-docker-context.outputs.file }}" | |
push: true | |
tags: "${{ steps.calculate-docker-context.outputs.tags }}" | |
cache-from: type=gha | |
cache-to: type=gha,mode=max |