push & tag & release #2
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
name: push & tag & release | |
on: | |
workflow_dispatch: | |
permissions: write-all | |
jobs: | |
release: | |
runs-on: ubuntu-22.04 | |
steps: | |
- name: Configure | |
run: | | |
git config --global user.name ${{ vars.SDK_USERNAME }} | |
git config --global user.email ${{ vars.SDK_EMAIL }} | |
- name: Pull source | |
uses: actions/checkout@v4 | |
with: | |
github-server-url: https://gitee.com | |
repository: ${{ vars.SOURCE_REPO }} | |
token: ${{ secrets.GITEE_TOKEN }} | |
ref: 'master-dev' | |
path: source | |
- name: Pull target | |
uses: actions/checkout@v3 | |
with: | |
ssh-key: ${{ secrets.SSH_KEY }} | |
path: target | |
- name: Push & Tag & Release | |
shell: bash | |
run: | | |
version=$(cat source/VERSION) | |
echo "version ${version} found." | |
echo 'ls' | |
ls -al target | |
echo 'ls -al source' | |
ls -al source | |
# Organize the content | |
#find target/ -maxdepth 1 -mindepth 1 | grep -Ev '*git*' | xargs -I '{}' rm -rf '{}' | |
find source/ -maxdepth 1 -mindepth 1 | grep -Ev '*git*|.cid|.codecheck|buildscript' | xargs -I '{}' cp -a '{}' target/ | |
echo 'or' | |
cd target | |
echo '-----BEGIN CONTENTS IN CURRENT DIR-----' | |
ls -a | |
echo '-----END CONTENTS IN CURRENT DIR-----' | |
git add -A | |
# Exit if there is nothing to commit | |
if [[ "$(git status | grep 'nothing to commit')" ]]; then echo 'nothing to commit, skip' && exit 0; fi | |
# Determine whether it is daily synchronization or a new version release | |
tag_name="v${version}" | |
tag_exists=false | |
for tag in $(git tag --list) | |
do | |
if [[ "$tag" == "$tag_name" ]]; then tag_exists=true && break; fi | |
done | |
echo "tag_exists=${tag_exists}" | |
if [[ "$tag_exists" == "true" ]] | |
then | |
current_time=$(date) | |
git commit -m "synchronized at ${current_time}" | |
git push origin main | |
echo "daily synchronization finished." | |
else | |
commit_msg="release ${version} source code" | |
git commit -m "$commit_msg" | |
git tag -a "$tag_name" -m "$commit_msg" | |
git push origin main "$tag_name" | |
echo "push tag ${tag_name} success." | |
curl -L \ | |
-X POST \ | |
-H 'Accept: application/vnd.github+json' \ | |
-H 'content-type: application/json' \ | |
-H "Authorization: Bearer ${github_token}" \ | |
-H 'X-GitHub-Api-Version: 2022-11-28' \ | |
-d "{\"tag_name\":\"${tag_name}\",\"name\":\"${tag_name}\",\"body\":\"Release ${version}\"}" \ | |
-L https://api.github.com/repos/${github_repo}/releases || exit 1 | |
echo "create release ${version} success." | |
if [ -n "$publish_workflows" ] | |
then | |
workflow_file=$(basename "$github_workflow") | |
IFS=',' read -ra workflows <<< "$publish_workflows" | |
for workflow in "${workflows[@]}" | |
do | |
if [[ "$workflow" == "$workflow_file" ]] | |
then | |
echo "workflow ${workflow} is same as the current, skip" | |
continue | |
fi | |
curl -L \ | |
-X POST \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "Authorization: Bearer ${github_token}" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
-d "{\"ref\":\"${tag_name}\"}" \ | |
-L "https://api.github.com/repos/${github_repo}/actions/workflows/${workflow}/dispatches" | |
echo "workflow triggered: ${workflow}" | |
done | |
fi | |
fi | |
env: | |
github_repo: ${{ github.repository }} | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
github_workflow: ${{ github.event.workflow }} | |
publish_workflows: ${{ vars.PUBLISH_WORKFLOWS }} |