使用Github Actions部署全栈应用程序(Go + ClojureScript)

huangapple go评论75阅读模式
英文:

Deploying fullstack app with Github Actions (Go + ClojureScript)

问题

我能够构建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文件的状态:

# 这是一个基本的工作流,帮助您开始使用Actions

name: CI

# 控制工作流何时运行
on:
  # 在“main”分支上触发工作流,推送或拉取请求事件
  push:
    branches: ["main"]
  pull_request:
    branches: ["main"]

  # 允许您从Actions选项卡手动运行此工作流
  workflow_dispatch:

# 工作流运行由一个或多个作业组成,可以按顺序或并行运行
jobs:
  # 此工作流包含一个名为“build”的作业
  build:
    # 作业将在其上运行的运行程序类型
    runs-on: ubuntu-latest

    # 步骤表示将作为作业的一部分执行的任务序列
    steps:
      # 检出存储库到$GITHUB_WORKSPACE,以便作业可以访问它
      - uses: actions/checkout@v3

      - name: 设置Node.js 16.x
        uses: actions/setup-node@v3
        with:
          node-version: 16.x

      - name: 运行安装
        uses: borales/actions-yarn@v4
        with:
          cmd: install # 将运行`yarn install`命令

      - name: 安装Lynx
        uses: awalsh128/cache-apt-pkgs-action@latest
        with:
          packages: lynx
          version: 1.0

      - name: 准备Java
        uses: actions/setup-java@v3
        with:
          distribution: "zulu"
          java-version: "17"

      - name: 安装Clojure工具
        uses: DeLaGuardo/setup-clojure@11.0
        with:
          # 安装一个或同时安装所有
          # 值必须指示工具的特定版本,或使用'latest'始终提供最新版本
          cli: latest # 基于tools.deps的Clojure CLI

      - name: 生成js捆绑包
        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 }} 示例
    steps:
      - uses: actions/checkout@v3
      - name: 设置go
        uses: actions/setup-go@v4
        with:
          go-version: ${{ matrix.go }}
      - run: |
                    go build

构建过程的当前状态:
使用Github Actions部署全栈应用程序(Go + ClojureScript)

英文:

I was able to build both the JavaScript bundle, and the Go binary. But, I'm currently unable, due to lack of knowledge, to make the Go server run for the backend, and to display the public/index.html as the entry for the front-end.

How could I accomplish that, adding to the existing yaml file?

The repository: https://github.com/BuddhiLW/free-riding.
The yaml file: https://github.com/BuddhiLW/free-riding/blob/main/.github/workflows/deploy.yaml

Current state of the Yaml file:

# 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/setup-clojure@11.0
        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

Current status of the build process:
使用Github Actions部署全栈应用程序(Go + ClojureScript)

答案1

得分: 1

你想将软件部署到哪里?你不能在流水线中运行一个 Go 服务器来提供生产环境的网站(一小时后就会消失)。由于你似乎只是使用 Go 来运行服务器,我建议你尝试在其他地方部署。例如,使用 Google Firebase(用于提供静态文件,如编译后的 cljs)。对于 Firebase,可以按照以下步骤进行操作:

  1. https://firebase.google.com/ 上创建一个账号并添加一个项目和令牌。
  2. 创建一个 firebase.json 文件,内容如下:
{
  "hosting": {
    "public": "resources/public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}
  1. package.json 中运行 npm i firebase 安装 Firebase。
  2. 添加一个操作,在推送到主分支(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 }}
  1. 在 GitHub 的 Secrets 中添加一个名为 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;'

default.conf 的内容如下:

server {
    listen 8080;
    root /usr/share/nginx/html;
    index index.html index.htm;
    location / {
        try_files $uri /index.html;
    }
}

希望对你有帮助!

英文:

Where are you trying to deploy the software to? You cannot run a go server in a pipeline to serve a website for production purposes (for one, after an hour it's gone). Since you seem to use go only to run a server, I would maybe try to deploy somewhere else? For example, Google Firebase (to serve static files, like the compiled cljs)?

For Firebase looks like this:

Make an account add https://firebase.google.com/ and a project + token.

firebase.json

{
"hosting": {
"public": "resources/public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}

Run npm i firebase to install firebase in package.json.

Add an action to deploy to Firebase on pushes to main/master:

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 }}

Add FIREBASE_TOKEN in a GitHub secret.

For serving static files somewhere else than Firebase I would advise using the web server NGINX. Make an NGINX Docker container and host that container somewhere (e.g. a cloud provider). Dockerfile will look something like this:

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;'

default.conf:

server {
listen 8080;
root /usr/share/nginx/html;
index index.html index.htm;
location / {
try_files $uri /index.html;
}
}

huangapple
  • 本文由 发表于 2023年6月30日 22:56:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76590076.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定