Skip to content

Commit

Permalink
fix publish_remove_last_build
Browse files Browse the repository at this point in the history
  • Loading branch information
ZanderZhao committed Oct 23, 2020
1 parent 5cf52f1 commit 6fbbd8d
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 92 deletions.
12 changes: 5 additions & 7 deletions .github/workflows/gitbook-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ jobs:
uses: actions/checkout@v2

# example1
- name: Gitbook Action
uses: ZanderZhao/gitbook-action@v1.2.3 # -> or ZanderZhao/gitbook-action@master
with: # or fork this repo and use YourName/gitbook-action@master
token: ${{ secrets.PERSONAL_TOKEN }} # -> remember add this in settings/secrets as https://ZanderZhao.github.io/gitbook-action/
- name: Gitbook Action # https://github.com/ZanderZhao/gitbook-action/releases
uses: ZanderZhao/gitbook-action@master # -> or ZanderZhao/gitbook-action@v1.2.4, Just example, click above, use latest please
with: # or fork this repo and use YourName/gitbook-action@master
token: ${{ secrets.PERSONAL_TOKEN }} # -> remember add this in settings/secrets as https://ZanderZhao.github.io/gitbook-action/
time_zone: Asia/Shanghai
source_dir: gitbook-source
source_edit_time: true
Expand All @@ -32,7 +32,5 @@ jobs:
source_branch: master
source_dir: docs
publish_dir: gitbook-docs
publish2_repo: gitbook-org/gitbook


publish2_repo: gitbook-org/gitbook

10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ jobs:
steps:
- name: Checkout action
uses: actions/checkout@v2
- name: Gitbook Action
uses: ZanderZhao/[email protected].3 # -> or ZanderZhao/gitbook-action@master
- name: Gitbook Action # https://github.com/ZanderZhao/gitbook-action/releases
uses: ZanderZhao/[email protected].4 # -> or ZanderZhao/gitbook-action@master. If not use master click above, use latest please
with: # or fork this repo and use YourName/gitbook-action@master
token: ${{ secrets.PERSONAL_TOKEN }} # -> remember add this in settings/secrets as following
```
​ [Detailed step](https://zanderzhao.github.io/gitbook-action/how-to-use.html#addaction) [Official introduction](https://help.github.com/cn/actions/getting-started-with-github-actions/starting-with-preconfigured-workflow-templates)
​ [Detailed step](https://zanderzhao.github.io/gitbook-action/how-to-use.html#addaction) | [Official introduction](https://help.github.com/cn/actions/getting-started-with-github-actions/starting-with-preconfigured-workflow-templates)
### **STEP2** Generate Token and add to Secrets
Expand All @@ -37,14 +37,14 @@ Create token from https://github.com/settings/tokens
+ choose repo
+ click Generate token
​ [Detailed step](https://zanderzhao.github.io/gitbook-action/how-to-use.html#createtoken) [Official introduction](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line#creating-a-token)
​ [Detailed step](https://zanderzhao.github.io/gitbook-action/how-to-use.html#createtoken) | [Official introduction](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line#creating-a-token)
Add your token to https://github.com/ **yourname/yourrepo** /settings/secrets
+ Name: PERSONAL_TOKEN
+ Value: which you get before
​ [Detail step](https://zanderzhao.github.io/gitbook-action/how-to-use.html#addtoken) [Official introduction](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets#creating-encrypted-secrets)
​ [Detail step](https://zanderzhao.github.io/gitbook-action/how-to-use.html#addtoken) | [Official introduction](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets#creating-encrypted-secrets)
### **STEP3** Choose options
Expand Down
111 changes: 67 additions & 44 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh -x


####################### prepare sh config ###############################
# print missage style
# error 31XX
print_error(){
Expand All @@ -18,6 +18,7 @@ print_warning(){
echo "\033[32m Find Detail From: https://ZanderZhao.github.io/gitbook-action/warning \033[0m"
}

################################ prepare global config and show config ###############################
# prepare var and env
echo "--------------------------------------------"
print_info "STEP1 env_config"
Expand Down Expand Up @@ -520,6 +521,8 @@ else
echo "PUBLISH3 default false, if need you can publish different repo/branch"
fi

# install gitbook if not default version

echo "--------------------"
echo "FOR git node npm gitbook-cli"
echo "----------"
Expand All @@ -538,6 +541,9 @@ else
echo "gitbook-cli 2.3.2"
fi


######################## clone source ###################################3

# git clone source from repo/branch
# source is must have. source2 can be not

Expand Down Expand Up @@ -614,6 +620,8 @@ if [ ${INPUT_SOURCE2_REPO} != "null" ]; then
fi
fi

########################### clone publish branch ####################################
# maybe different repo and need history before or be a part of repo

# git clone publish history, prepare for the gitbook build file is apart of other object

Expand Down Expand Up @@ -684,24 +692,40 @@ if [ ${INPUT_PUBLISH3_REPO} != "null" ]; then
fi
fi


################ Build #################################3

echo "--------------------------------------------"
print_info "STEP4 build_gitbook_and_generate_file"
echo "-----------------------------------"


cd local_source
ls

# prepare install
if [ -n "${INPUT_PREPARE_INSTALL}" ]; then
print_info "Message:Runing user's prepare_install"
${INPUT_PREPARE_INSTALL}
# npm i --unsafe-perm -g [email protected]
# just like npm i --unsafe-perm -g [email protected]
# which has been installed before ? -> https://hub.docker.com/r/zanderzhao/gitbook-action/dockerfile
else
echo "no prepare_install"
fi

# install font
if ${INPUT_GITBOOK_PDF} || ${INPUT_GITBOOK_EPUB} || ${INPUT_GITBOOK_MOBI} ; then
if [ -n "${INPUT_FONT_INSTALL}" ]; then
apt-get update
apt-get install sudo -y
print_info "Message:Runing user's font_install"
${INPUT_FONT_INSTALL}
#sudo apt-get install fonts-noto-cjk
#sudo apt-get install ttf-mscorefonts-installer
else
print_warning "3308:Not install any font, maybe affect the pdf/mobi/epub, can add font_install at book.json"
fi
fi

gitbook build --gitbook=${GITBOOK_BUILD_VERSION}
if [ $? -eq 0 ]; then
print_info "Message:gitbook built success"
Expand All @@ -714,23 +738,10 @@ else # need plugins or README.md SUMMARY.md
print_info "Message:gitbook built success(with plugins)"
else
print_error "3105:gitbook built fail,please check is there something wrong with your book.json or others"

fi
fi

# install font
if ${INPUT_GITBOOK_PDF} || ${INPUT_GITBOOK_EPUB} || ${INPUT_GITBOOK_MOBI} ; then
if [ -n "${INPUT_FONT_INSTALL}" ]; then
apt-get update
apt-get install sudo -y
print_info "Message:Runing user's font_install"
${INPUT_FONT_INSTALL}
#sudo apt-get install fonts-noto-cjk
#sudo apt-get install ttf-mscorefonts-installer
else
print_warning "3308:Not install any font, maybe affect the pdf/mobi/epub, can add font_install at book.json"
fi
fi


# gitbook pdf
if ${INPUT_GITBOOK_PDF} ; then
Expand All @@ -753,6 +764,8 @@ print_info "STEP5 push_to_pages"
echo "--------------------------------------------"

# remove cname in source or source2
# why? maybe use other src_repo which we can't control whether it has cname before
# if need cname please add at config
if [ -f local_source/_book/CNAME ]; then
rm -rf local_source/_book/CNAME;
if [ $? -eq 0 ]; then
Expand All @@ -766,19 +779,24 @@ fi

######################### For publish #########################################

# if branch is new, need pubilsh_dir(if not /)
if [ ! -d "./local_publish/${INPUT_PUBLISH_DIR}" ] ; then
mkdir ./local_publish/${INPUT_PUBLISH_DIR}
fi

# need or not gitbook build history
### need or not gitbook build history
# I want use this config setting keep build history to get different time https://github.com/ZanderZhao/gitbook-action/issues/1
# But I fine it can't work, because some plugins still use the time when the action git clone src
# So I use https://github.com/Dream4ever/Knowledge-Base/issues/69 replace it like 'source_edit_time'
if [ ${INPUT_PUBLISH_REMOVE_LAST_BUILD} = "true" ] ; then
cd local_publish
git rm -rf --ignore-unmatch ./${INPUT_PUBLISH_DIR}/*
# git rm -rf --ignore-unmatch ./${INPUT_PUBLISH_DIR}/.*
cd ..
fi

# if branch is new, need pubilsh_dir(if not /) or rm by this action before
# this is below INPUT_PUBLISH_REMOVE_LAST_BUILD -> https://github.com/ZanderZhao/gitbook-action/issues/5
if [ ! -d "./local_publish/${INPUT_PUBLISH_DIR}" ] ; then
mkdir ./local_publish/${INPUT_PUBLISH_DIR}
fi

# move build file to each publish dir
cp -rfp local_source/_book/. local_publish/${INPUT_PUBLISH_DIR}

Expand All @@ -793,6 +811,7 @@ if [ "${INPUT_PUBLISH_CNAME}" != "null" ]; then # CNAME
fi
fi

# may push sucessfully, but built fail, try use this option in config -> https://github.com/ZanderZhao/gitbook-action/issues/4
if [ "${INPUT_PUBLISH_NOJEKYLL}" != "false" ]; then # add .nojekyll
touch .nojekyll
if [ $? -eq 0 ]; then
Expand All @@ -801,29 +820,30 @@ if [ "${INPUT_PUBLISH_NOJEKYLL}" != "false" ]; then # add .nojekyll
fi

# git config
# maybe someone need different name for publish
git config --local user.name ${PUBLISH_GIT_NAME}
git config --local user.email ${PUBLISH_GIT_EMAIL}

# git commit
# need or not publish commit history
if [ ${INPUT_PUBLISH_COMMIT_HISTORY} = "false" ] ; then # Clean commit history
# https://stackoverflow.com/a/26000395
# Checkout
# https://stackoverflow.com/a/26000395
# Checkout
git checkout --orphan latest_branch
# Add all the files
# Add all the files
git add .
# Commit the changes
# Commit the changes
git commit -am "${PUBLISH_COMMIT_MESSAGE}"
# Delete the branch
# Delete the branch
git branch -D ${INPUT_PUBLISH_BRANCH}
# Rename the current branch
# Rename the current branch
git branch -m ${INPUT_PUBLISH_BRANCH}
# Finally, force update your repository
# git push -f origin master
# --------
# rm -rf ./.git
# git init
# git checkout -b ${INPUT_PUBLISH_BRANCH}
# Finally, force update your repository
# git push -f origin master
# --------
# rm -rf ./.git
# git init
# git checkout -b ${INPUT_PUBLISH_BRANCH}
print_info "Message:Clean commit history success"
else
git add .
Expand All @@ -842,7 +862,6 @@ elif [ ${INPUT_PUBLISH_PUSH_FORCE} = "true" ] ; then # try push force
print_warning "3306:Push force success"
else
print_error "3106:Can't push publish_repo/branch, maybe not add publish_token(with access) in gitbook_action.yml"

fi
else
print_error "3107:Can't push publish_repo/branch, maybe not add publish_token(with access) in gitbook_action.yml or try set publish_push_force true"
Expand All @@ -851,15 +870,12 @@ cd ..


######################### For publish2 #########################################
# why publish2 exists ? maybe someone need publish for different place, eg gitlib.
# The following is the same with above except if.
# Can use loop, but for different function later(like backup), I'm not
# Can use loop, but for different function later(like backup), I'm not now

if [ ${INPUT_PUBLISH2_REPO} != "null" ]; then

if [ ! -d "./local_publish2/${INPUT_PUBLISH2_DIR}" ]; then
mkdir ./local_publish2/${INPUT_PUBLISH2_DIR}
fi

# need or not gitbook build history
if [ ${INPUT_PUBLISH2_REMOVE_LAST_BUILD} = "true" ] ; then
cd local_publish2
Expand All @@ -868,6 +884,10 @@ if [ ${INPUT_PUBLISH2_REPO} != "null" ]; then
cd ..
fi

if [ ! -d "./local_publish2/${INPUT_PUBLISH2_DIR}" ]; then
mkdir ./local_publish2/${INPUT_PUBLISH2_DIR}
fi

cp -rfp local_source/_book/. local_publish2/${INPUT_PUBLISH2_DIR}
cd local_publish2

Expand Down Expand Up @@ -922,9 +942,7 @@ fi

if [ ${INPUT_PUBLISH3_REPO} != "null" ]; then

if [ ! -d "./local_publish3/${INPUT_PUBLISH3_DIR}" ]; then
mkdir ./local_publish3/${INPUT_PUBLISH3_DIR}
fi


# need or not gitbook build history
if [ ${INPUT_PUBLISH3_REMOVE_LAST_BUILD} = "true" ] ; then
Expand All @@ -934,6 +952,10 @@ if [ ${INPUT_PUBLISH3_REPO} != "null" ]; then
cd ..
fi

if [ ! -d "./local_publish3/${INPUT_PUBLISH3_DIR}" ]; then
mkdir ./local_publish3/${INPUT_PUBLISH3_DIR}
fi

cp -rfp local_source/_book/. local_publish3/${INPUT_PUBLISH3_DIR}
cd local_publish3

Expand Down Expand Up @@ -985,7 +1007,8 @@ if [ ${INPUT_PUBLISH3_REPO} != "null" ]; then
cd ..
fi

# TODO prepare for other gitbook action (if two or more gitbook-action in one action)(local_source exists), but at same time remove the plugins file (node_modules) for cache, maybe can remove to one place
# TODO prepare for other gitbook action (if two or more gitbook-action in one action)(local_source exists),
# but at same time remove the plugins file (node_modules) for cache, maybe can remove to one place

if [ "${INPUT_NOT_CLEAN}" = "false" ]; then
rm -rf local_source
Expand Down
Loading

0 comments on commit 6fbbd8d

Please sign in to comment.