From 3c6969f7f475335293ecd69ecce6d38b3d3cb960 Mon Sep 17 00:00:00 2001 From: Arthur Meyre Date: Tue, 31 Aug 2021 13:43:55 +0200 Subject: [PATCH] tools: updated package watcher with helper script --- .github/workflows/package-watcher.yaml | 43 ++++-------- .../container_timestamp_check.sh | 70 +++++++++++++++++++ 2 files changed, 82 insertions(+), 31 deletions(-) create mode 100755 script/actions_utils/container_timestamp_check.sh diff --git a/.github/workflows/package-watcher.yaml b/.github/workflows/package-watcher.yaml index f448173c2..1352bf146 100644 --- a/.github/workflows/package-watcher.yaml +++ b/.github/workflows/package-watcher.yaml @@ -1,42 +1,23 @@ name: Package Version Checker on: - # schedule: - # # * is a special character in YAML so you have to quote this string - # # At minute 0 for each hour from 8:00 to 22:00 inclusive from Monday to Friday inclusive - # - cron: '0 8-22 * * 1-5' - workflow_dispatch: + schedule: + # * is a special character in YAML so you have to quote this string + # At minute 0 for each hour from 8:00 to 22:00 inclusive from Monday to Friday inclusive + - cron: '0 8-22 * * 1-5' jobs: check_and_notify_build: name: Check timestamps and notify build runs-on: ubuntu-20.04 - env: - BASE_IMG_ENDPOINT_URL: "https://api.github.com/orgs/zama-ai/packages/container/zamalang-compiler" - ENV_IMG_ENDPOINT_URL: "https://api.github.com/orgs/zama-ai/packages/container/hdk-env" steps: + - name: Checkout Code + uses: actions/checkout@v2 - name: Compare image timestamps and notify run: | - BASE_IMG_TIMESTAMP=$(curl \ - -X GET \ - -H "Accept: application/vnd.github.v3+json" \ - -H "Authorization: token ${{ secrets.BOT_TOKEN }}" \ - "${BASE_IMG_ENDPOINT_URL}" | jq -r '.updated_at') - ENV_IMG_TIMESTAMP=$(curl \ - -X GET \ - -H "Accept: application/vnd.github.v3+json" \ - -H "Authorization: token ${{ secrets.BOT_TOKEN }}" \ - "${ENV_IMG_ENDPOINT_URL}" | jq -r '.updated_at') - BASE_IMG_DATE=$(date -d ${BASE_IMG_TIMESTAMP} +%s) - ENV_IMG_DATE=$(date -d ${ENV_IMG_TIMESTAMP} +%s) - if [[ "${BASE_IMG_DATE}" -ge "${ENV_IMG_DATE}" ]]; then - echo "Env image out of date, sending rebuild request." - curl \ - -X POST \ - -H "Accept: application/vnd.github.v3+json" \ - -H "Authorization: token ${{ secrets.BOT_TOKEN }}" \ - https://api.github.com/repos/zama-ai/hdk/dispatches \ - -d '{"event_type":"rebuild-docker"}' - else - echo "Image up to date, nothing to do." - fi + ./script/actions_utils/container_timestamp_check.sh \ + --base_img_url \ + https://api.github.com/orgs/zama-ai/packages/container/zamalang-compiler/versions \ + --env_img_url \ + https://api.github.com/orgs/zama-ai/packages/container/hdk-env/versions \ + --token ${{ secrets.BOT_TOKEN }} diff --git a/script/actions_utils/container_timestamp_check.sh b/script/actions_utils/container_timestamp_check.sh new file mode 100755 index 000000000..989dfb689 --- /dev/null +++ b/script/actions_utils/container_timestamp_check.sh @@ -0,0 +1,70 @@ +#!/bin/bash -e + +set -e + +BASE_IMG_ENDPOINT_URL= +ENV_IMG_ENDPOINT_URL= +TOKEN= + +while [ -n "$1" ] +do + case "$1" in + "--base_img_url" ) + shift + BASE_IMG_ENDPOINT_URL="$1" + ;; + + "--env_img_url" ) + shift + ENV_IMG_ENDPOINT_URL="$1" + ;; + + "--token" ) + shift + TOKEN="$1" + ;; + + *) + echo "Unknown param : $1" + exit -1 + ;; + esac + shift +done + +BASE_JSON=$(curl \ +-X GET \ +-H "Accept: application/vnd.github.v3+json" \ +-H "Authorization: token ${TOKEN}" \ +"${BASE_IMG_ENDPOINT_URL}") + +BASE_IMG_TIMESTAMP=$(echo "${BASE_JSON}" | jq -r 'sort_by(.updated_at)[-1].updated_at') + +ENV_JSON=$(curl \ +-X GET \ +-H "Accept: application/vnd.github.v3+json" \ +-H "Authorization: token ${TOKEN}" \ +"${ENV_IMG_ENDPOINT_URL}") + +ENV_IMG_TIMESTAMP=$(echo "${ENV_JSON}" | jq -r 'sort_by(.updated_at)[-1].updated_at') + +echo "Base timestamp: ${BASE_IMG_TIMESTAMP}" +echo "Env timestamp: ${ENV_IMG_TIMESTAMP}" + +BASE_IMG_DATE=$(date -d ${BASE_IMG_TIMESTAMP} +%s) +ENV_IMG_DATE=$(date -d ${ENV_IMG_TIMESTAMP} +%s) + +echo "Base epoch: ${BASE_IMG_DATE}" +echo "Env epoch: ${ENV_IMG_DATE}" + +if [[ "${BASE_IMG_DATE}" -ge "${ENV_IMG_DATE}" ]]; then + echo "Env image out of date, sending rebuild request." + curl \ + -X POST \ + -H "Accept: application/vnd.github.v3+json" \ + -H "Authorization: token ${TOKEN}" \ + https://api.github.com/repos/zama-ai/hdk/dispatches \ + -d '{"event_type":"rebuild-docker"}' +else + echo "Image up to date, nothing to do." +fi