在Azure Web App上自定义Golang启动命令

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

Custom golang startup command on Azure Web App

问题

我正在尝试使用Github Actions将一个Go Web应用部署到Azure App Services。整个部署过程都成功,直到需要使用azure/webapps-deploy@v2部署应用的那一步。

为了查看问题出在哪里,我创建了一个简单的Go 'Hello world'测试应用程序。只部署这个非常简单的应用程序是可以工作的。然而,在尝试部署测试应用程序时,我注意到了一些问题:

  • 应用程序在Azure上完全重新构建,而不是使用可执行文件运行。我之前的部署文件如下所示:
name: Go deployment

on:
  push:
    branches: ["master"]
  pull_request:
    branches: ["master"]

jobs:
  build:
    runs-on: ubuntu-latest
    environment: production
    steps:
    # checkout the repo 
    - uses: actions/checkout@master

    # setup Go
    - name: Setup Go
      uses: actions/setup-go@v3
      with:
        go-version: '1.20'

    - run: go version

    # install dependencies
    - name: go build
      working-directory: .
      run: |
                go build
      
    - name: Upload artifact for deployment job
      uses: actions/upload-artifact@v3
      with:
        name: go-app
        path: .

  deploy:
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: 'Production'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v3
        with:
          name: go-app
          
      - name: 'Deploy to Azure Web App'
        id: deploy-to-webapp
        uses: azure/webapps-deploy@v2
        with:
          app-name: ${{ env.AZURE_WEBAPP_NAME }}
          slot-name: 'Production'
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE }}
          package: .

这样部署没有问题。代码库应用程序被提供给Azure Web App。当我尝试在最后一步中使用可执行文件时,部署失败。当然,Azure Web App有一个自定义字段用于设置启动命令。我尝试将其设置为./main,以便在启动时只运行可执行文件,但仍然失败。

        with:
          app-name: ${{ env.AZURE_WEBAPP_NAME }}
          slot-name: 'Production'
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE }}
          package: main

在Azure Web App上自定义Golang启动命令

在我的本地机器上使用go build main.go构建Go应用程序,然后执行./main,这个应用程序可以正常运行。

  • 由于我无法在上一步中执行可执行文件,我决定回退,让Azure App Service按原样执行Go应用程序。如果是这样,整个构建步骤就不再需要了,只需将代码推送到Azure Web Service即可。如下所示:
name: Go

on:
  push:
    branches: ["master"]
  pull_request:
    branches: ["master"]

jobs:
  build:
    runs-on: ubuntu-latest
    environment:
      name: 'Production'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
    steps:
    # checkout the repo 
    - uses: actions/checkout@master

    - name: 'Deploy to Azure Web App'
      id: deploy-to-webapp
      uses: azure/webapps-deploy@v2
      with:
        app-name: ${{ env.AZURE_WEBAPP_NAME }}
        slot-name: 'Production'
        publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE }}
        package: .

尽管需要推送整个代码库,但这个方法也可以正常运行。然而,在我们的生产应用程序中,main.go文件由于结构原因不位于根目录下。为了模拟这种行为,我将main.go文件放在了/cmd目录下。然后,Azure Web App的部署失败。可以猜到,这可能是因为Azure找不到main.go文件。我尝试再次使用启动命令,但这次是go run cmd/main.go。不幸的是,这也不起作用。
在Azure Web App上自定义Golang启动命令

Azure Web Apps在运行流水线时显示了它正在构建的所有内容:在Azure Web App上自定义Golang启动命令

有什么建议吗?我在这里漏掉了什么吗?

有没有办法在之前的步骤中上传创建的可执行文件到Azure Web App,并在那里运行这个可执行文件?

英文:

I'm trying to deploy a Go webapp with Github actions to Azure App Services. The whole deployment succeeds until the point that the app needs to be deployed with azure/webapps-deploy@v2.

在Azure Web App上自定义Golang启动命令

To see where the problems raise I created just a simple Go 'Hello world' test app. Just deploying this really simple app works. However, while experimenting with deploying the test app I noticed a few things:

  • The app is completely rebuilt on Azure instead of using the executable file to run. My previous deployment file looked like this:
name: Go deployment

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

jobs:
  build:
    runs-on: ubuntu-latest
    environment: production
    steps:
    # checkout the repo 
    - uses: actions/checkout@master

    # setup Go
    - name: Setup Go
      uses: actions/setup-go@v3
      with:
       go-version: '1.20'

    - run: go version

    # install dependencies
    - name: go build
      working-directory: .
      run: |
       go build
      
    - name: Upload artifact for deployment job
      uses: actions/upload-artifact@v3
      with:
        name: go-app
        path: .

  deploy:
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: 'Production'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v3
        with:
          name: go-app
          
      - name: 'Deploy to Azure Web App'
        id: deploy-to-webapp
        uses: azure/webapps-deploy@v2
        with:
          app-name: ${{ env.AZURE_WEBAPP_NAME }}
          slot-name: 'Production'
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE }}
          package: .

This deploys without a problem. The codebase app is fed to the Azure Web App. When I try to use the executable in the last step, the deployment fails. Of course Azure Web App has a custom field for setting a startup command. I tried setting this to ./main to just run the executable at startup but this still fails.

        with:
          app-name: ${{ env.AZURE_WEBAPP_NAME }}
          slot-name: 'Production'
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE }}
          package: main

在Azure Web App上自定义Golang启动命令

When building the Go app on my local machine with go build main.go and then executing ./main this app runs without a problem.

  • Because I couldn't just execute the executable in previous step I decided to roll back and just let Azure App Service execute the Go app as is. If so, the whole building step isn't necessary anymore and it's just fine to push the code to the Azure Web Service. Like so:
name: Go

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

jobs:
  build:
    runs-on: ubuntu-latest
    environment:
      name: 'Production'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
    steps:
    # checkout the repo 
    - uses: actions/checkout@master

    - name: 'Deploy to Azure Web App'
      id: deploy-to-webapp
      uses: azure/webapps-deploy@v2
      with:
        app-name: ${{ env.AZURE_WEBAPP_NAME }}
        slot-name: 'Production'
        publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE }}
        package: .

Despite having to push the whole code base, this again runs beautifully. However in our production app the main.go file is for structural reasons not located in the root dir. To imitate this behaviour I placed the main.go file under a /cmd directory. And again the deployment of Azure Web App fails. As can be guessed, this is probably due to Azure not able to find the main.go file. I thought to use the startup command again but this time with go run cmd/main.go. Sadly, this also does not work.
在Azure Web App上自定义Golang启动命令

Azure Web Apps shows everything that is it building while running the pipeline: 在Azure Web App上自定义Golang启动命令

Any suggestions? Things I'm missing here?

Any solution on how I would be able to just upload the created executable in a previous step to Azure Web App and run this executable there?

答案1

得分: 1

首先,您应该在Azure Web App中设置一个环境变量:WEBSITE_RUN_FROM_PACKAGE1。这将防止在Azure上再次运行构建。从此时开始,您应该能够上传预构建的可执行文件。

我还必须设置启动命令来运行我的特定可执行文件。
在Azure Web App上自定义Golang启动命令

在执行此操作后,我在https://APPNAME-HERE.scm.azurewebsites.net/api/logstream的日志中看到以下内容:

2023-04-26T17:20:12.596331026Z 检测平台...

2023-04-26T17:20:12.805572634Z 在源目录中未检测到任何平台。

2023-04-26T17:20:15.792565274Z 正在运行 /home/site/wwwroot/go-test

2023-04-26T17:20:15.928193597Z /home/site/wwwroot/go-test: /lib/x86_64-linux-gnu/libc.so.6: version 'GLIBC_2.32' not found (required by /home/site/wwwroot/go-test)

2023-04-26T17:20:15.934491135Z /home/site/wwwroot/go-test: /lib/x86_64-linux-gnu/libc.so.6: version 'GLIBC_2.34' not found (required by /home/site/wwwroot/go-test)

版本GLIBC_2.34之所以出现是因为该应用程序是在使用ubuntu-latest的流水线中构建的。这是Ubuntu-22.04,它具有GLIBC_2.35,但用于运行的Azure机器上没有此版本。使用ubuntu-20.04以版本GLIBC_2.31进行构建,它将完美运行。

英文:

First you should set in Azure Web App an environment variable: WEBSITE_RUN_FROM_PACKAGE to 1. This prevents running the build on Azure again. From this moment on you should be able to upload pre-built executables.

I also had to set the startup command for running my specific executable.
在Azure Web App上自定义Golang启动命令

After doing this I saw the following in the logs on https://APPNAME-HERE.scm.azurewebsites.net/api/logstream

2023-04-26T17:20:12.596331026Z Detecting platforms...

2023-04-26T17:20:12.805572634Z Could not detect any platform in the source directory.

2023-04-26T17:20:15.792565274Z Running /home/site/wwwroot/go-test now

2023-04-26T17:20:15.928193597Z /home/site/wwwroot/go-test: /lib/x86_64-linux-gnu/libc.so.6: version 'GLIBC_2.32' not found (required by /home/site/wwwroot/go-test)

2023-04-26T17:20:15.934491135Z /home/site/wwwroot/go-test: /lib/x86_64-linux-gnu/libc.so.6: version 'GLIBC_2.34' not found (required by /home/site/wwwroot/go-test)

The version GLIBC_2.34 happens because the app was built in pipeline with ubuntu-latest. This is Ubuntu-22.04 which has GLIBC_2.35 but the Azure machine to run on does not have this version. Use ubuntu-20.04 to built with version GLIBC_2.31 and it will run perfect.

huangapple
  • 本文由 发表于 2023年4月26日 08:23:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76106323.html
匿名

发表评论

匿名网友

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

确定