Home  >  Article  >  Backend Development  >  Custom golang launch command on Azure Web App

Custom golang launch command on Azure Web App

王林
王林forward
2024-02-09 12:03:081008browse

Azure Web App 上的自定义 golang 启动命令

#php editor Strawberry will introduce to you the custom golang startup command on Azure Web App today. Azure Web App is a managed cloud service that helps developers easily deploy and scale web applications. Golang is an efficient programming language that is fast, reliable and concise. By customizing golang startup commands, developers can better control the startup process of web applications and achieve more personalized functions. This article will introduce in detail how to configure and use customized golang startup commands on Azure Web App to help developers make better use of this feature.

Question content

I am trying to deploy a go web application with github actions to azure app service. The entire deployment succeeds until the application needs to be deployed using azure/webapps-deploy@v2.

To see where the problem lies, I created a simple go 'hello world' test application. Just deploy this very simple application and you're good to go. However, while trying to deploy the test application, I noticed something:

  • The application is completely rebuilt on azure rather than using an executable 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: .

There is no problem with this deployment. Codebase applications are fed into Azure Web Apps. 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 the launch command. I tried setting it to ./main to run the executable on startup, but this still failed.

with:
          app-name: ${{ env.azure_webapp_name }}
          slot-name: 'production'
          publish-profile: ${{ secrets.azureappservice_publishprofile }}
          package: main

When building the go application on my local machine using go build main.go and then executing ./main, the application runs without issue.

  • Because I couldn't just execute the executable from the previous step, I decided to roll back and let Azure App Service execute the go app as-is. If so, the whole build step is no longer needed and you can just push the code to an azure web service. like this:
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 entire codebase, it still works great. However, in our production application, due to structural reasons, the main.go file is not located in the root directory. To mimic this behavior, I placed the main.go file in the /cmd directory. Deployment of azure web app failed again. One can guess that this may be due to azure not being able to find the main.go file. I want to use the start command again, but this time run cmd/main.go using go. Sadly, this doesn't work either.

azure web apps displays everything built when running the pipeline:

Any suggestions? What am I missing here?

Is there any solution on how to upload the executable created in the previous step to azure web app and run the executable there?

Workaround

First, you should set an environment variable in azure web app: website_run_from_package to 1. This prevents the build from running on Azure again. From this moment on, you should be able to upload pre-built executables.

I also had to set up the launch command to run my specific executable.

After doing this, I see the following in the logs for 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 appears because the application was built in the pipeline using ubuntu-latest. This is ubuntu-22.04, which has glibc_2.35 but the azure machine I want to run it on doesn't have this version. Building with ubuntu-20.04 version glibc_2.31 works perfectly.

The above is the detailed content of Custom golang launch command on Azure Web App. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete