diff --git a/.github/workflows/hamilton-ui-build-and-push.yml b/.github/workflows/hamilton-ui-build-and-push.yml new file mode 100644 index 000000000..7a642f06e --- /dev/null +++ b/.github/workflows/hamilton-ui-build-and-push.yml @@ -0,0 +1,83 @@ +name: Building and pushing UI frontend and backend images + +on: + schedule: + - cron: '0 0 * * *' + workflow_dispatch: + +jobs: + check_and_build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Log in to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_TOKEN }} + + - name: Install jq + run: sudo apt-get update && sudo apt-get install -y jq + + - name: Check latest version of sf-hamilton-ui + id: check_version + run: | + response=$(curl -s https://pypi.org/pypi/sf-hamilton-ui/json) + version=$(echo "$response" | jq -r '.info.version') + echo "latest_version=$version" >> $GITHUB_ENV + + - name: Fetch highest version from Docker Hub + id: fetch_tags + run: | + # get the list of tags for the frontend image + tags=$(curl -s https://registry.hub.docker.com/v2/repositories/dagworks/ui-frontend/tags?page_size=100 | jq -r '.results[].name') + + # find the highest version tag + highest_version="none" + for tag in $tags; do + if [[ "$tag" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + if [ "$highest_version" == "none" ] || [ "$(printf '%s\n' "$tag" "$highest_version" | sort -V | tail -n1)" == "$tag" ]; then + highest_version="$tag" + fi + fi + done + + echo "Highest version on Docker Hub: $highest_version" + echo "highest_version=$highest_version" >> $GITHUB_OUTPUT + + - name: Compare versions + id: compare_versions + run: | + echo "Current version: $latest_version" + highest_version=${{ steps.fetch_tags.outputs.highest_version }} + echo "Highest version found: $highest_version" + + if [[ "$latest_version" == "$highest_version" ]]; then + echo "No new version found. Exiting." + echo "skip_build=true" >> $GITHUB_ENV + else + echo "New version detected: $latest_version" + echo "skip_build=false" >> $GITHUB_ENV + fi + + - name: Run build and push script + if: env.skip_build != 'true' + run: | + chmod +x ./ui/buildx_and_push.sh + ./ui/buildx_and_push.sh $latest_version + + - name: Save the latest version to a file + if: env.skip_build != 'true' + run: | + mkdir -p version_cache + echo "$latest_version" > version_cache/previous_version.txt + + - name: Cache the latest version + if: env.skip_build != 'true' + uses: actions/cache@v4 + with: + path: ./version_cache + key: hamilton-ui-version-cache diff --git a/ui/buildx_and_push.sh b/ui/buildx_and_push.sh new file mode 100755 index 000000000..755cb766a --- /dev/null +++ b/ui/buildx_and_push.sh @@ -0,0 +1,69 @@ +#!/bin/bash + +export DOCKER_CLI_EXPERIMENTAL=enabled + +# check if Docker Buildx is installed +check_buildx_installed() { + if ! docker buildx version &> /dev/null; then + echo "Error: Docker Buildx is not installed. Please install Docker Buildx to proceed." + exit 1 + fi +} + +# check if jq is installed +check_jq_installed() { + if ! jq --version &> /dev/null; then + echo "Error: jq is not installed. Please install jq to proceed." + exit 1 + fi +} + +# fetches the latest version of sf-hamilton-ui from PyPI +get_latest_version() { + response=$(curl -s https://pypi.org/pypi/sf-hamilton-ui/json) + + # check if curl succeeded and the response is not empty + if [ $? -ne 0 ] || [ -z "$response" ]; then + echo "Error: Failed to fetch data from PyPI. Please check your internet connection or the URL." + exit 1 + fi + + # extract version using jq and handle potential errors + version=$(echo "$response" | jq -r '.info.version') + if [ "$version" == "null" ]; then + echo "Error: Unable to extract version from the response." + exit 1 + fi + + echo "$version" +} + +# check if Docker Buildx and jq are installed +check_buildx_installed +check_jq_installed + +VERSION=$(get_latest_version) + +echo "Using sf-hamilton-ui version: $VERSION" + +# check if Buildx is already enabled; create a builder instance if not +docker buildx inspect hamilton-builder > /dev/null 2>&1 || \ + docker buildx create --use --name hamilton-builder + +FRONTEND_IMAGE="dagworks/ui-frontend" +BACKEND_IMAGE="dagworks/ui-backend" + +# define common platforms/architectures +PLATFORMS="linux/amd64,linux/arm64" + +cd "$(dirname "$0")" # cd into the directory where this script is present(i.e. ui) + +docker buildx build --platform $PLATFORMS \ + -t $BACKEND_IMAGE:$VERSION -t $BACKEND_IMAGE:latest \ + --push -f backend/Dockerfile.backend-prod backend/ + +docker buildx build --platform $PLATFORMS \ + -t $FRONTEND_IMAGE:$VERSION -t $FRONTEND_IMAGE:latest \ + --push -f frontend/Dockerfile.frontend-prod frontend/ \ + --build-arg REACT_APP_AUTH_MODE=local \ + --build-arg REACT_APP_USE_POSTHOG=false \