Hugo 作为静态网站生成器,是一个将 Markdown 转化为漂亮的静态网站的工具网站仍然使用 Hugo,托管在云负载平衡器后面的谷歌云存储(GCS)上。使用 Cloud Build 来生成和部署我的网站。在我仓库上推送 Git 会触发网站的构建,然后自动发布到 GCS 上。博客现在托管在 Cloud Run 上。Cloud Run是谷歌云平台(GCP)推出的全新无服务器托管服务。它基本上是 Knative 的托管版本,Knative 是一个基于 Istio 和 Kubernetes 的开源无服务器平台。
AWS S3 用于托管网站本身
Docker 用于运行 Hugo 并从的 Markdown 文件生成网站
FROM nginx:1.15此时,网站已经生成,我只需复制镜像中的文件即可。也许这里最有趣的是使用 envsubst 在容器启动时生成一个有效的 Nginx 配置文件。envsubst 是一个小型的 "模板化 "工具,它能将文件中的环境变量替换为其值。
ENV PORT=8080 \
ROBOTS_FILE=robots-prod.txt
ADD site.template /etc/nginx/site.template
ADD blog/public /usr/share/nginx/html/
ENTRYPOINT [ "/bin/bash", "-c", "envsubst '$PORT $HOST $ROBOTS_FILE' < /etc/nginx/site.template > /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'" ]
server {我使用了 3 个环境变量:
listen ${PORT};
error_page 404 /404.html;
if ( $http_x_forwarded_proto = "http" ) {
return 301 https://${HOST}$request_uri;
}
location / {
root /usr/share/nginx/html;
index index.html;
}
location /robots.txt {
alias /usr/share/nginx/html/${ROBOTS_FILE};
}
}
steps:在 Cloud Run 上部署
- id: 'Download academic theme'
name: 'gcr.io/cloud-builders/git'
args: ['submodule', 'update', '--init', '--recursive']
- id: 'Run Hugo'
name: 'mrtrustor/hugo:0.46'
args: ['--baseURL=https://blog.mrtrustor.net']
dir: 'blog'
- id: 'Build Image'
name: 'gcr.io/cloud-builders/docker'
args: ["build", "-t", "gcr.io/${PROJECT_ID}/blog{SHORT_SHA}", "."]
- id: 'Push Image'
name: 'gcr.io/cloud-builders/docker'
args: ["push", "gcr.io/${PROJECT_ID}/blog{SHORT_SHA}"]
steps:在 gcloud beta 运行部署命令中,有几个有趣的选项值得关注:
- id: 'Download academic theme'
name: 'gcr.io/cloud-builders/git'
args: ['submodule', 'update', '--init', '--recursive']
- id: 'Run Hugo'
name: 'mrtrustor/hugo:0.46'
args: ['--baseURL=https://blog.mrtrustor.net']
dir: 'blog'
- id: 'Build Image'
name: 'gcr.io/cloud-builders/docker'
args: ["build", "-t", "gcr.io/${PROJECT_ID}/blog{SHORT_SHA}", "."]
- id: 'Push Image'
name: 'gcr.io/cloud-builders/docker'
args: ["push", "gcr.io/${PROJECT_ID}/blog{SHORT_SHA}"]
- id: 'Deploy to Cloud Run'
name: 'gcr.io/cloud-builders/gcloud'
args: ['beta', 'run', 'deploy', 'blog', '--set-env-vars=HOST=blog.mrtrustor.net,ROBOTS_FILE=robots-prod.txt', '--image', 'gcr.io/${PROJECT_ID}/blog{SHORT_SHA}', '--allow-unauthenticated', '--region', 'us-central1']
images:
- "gcr.io/${PROJECT_ID}/blog{SHORT_SHA}"
如果您的阴影 JAR 包含所有依赖项,您可以在容器构建期间使用 Dockerfile 生成简单归档:复制代码
- <font size="3"><build>
- <finalName>helloworld</finalName>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-shade-plugin</artifactId>
- <configuration>
- <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
- <createDependencyReducedPom>true</createDependencyReducedPom>
- <filters>
- <filter>
- <artifact>*:*</artifact>
- <excludes>
- <exclude>META-INF/*.SF</exclude>
- <exclude>META-INF/*.DSA</exclude>
- <exclude>META-INF/*.RSA</exclude>
- </excludes>
- </filter>
- </filters>
- </configuration>
- <executions>
- <execution>
- <phase>package</phase>
- <goals><goal>shade</goal></goals>
- <configuration>
- <transformers>
- <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
- <resource>META-INF/spring.handlers</resource>
- </transformer>
- <transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
- <resource>META-INF/spring.factories</resource>
- </transformer>
- <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
- <resource>META-INF/spring.schemas</resource>
- </transformer>
- <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
- <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
- <mainClass>${mainClass}</mainClass>
- </transformer>
- </transformers>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </font>
减小线程栈大小复制代码
- <font size="3"># Use Docker's multi-stage build
- FROM eclipse-temurin:11-jre as APPCDS
- COPY target/helloworld.jar /helloworld.jar
- # Run the application, but with a custom trigger that exits immediately.
- # In this particular example, the application looks for the '--appcds' flag.
- # You can implement a similar flag in your own application.
- RUN java -XX:DumpLoadedClassList=classes.lst -jar helloworld.jar --appcds=true
- # From the captured list of classes (based on execution coverage),
- # generate the AppCDS archive file.
- RUN java -Xshare:dump -XX:SharedClassListFile=classes.lst -XX:SharedArchiveFile=appcds.jsa --class-path helloworld.jar
- FROM eclipse-temurin:11-jre
- # Copy both the JAR file and the AppCDS archive file to the runtime container.
- COPY --from=APPCDS /helloworld.jar /helloworld.jar
- COPY --from=APPCDS /appcds.jsa /appcds.jsa
- # Enable Application Class-Data sharing
- ENTRYPOINT java -Xshare:on -XX:SharedArchiveFile=appcds.jsa -jar helloworld.jar
- </font>
使用不在生产中使用的 Spring Boot 开发者工具复制代码
- <font size="3"><dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-indexer</artifactId>
- <optional>true</optional>
- </dependency>
- </font>
欢迎光临 qidao123.com技术社区-IT企服评测·应用市场 (https://dis.qidao123.com/) | Powered by Discuz! X3.4 |