ci: Docs preview #239
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: "Build and Push" | |
on: | |
push: | |
branches: | |
- master | |
jobs: | |
build-image: | |
name: Build image | |
needs: calculate_matrix | |
runs-on: ubuntu-latest | |
timeout-minutes: 30 | |
strategy: | |
fail-fast: false | |
matrix: ${{fromJSON(needs.calculate_matrix.outputs.matrix)}} | |
env: | |
IMAGE_NAME: heartexlabs/label-studio-ml-backend | |
examples_dir: label_studio_ml/examples | |
backend_dir_name: ${{ matrix.ml_backend.backend_dir_name }} | |
backend_tag_prefix: ${{ matrix.ml_backend.backend_tag_prefix }} | |
steps: | |
- uses: actions/checkout@v4 | |
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 's#/#-#g' | sed 's#_#-#g'| sed 's#\.#-#g' | tr '[:upper:]' '[:lower:]')" | |
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/[email protected] | |
- name: Login to DockerHub | |
if: ${{ !github.event.pull_request.head.repo.fork }} | |
uses: docker/[email protected] | |
with: | |
username: ${{ vars.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Calculate Docker tags | |
id: calculate-docker-tags | |
uses: actions/github-script@v7 | |
env: | |
IMAGE_NAME: "${{ env.IMAGE_NAME }}" | |
TAGS: "${{ steps.version.outputs.image_version }},${{ steps.version.outputs.image_branch_version }},latest" | |
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/[email protected] | |
id: docker_build_and_push | |
with: | |
context: "${{ env.examples_dir }}/${{ env.backend_dir_name }}" | |
push: true | |
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: ${{ steps.set-matrix.outputs.matrix }} | |
steps: | |
- uses: hmarr/[email protected] | |
- name: Extract branch name on direct push to a branch | |
if: github.event_name == 'push' | |
run: | | |
echo "BASE_SHA=${{ github.event.before }}" >> $GITHUB_ENV | |
- name: Extract branch name on 'pull_request' | |
if: github.event_name == 'pull_request' | |
run: | | |
echo "BASE_SHA=${{ github.event.pull_request.base.sha }}" >> $GITHUB_ENV | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
ref: "${{ env.GITHUB_SHA }}" | |
fetch-depth: 2 | |
- name: Build matrix | |
id: set-matrix | |
shell: bash | |
env: | |
BASE_SHA: ${{ env.BASE_SHA }} | |
CONFIG: .github/docker-build-config.yml | |
run: | | |
set -xuo pipefail | |
set +e | |
CHANGED=$(git diff --name-only $BASE_SHA ${{ github.sha }} -- label_studio_ml/ | xargs -L1 dirname | uniq | cut -d'/' -f2-) | |
CHANGED_EXAMPLES=$(echo "$CHANGED" | grep examples ) | |
set -e | |
echo "Finding examples to test" | |
if [[ $(echo "$CHANGED" | grep -c -v ^$) == 0 ]]; then | |
echo "matrix={\"ml_backend\":[]}" >> $GITHUB_OUTPUT | |
else | |
ALL_EXAMPLES=$(ls -1d label_studio_ml/examples/*/ | cut -d'/' -f3- | cut -d'/' -f1) | |
echo "Reading configuration from $CONFIG" | |
MATRIX="{\"ml_backend\": [" | |
while IFS= read -r line; do | |
FOLDER=$(echo $line | awk '{print $1}') | |
TAG_PREFIX=$(echo $line | awk '{print $2}') | |
if egrep -q $FOLDER <<< "${CHANGED_EXAMPLES}"; then | |
MATRIX+="{\"backend_dir_name\":\"$FOLDER\", \"backend_tag_prefix\":\"$TAG_PREFIX\"}," | |
fi | |
done < <(yq '.[] | .backend_dir_name + " " + .backend_tag_prefix' $CONFIG) | |
MATRIX=$(echo "$MATRIX" | sed 's/,$//') | |
MATRIX+="]}" | |
echo "matrix=$MATRIX" >> $GITHUB_OUTPUT | |
fi |