Background: According to the operation and maintenance requirements, when the Web front-end project releases the formal environment, the developer needs to create a tag in the Git project repository, and inform the operation and maintenance personnel of the address and tag of the project to be released by email. Finally, the operation and maintenance personnel complete the deployment and go online with Jenkins.
To meet the above requirements and avoid manual tag creation, a simple script file deploy.sh is created. A developer can execute NPM run deploy tag name locally to create a tag based on the current branch and upload it to a remote Git repository.
PS: This process is relatively simple and rough, only to throw a brick to attract jade
1. Create deploy.sh in the root directory of the local project
Sh contains the following information
#! /usr/bin/env sh
Make sure the script throws any errors it encounters
set -e
# check whether the tag is empty
if [ $# -eq 0 ];
then
echo 'please input tag name! '
exit
fi
Verify that the tag format is correct
TAGPATTERN="^final_v[1-9]\.[0-9]\.[0-9]$"
if [[ "The $1"= ~$TAGPATTERN]].then :
else
echo "tag format is invalid!"
exit
fi
Generate static files
npm run build
Dist directory exists
if [ ! -d "dist" ]; then
echo 'dist folder cannot found! '
exit
fi
Create a tag and upload it to the remote Git repository
git tag The $1
git push -f origin The $1
Copy the code
The script content has some constraints:
- The tag name cannot be empty
- The tag name must match the final_vx.x format
- Dist must exist in the root directory. Remove the dist ignore item in the. Gitignore
2. Add the deploy attribute to the package.json file scripts
3. Run NPM run deploy final_v1.1.1 to create a tag named final_v1.1.1
Macbook-pro: Autotag Kuiper $NPM run deploy Final_v1.1.1
> [email protected] deploy/Users/kuiper/autotag
> bash deploy.sh "Final_v1. 1.1"
/Users/kuiper/autotag
Total 0 (delta 0), reused 0 (delta 0)
To http://gitlab.skysri.com/kuiper/autotag.git
* [new tag] final_v1.1.1 -> final_v1.1.1
Copy the code
With this tag, the remote repository can notify o&M of the release of the official environment
Learn Webhooks, Jenkins, Docker, Jest and other techniques to achieve a more complete and standardized CI/CD