Skip to content

Instantly share code, notes, and snippets.

@xmsz
Last active December 28, 2021 09:30
Show Gist options
  • Save xmsz/da80c8f1e4ff7905015782ab4dd2ef16 to your computer and use it in GitHub Desktop.
Save xmsz/da80c8f1e4ff7905015782ab4dd2ef16 to your computer and use it in GitHub Desktop.
生成项目部署文件
#!/bin/sh
function createDockerIgnore() {
cp .gitignore .dockerignore
}
function createDeploySh() {
read -p "请输入dockerRegistry: " dockerRegistry
cat > deploy.sh <<EOF
VERSION=\`git rev-parse --short HEAD\`
DOCKER_NAME="$projectName"
DOCKER_REPO="$dockerRegistry"
docker build -t \${DOCKER_NAME}:\${VERSION} .
docker tag \${DOCKER_NAME}:\${VERSION} \${DOCKER_REPO}:\${VERSION}
docker push \${DOCKER_REPO}:\${VERSION}
EOF
}
function createNginxConf() {
cat > nginx.conf <<EOF
server
{
listen 80;
listen [::]:80;
root /usr/share/nginx/html;
index index.html index.htm;
location /
{
try_files \$uri \$uri/ /index.html;
}
# favicon.ico
location = /favicon.ico
{
log_not_found off;
access_log off;
}
# robots.txt
location = /robots.txt
{
log_not_found off;
access_log off;
}
# assets, media
location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$
{
expires 7d;
access_log off;
}
# svg, fonts
location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff2?)\$
{
add_header Access-Control-Allow-Origin "*";
expires 7d;
access_log off;
}
# gzip
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;
}
EOF
}
function CreateDockerFile() {
cat > Dockerfile <<EOF
FROM node:12 AS build
COPY . .
RUN npm i pnpm -g --registry=https://registry.npmmirror.com
RUN pnpm i --prefer-offline --shamefully-hoist --registry=https://registry.npmmirror.com
RUN npm run build
# 生成生产镜像
FROM nginx
COPY --from=build /build/web /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
ENV TZ="Asia/Shanghai"
EXPOSE 80
EOF
}
function createFrontendNormalProject() {
# 创建.dockerignore
createDockerIgnore
# 创建deploy.sh
createDeploySh
# 创建nginx
createNginxConf
# 创建dockerFile
CreateDockerFile
}
# 输入配置
read -p "请输入项目名: " projectName
# 选择项目类型
cat <<EOF
1) 普通前端应用
EOF
read -p "请选择项目类型: " projectType
case "$projectType" in
1) createFrontendNormalProject ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment