Skip to content

Commit

Permalink
Merge pull request #444 from rokkiter/sync-api-action
Browse files Browse the repository at this point in the history
sync api by pipeline
  • Loading branch information
Iceber authored Nov 26, 2022
2 parents b4d768a + e901cb7 commit 61109a0
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/sync-api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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 }}
REFNAME: ${{ 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
80 changes: 80 additions & 0 deletions hack/sync-api.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env bash

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

function usage() {
cat <<EOF
ENV:
REFTYPE: the current operation is tag or sync code. if it is undefined, it defaults to branch
eg. REFTYPE=tag or REFTYPE=branch
REFNAME: the name of the branch or tag, when REFTYPE=tag it mean the name of tag,
otherwise it mean the name of branch
GH_TOKEN: github token for api repo auth.
EOF
}

API_ROOT="staging/src/github.com/clusterpedia-io/api"
if [ ! -d $API_ROOT ];then
echo "can not find API_ROOT in the path, please check in the clusterpedia root path"
exit 1
fi

if [ -z $GH_TOKEN ]; then
echo "the github token is not in the env, please check GH_TOKEN"
usage
exit 1
fi

if [ -z $REFNAME ]; then
echo "can not find refg"
usage
exit 1
fi

API_REPO="https://$GH_TOKEN@github.com/clusterpedia-io/api.git"

install_filter_repo(){
python3 -m pip install --user git-filter-repo
}

TMP_CLUSTERPEDIA=/tmp/clusterpedia
clean_tmp_dir(){
rm -rf $TMP_CLUSTERPEDIA
}
trap clean_tmp_dir EXIT

create_tmp_dir(){
mkdir -p $TMP_CLUSTERPEDIA/api
git clone ./ $TMP_CLUSTERPEDIA/api
cd $TMP_CLUSTERPEDIA/api
}

# check tag, if exist, delete it
check_tag(){
if [ -n "$(git ls-remote --tags api-origin -l $REFNAME)" ]; then
echo "tag already exist, delete it before retag"
git push -d api-origin $REFNAME
fi
}

sync_api(){
create_tmp_dir
if [ $REFTYPE == "tag" ]; then
git filter-repo --subdirectory-filter $API_ROOT
git remote add api-origin $API_REPO
check_tag
git push api-origin $REFNAME
echo "push tag success~"
else
git filter-repo --subdirectory-filter $API_ROOT
git remote add api-origin $API_REPO
git push api-origin $REFNAME
echo "sync code success~"
fi
}

install_filter_repo

sync_api

0 comments on commit 61109a0

Please sign in to comment.