Skip to content

Commit

Permalink
Merge pull request #106 from trade-tariff/FPO-419-automated-model-runs
Browse files Browse the repository at this point in the history
FPO-419: Adds automated model build workflow
  • Loading branch information
amberstarlight authored Oct 22, 2024
2 parents 661f167 + e153417 commit c80ff86
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 3 deletions.
22 changes: 22 additions & 0 deletions .circleci/bin/closepr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

[[ "$TRACE" ]] && set -o xtrace
set -o errexit
set -o nounset
set -o pipefail

close_pr() {
gh pr close "$CIRCLE_PULL_REQUEST"
}

delete_branch() {
git push origin --delete "$CIRCLE_BRANCH"
}

main() {
.circleci/bin/configuregit
close_pr
delete_branch
}

main
9 changes: 9 additions & 0 deletions .circleci/bin/configuregit
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

[[ "$TRACE" ]] && set -o xtrace
set -o errexit
set -o nounset
set -o pipefail

git config --global user.email "noreply@trade-tariff.service.gov.uk"
git config --global user.name "trade-tariff-bot"
94 changes: 94 additions & 0 deletions .circleci/bin/createpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/env bash

[[ "$TRACE" ]] && set -o xtrace
set -o errexit
set -o nounset
set -o pipefail

EPOCH=$(date +%s)

update_patch_version() {
local raw_current_version
local current_version
local current_major_version
local current_minor_version
local current_patch_version
local new_patch_version
local new_version

raw_current_version=$(cat search-config.toml | grep version)
current_version=$(cat search-config.toml | grep version | cut -d= -f2 | tr -d "[:space:]" | tr -d '"')
current_major_version=$(echo $current_version | cut -d. -f1)
current_minor_version=$(echo $current_version | cut -d. -f2)
current_patch_version=$(echo $current_version | cut -d. -f3)
new_patch_version=$((current_patch_version + 1))
new_version=$(echo "$current_major_version.$current_minor_version.$new_patch_version")

sed -i "s/$raw_current_version/version = \"$new_version\"/g" search-config.toml
}

get_branch_name() {
echo "automated-model-run-$EPOCH"
}

get_model_version() {
echo "$(cat search-config.toml | grep version | cut -d= -f2 | tr -d "[:space:]" | tr -d '"')"
}

checkout_new_branch() {
git checkout -b "$(get_branch_name)"
}

stage_commit_and_push_changes() {
git add search-config.toml
git commit -m "BAU: Automated model run $EPOCH."
git push origin "$(get_branch_name)"
}

open_pr() {
local title
local description
local base_branch="main"
local branch_name

title="🔄 BAU: Automated model run updated to model version $(get_model_version)"
description=$(
cat <<EOF
# Automated Model Run ${EPOCH}
🚀 **What's happening?**
This automated model run has been initiated to incorporate any new data added to the model via the training pipeline's dynamic data sources.
🔔 **Note:**
This pull request will be **closed automatically** once the model run is complete.
Thank you for your patience! 🙌
EOF
)
branch_name=$(get_branch_name)

gh pr create --title "$title" --body "$description" --base "$base_branch" --head "$branch_name"
}

tag_branch() {
local tag_name
local tag_description

tag_name=$(get_branch_name)-$(get_model_version)
tag_description="BAU: Automated model run bumping model to $(get_model_version) on $EPOCH."

git tag -a $tag_name -m "$tag_description"
git push origin $tag_name
}

main() {
.circleci/bin/configuregit
update_patch_version
checkout_new_branch
stage_commit_and_push_changes
open_pr
tag_branch
}

main
10 changes: 9 additions & 1 deletion .circleci/bin/fetchmodel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ MODEL_KEY=$(.circleci/bin/getmodel)
VERSION_PREFIX=$(echo "$MODEL_KEY" | awk -F/ '{print $1}')
MODEL_BUCKET_NAME=trade-tariff-models-382373577178

source .circleci/bin/isautomatedbuild

if [ "$MODEL_KEY" = "" ]; then
echo "No model for current version found"
exit 1
Expand All @@ -23,6 +25,12 @@ validate_model() {
echo "Model $MODEL_KEY has not passed validation"
aws s3 cp --quiet "s3://$MODEL_BUCKET_NAME/$VERSION_PREFIX/failed" . && {
echo "Model $MODEL_KEY is marked as failed"

if is_automated_build; then
echo "Automated build, closing PR"

.circleci/bin/closepr
fi
exit 1
}
exit 1
Expand Down Expand Up @@ -51,7 +59,7 @@ mark_environment() {
store_version() {
echo "Storing version $VERSION_PREFIX"
rm -f MODEL_VERSION
echo "$VERSION_PREFIX" > MODEL_VERSION
echo "$VERSION_PREFIX" >MODEL_VERSION
}

validate_model
Expand Down
21 changes: 21 additions & 0 deletions .circleci/bin/ghinstall
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail

sudo apt-get install dirmngr

curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo gpg --dearmor -o /usr/share/keyrings/githubcli-archive-keyring.gpg

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null

sudo apt update
sudo apt install gh

if [ -z "${GITHUB_TOKEN:-}" ]; then
echo "GITHUB_TOKEN is not set"
exit 1
fi

echo "${GITHUB_TOKEN}" | env -u GITHUB_TOKEN gh auth login --with-token --hostname github.com
18 changes: 18 additions & 0 deletions .circleci/bin/isautomatedbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail
set -o noclobber

get_current_head_tags() {
git tag --points-at HEAD
}

is_automated_build() {
if [[ "$(get_current_head_tags)" == *"automated-model-run"* ]]; then
return 0
else
return 1
fi
}
24 changes: 24 additions & 0 deletions .circleci/bin/mergepr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

[[ "$TRACE" ]] && set -o xtrace
set -o errexit
set -o nounset
set -o pipefail

source .circleci/bin/isautomatedbuild

merge_pr() {
gh pr merge "$CIRCLE_PULL_REQUEST"
}

main() {
.circleci/bin/configuregit

if is_automated_build; then
echo "Automated build, merging PR"

merge_pr
fi
}

main
32 changes: 32 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ jobs:
command: .circleci/bin/buildami
no_output_timeout: 30m

automated-build-model:
executor: base
steps:
- checkout
- run: ".circleci/bin/ghinstall"
- run: ".circleci/bin/createpr"

build-model:
executor: base
steps:
Expand Down Expand Up @@ -207,6 +214,13 @@ jobs:
name: "Clear Lingering Model Instances"
command: .circleci/bin/clearinstances

merge-automated-model-pr:
executor: base
steps:
- checkout
- run: ".circleci/bin/ghinstall"
- run: ".circleci/bin/mergepr"

workflows:
version: 2

Expand Down Expand Up @@ -288,6 +302,15 @@ workflows:
- deploy-development
<<: *filter-not-main

- merge-automated-model-pr:
name: merge-automated-model-pr
context: trade-tariff-fpo-models-development
requires:
- smoketest-development
- e2etest-development
- perftest-development
<<: *filter-not-main

deploy-to-staging-and-production:
jobs:
- fetch-model:
Expand Down Expand Up @@ -368,3 +391,12 @@ workflows:
jobs:
- clear-model-instances:
context: trade-tariff-fpo-models-development

bi-weekly-automated-model-build:
triggers:
- schedule:
cron: "0 0 1,15 * *" # 1st and 15th of every month at midnight UTC
<<: *filter-main
jobs:
- automated-build-model:
context: trade-tariff-fpo-models-development
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ distlib==0.3.8
filelock==3.13.4
fsspec==2024.3.1
h11==0.14.0
huggingface-hub==0.22.2
huggingface-hub==0.25.2
identify==2.5.35
idna==3.7
Jinja2==3.1.4
Expand Down
2 changes: 1 addition & 1 deletion requirements_lambda.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
aws-lambda-powertools==2.28.1
jmespath==1.0.1
lingua-language-detector==2.0.2
sentence-transformers==2.2.2
sentence-transformers==2.6.1
sentry-sdk==2.8.0
toml==0.10.2

0 comments on commit c80ff86

Please sign in to comment.