借助GitHub Actions部署Go+ClojureScript全栈应用:实现后端服务启动与前端页面托管的技术咨询
Hey there! Let's work through this together—you've already nailed the build parts for both your front-end JS bundle and Go binary, so we just need to connect the dots to get your Go server running and serving that front-end public/index.html file. Here's a step-by-step breakdown:
First, Quick Code Adjustment (If You Haven’t Done This Yet)
Your Go server needs to know how to serve static files from the public directory. Add this to your Go entry file (like main.go) to set up static file hosting:
package main import ( "net/http" ) func main() { // Serve all files from the public directory at the root path fileServer := http.FileServer(http.Dir("./public")) http.Handle("/", fileServer) // Add your API routes here if you have any // http.HandleFunc("/api/your-endpoint", yourAPIFunction) // Start the server on port 8080 (you can change this if needed) println("Server running on http://localhost:8080") if err := http.ListenAndServe(":8080", nil); err != nil { panic(err) } }
Now, Update Your GitHub Actions YAML
Your current workflow splits front-end and Go builds into separate jobs, but they aren’t connected. We’ll modify the workflow to share the built front-end assets with the Go job, then test or deploy the server. Here’s the updated deploy.yaml:
# This is a basic workflow to help you get started with Actions name: CI/CD for Full-Stack App # 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: jobs: # First, build the front-end assets and save them as a workflow artifact build-frontend: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set Node.js 16.x uses: actions/setup-node@v3 with: node-version: 16.x - name: Run yarn install uses: borales/actions-yarn@v4 with: cmd: install - 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: cli: latest - name: Generate JS bundle uses: borales/actions-yarn@v4 with: cmd: run build # Upload the built public folder so the Go job can access it - name: Upload front-end assets uses: actions/upload-artifact@v3 with: name: frontend-assets path: public/ # Build the Go server, combine it with front-end assets, and test/deploy build-and-run-go-server: runs-on: ubuntu-latest needs: build-frontend # Wait for front-end build to finish first strategy: matrix: go: ["1.20.3"] name: Build & Test Go ${{ matrix.go }} Server steps: - uses: actions/checkout@v3 - name: Setup Go uses: actions/setup-go@v4 with: go-version: ${{ matrix.go }} # Download the front-end assets we saved earlier - name: Download front-end assets uses: actions/download-artifact@v3 with: name: frontend-assets path: public/ # Build the Go binary with a clear name - name: Build Go server binary run: go build -o free-riding-server # Optional: Test if the server starts and serves the front-end - name: Test server functionality run: | ./free-riding-server & # Start server in background sleep 5 # Give it time to initialize curl -I http://localhost:8080 # Check if index.html is accessible
Key Changes Explained
- Connected Jobs: The
build-and-run-go-serverjob usesneeds: build-frontendto wait for the front-end to finish building, so it can access the latest assets. - Shared Assets: We use
actions/upload-artifactandactions/download-artifactto pass thepublicfolder between jobs—this ensures the Go server has the latest front-end files. - Test Step: The optional test step starts the server and checks if it can serve the index page, so you can catch issues early in the workflow.
Next Steps for Production Deployment
If you want to deploy this to a production environment, you’ll need to add a deployment step based on your hosting platform:
- For a self-hosted server: Use
scpor a deployment tool to upload the Go binary andpublicfolder, then restart the server. - For platforms like Heroku/Vercel: Add their official GitHub Actions to deploy the built assets and server.
备注:内容来源于stack exchange,提问作者BuddhiLW

