From fdd365fd994b15e2b2dd2a73631b4bdb255268bc Mon Sep 17 00:00:00 2001 From: rokkiter <101091030+rokkiter@users.noreply.github.com> Date: Fri, 4 Nov 2022 14:00:51 +0800 Subject: [PATCH] sync api by pipeline Signed-off-by: rokkiter <101091030+rokkiter@users.noreply.github.com> --- .github/workflows/sync-api.yml | 29 +++++++++++++ hack/sync-api.sh | 74 ++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 .github/workflows/sync-api.yml create mode 100755 hack/sync-api.sh diff --git a/.github/workflows/sync-api.yml b/.github/workflows/sync-api.yml new file mode 100644 index 000000000..19d4280d6 --- /dev/null +++ b/.github/workflows/sync-api.yml @@ -0,0 +1,29 @@ +name: Sync Api +on: + push: + paths: + - 'staging/src/github.com/clusterpedia-io/api/*' +jobs: + sync-api: + name: Sync Api + runs-on: ubuntu-latest + env: + WORKSPACE: ${{ github.workspace }}/src/github.com/clusterpedia-io/clusterpedia + GH_TOKEN: ${{ secrets.CLUSTERPEDIA_BOT_TOKEN }} + MESSAGE: ${{ github.event.head_commit.message }} + REFTYPE: ${{ github.ref_type }} + TAGNAME: ${{ github.ref_name }} + REF: ${{ github.ref }} + defaults: + run: + working-directory: ${{ env.WORKSPACE }} + steps: + - name: Checkout code + uses: actions/checkout@v3 + with: + token: ${{ secrets.CLUSTERPEDIA_BOT_TOKEN }} + path: ${{ env.WORKSPACE }} + ref: ${{ github.ref }} + fetch-depth: 0 + - run: hack/sync-api.sh + diff --git a/hack/sync-api.sh b/hack/sync-api.sh new file mode 100755 index 000000000..288f169da --- /dev/null +++ b/hack/sync-api.sh @@ -0,0 +1,74 @@ +#!/usr/bin/env bash + +set -o errexit +set -o nounset +set -o pipefail + +function usage() { + cat </dev/null);set -e +if [ -z $RAW ]; then + echo "the current directory is not in the clusterpedia path" + usege + exit 1 +else + BRANCH_NAME=${RAW/origin\/} + if [ -z $BRANCH_NAME ]; then + echo "can not get current branch" + usage + exit 1 + fi +fi + +if [ -z $GH_TOKEN ]; then + echo "the github token is not in the env, please check CLUSTERPEDIA_BOT_TOKEN" + usage + exit 1 +else + API_REPO="https://$GH_TOKEN@github.com/clusterpedia/api.git" +fi + +TAG_MESSAGE=$(git tag -l --format="%(contents)" $TAGNAME) + +install_filter_repo(){ + python3 -m pip install --user git-filter-repo +} + +# check tag, if exist, delete it +check_tag(){ + git tag -d $TAGNAME + if [ -n "$(git ls-remote --tags origin -l $TAGNAME)" ]; then + echo "tag already exist, delete it before retag" + git push -d origin $TAGNAME + git tag -d $TAGNAME + fi +} + +sync_api(){ + if [ $REFTYPE == "tag" ]; then + git filter-repo --subdirectory-filter staging/src/github.com/clusterpedia-io/api --force + git remote add origin $API_REPO + check_tag + git tag $TAGNAME -a -m $TAG_MESSAGE + git push origin $TAGNAME + echo "push tag success~" + else + git filter-repo --subdirectory-filter staging/src/github.com/clusterpedia-io/api --force + git remote add origin $API_REPO + git push origin $BRANCH_NAME + echo "sync code success~" + fi +} + +install_filter_repo + +sync_api +