Skip to content

Commit 5ddd6a1

Browse files
ci: PLATE-837: Change Docker build to common approach (#417)
ci: PLATE-837: Change Docker image version to common approach
1 parent c093a7f commit 5ddd6a1

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

.github/workflows/build.yml

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,47 @@ jobs:
1616
# Allow list of models
1717
include:
1818
- backend_dir_name: segment_anything_model
19-
backend_tag_name: sam-v0
19+
backend_tag_prefix: sam-
2020
- backend_dir_name: llm_interactive
21-
backend_tag_name: llm-v8
21+
backend_tag_prefix: llm-
2222
- backend_dir_name: the_simplest_backend
23-
backend_tag_name: simplebackend-v0
23+
backend_tag_prefix: simplebackend-
2424
env:
2525
IMAGE_NAME: heartexlabs/label-studio-ml-backend
2626
examples_dir: label_studio_ml/examples
2727
backend_dir_name: ${{ matrix.backend_dir_name }}
28-
backend_tag_name: ${{ matrix.backend_tag_name }}
28+
backend_tag_prefix: ${{ matrix.backend_tag_prefix }}
2929
steps:
3030
- uses: actions/checkout@v4
3131
with:
3232
ref: "${{ env.GITHUB_SHA }}"
3333
fetch-depth: 0
3434

35-
- name: Set up Docker Buildx
36-
uses: docker/[email protected]
35+
- name: Calculate version
36+
id: version
37+
env:
38+
BRANCH_NAME: "${{ github.event.pull_request.head.ref || github.ref_name }}"
39+
PREFIX: "${{ env.backend_tag_prefix }}"
40+
run: |
41+
set -xueo pipefail
42+
MAX_TAG_LENGTH=50
43+
pretty_branch_name="$(echo -n "${BRANCH_NAME#refs/heads/}" | sed 's#/#-#g' | sed 's#_#-#g'| sed 's#\.#-#g' | tr '[:upper:]' '[:lower:]')"
44+
echo "pretty_branch_name=$pretty_branch_name" >> $GITHUB_OUTPUT
45+
timestamp="$(date +'%Y%m%d.%H%M%S')"
46+
echo "timestamp=$timestamp" >> $GITHUB_OUTPUT
47+
short_sha="$(git rev-parse --short HEAD)"
48+
echo "short_sha=$short_sha" >> $GITHUB_OUTPUT
49+
long_sha="$(git rev-parse HEAD)"
50+
echo "sha=$long_sha" >> $GITHUB_OUTPUT
51+
short_sha_length="$(echo $short_sha | awk '{print length}')"
52+
timestamp_length="$(echo $timestamp | awk '{print length}')"
53+
prefix_length="$(echo $PREFIX | awk '{print length}')"
54+
short_branch="$(echo $pretty_branch_name | cut -c1-$((MAX_TAG_LENGTH - 2 - short_sha_length - timestamp_length - prefix_length)))"
55+
echo "short_branch=$short_branch" >> $GITHUB_OUTPUT
56+
image_version="${PREFIX}${timestamp}-${short_branch}-${short_sha}"
57+
echo "image_version=$image_version" >> $GITHUB_OUTPUT
58+
image_branch_version="${PREFIX}${short_branch}"
59+
echo "image_branch_version=$image_branch_version" >> $GITHUB_OUTPUT
3760
3861
- name: Check for Changes in Directory
3962
id: check_changes
@@ -48,20 +71,43 @@ jobs:
4871
echo "skip=false" >> $GITHUB_OUTPUT
4972
fi
5073
74+
- name: Set up Docker Buildx
75+
uses: docker/[email protected]
76+
5177
- name: Login to DockerHub
5278
if: ${{ steps.check_changes.outputs.skip != 'true' && !github.event.pull_request.head.repo.fork }}
5379
uses: docker/[email protected]
5480
with:
5581
username: ${{ vars.DOCKERHUB_USERNAME }}
5682
password: ${{ secrets.DOCKERHUB_TOKEN }}
5783

84+
- name: Calculate Docker tags
85+
id: calculate-docker-tags
86+
uses: actions/github-script@v7
87+
env:
88+
IMAGE_NAME: "${{ env.IMAGE_NAME }}"
89+
TAGS: "${{ steps.version.outpust.image_version }},${{ steps.version.outpust.image_branch_version }}"
90+
with:
91+
script: |
92+
const raw_tags_input = process.env.TAGS;
93+
const image_name = process.env.IMAGE_NAME;
94+
95+
const tags = raw_tags_input
96+
.split(',')
97+
.map(x => x.trim())
98+
.map(x => `${image_name}:${x}`)
99+
.join(',');
100+
console.log(tags);
101+
core.setOutput("tags", tags);
102+
103+
58104
- name: Push Docker image
59105
if: steps.check_changes.outputs.skip != 'true'
60106
uses: docker/[email protected]
61107
id: docker_build_and_push
62108
with:
63-
context: ${{ env.examples_dir }}/${{ env.backend_dir_name }}
109+
context: "${{ env.examples_dir }}/${{ env.backend_dir_name }}"
64110
push: true
65-
tags: ${{ env.IMAGE_NAME }}:${{ env.backend_tag_name }}
111+
tags: "${{ steps.calculate-docker-tags.outputs.tags }}"
66112
cache-from: type=gha
67113
cache-to: type=gha,mode=max

0 commit comments

Comments
 (0)