-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: rokkiter <101091030+rokkiter@users.noreply.github.com>
- Loading branch information
Showing
2 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -o errexit | ||
set -o nounset | ||
set -o pipefail | ||
|
||
function usage() { | ||
cat <<EOF | ||
ENV: | ||
API_ROOT: clusterpedia api path, required if the pwd is not the api path. | ||
eg. API_ROOT=/home/runner/work/clusterpedia/staging/src/github.com/clusterpedia-io/api | ||
GH_TOKEN: github token for api repo auth. | ||
EOF | ||
} | ||
|
||
set +e; REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null);set -e | ||
if [ -z $REPO_ROOT ]; then | ||
if [ -z $API_ROOT ]; then | ||
echo "the current directory is not in the clusterpedia api path, please set API_ROOT=<clusterpedia api path>" | ||
usage | ||
exit 1 | ||
fi | ||
REPO_ROOT=$(pwd) | ||
else | ||
API_ROOT="${REPO_ROOT}/staging/src/github.com/clusterpedia-io/api" | ||
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 | ||
|
||
raw=$(git branch -r --contains $REF) | ||
BRANCH_NAME=${raw/origin\/} | ||
|
||
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 | ||
} | ||
|
||
check_branch(){ | ||
if [ -z "$(git ls-remote --exit-code --heads origin $BRANCH_NAME)" ]; then | ||
echo "remote branch does not exist, create it" | ||
git checkout -b $BRANCH_NAME | ||
git push --set-upstream origin $BRANCH_NAME | ||
else | ||
git checkout $BRANCH_NAME | ||
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 | ||
|