我能够构建 JavaScript 包 和 Go 二进制文件。但是,由于缺乏知识,我目前无法让 Go 服务器在后端运行,并显示
public/index.html
作为前端的入口。
我怎样才能做到这一点,添加到现有的 yaml 文件中?
存储库:https://github.com/BuddhiLW/free-riding。 yaml 文件:https://github.com/BuddhiLW/free-riding/blob/main/.github/workflows/deploy.yaml
Yaml 文件的当前状态:
# This is a basic workflow to help you get started with Actions
name: CI
# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the "main" branch
push:
branches: ["main"]
pull_request:
branches: ["main"]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3
- name: Set Node.js 16.x
uses: actions/setup-node@v3
with:
node-version: 16.x
- name: Run install
uses: borales/actions-yarn@v4
with:
cmd: install # will run `yarn install` command
- name: Install Lynx
uses: awalsh128/cache-apt-pkgs-action@latest
with:
packages: lynx
version: 1.0
- name: Prepare java
uses: actions/setup-java@v3
with:
distribution: "zulu"
java-version: "17"
- name: Install clojure tools
uses: DeLaGuardo/[email protected]
with:
# Install just one or all simultaneously
# The value must indicate a particular version of the tool, or use 'latest'
# to always provision the latest version
cli: latest # Clojure CLI based on tools.deps
- name: Generate js bundle
uses: borales/actions-yarn@v4
with:
cmd: run build
golang-server:
runs-on: ubuntu-latest
strategy:
matrix:
go: ["1.20.3"]
name: Go ${{ matrix.go }} sample
steps:
- uses: actions/checkout@v3
- name: Setup go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
- run: |
go build
您想将软件部署到哪里?您不能在管道中运行 go 服务器来为网站提供生产服务(例如,一小时后它就消失了)。由于您似乎只使用 go 来运行服务器,我可能会尝试部署在其他地方?例如,Google Firebase(提供静态文件,如编译后的 cljs)?
Firebase 看起来像这样:
创建一个帐户,添加 https://firebase.google.com/ 和一个项目 + 令牌。
firebase.json
{
"hosting": {
"public": "resources/public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
运行
npm i firebase
在 package.json
中安装 firebase。
添加一个操作,以在推送到 main/master 时部署到 Firebase:
name: Deploy to Firebase Hosting on push to master
on:
push:
branches: [master]
jobs:
test_build_and_deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Test
run: make test
- name: Build
run: make prod
- name: Deploy
uses: w9jds/firebase-action@master
with:
args: deploy --only hosting
env:
FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
在 GitHub 秘密中添加 FIREBASE_TOKEN。
为了在 Firebase 之外的其他地方提供静态文件,我建议使用 Web 服务器 NGINX。创建一个 NGINX Docker 容器并将该容器托管在某个地方(例如云提供商)。 Dockerfile 看起来像这样:
FROM nginxinc/nginx-unprivileged:1.23
COPY resources/public /usr/share/nginx/html
COPY nginx/default.template /etc/nginx/conf.d/default.conf
CMD nginx -g 'daemon off;'
默认.conf:
server {
listen 8080;
root /usr/share/nginx/html;
index index.html index.htm;
location / {
try_files $uri /index.html;
}
}