如何将 helmfile 实施到我的 azure 管道中?

How to implement helmfile into my azure pipeline?

我已经创建了一个节点应用程序,我想为其自动部署。到目前为止,我只自动化了构建过程,即安装依赖项、创建 docker 图像并推送到 Azure 容器注册表。这工作得很好,对应于下面没有注释掉的代码。 在目前的过程中我还需要

  1. 手动更改我的 helmfile 配置中的图像标签和
  2. 手动执行 helmfile -environment=<my-environment> sync

我的挣扎在第二部分,因为我相信当我对第二部分进行设置时,第一部分很容易实现。

在存储库的源目录中,我有 helmfile.yaml 可以在构建后立即调用。这是我试图通过以下注释设置实现的。我的想法是有一个已经安装了 helfmile 的容器,例如cablespaghetti/helmfile-docker,然后使用 Azure kubectl 任务连接到 K8s 集群,然后执行 helmfile sync 命令。这种方法失败了,因为我得到了 Docker exec fail with exit code 1,可能是因为指定的容器使用了 ENTRYPOINT 方法,这在 Azure Pipeline 中是不允许的。

然而,这种方法感觉有些麻烦,好像我缺少一种更简单的方法来 'simply' 执行 helmfile sync 命令。我怎样才能让它工作?

trigger:
- dev

resources:
- repo: self

variables:
  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: <service-connection>
  imageRepository: <node-app-image>
  containerRegistry: <registry-name>
  dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
  tag: '$(Build.BuildId)'
  
  # Agent VM image name
  vmImageName: 'ubuntu-latest'

  ## This was an approach I tried but failed, because the below image is not suitable
  # helmfileImageName: 'cablespaghetti/helmfile-docker:3.3.4.1'
  # azureSubscriptionEndpoint: <endpoint>
  # azureResourceGroup: <resource-group>
  # kubernetesCluster: <cluster-name>
  # stage: <stage>

stages:
- stage: Build
  displayName: Build and push stage
  jobs:  
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: NodeTool@0
      inputs:
        versionSpec: '13.x'
      displayName: 'Install Node.js'
    - script: |
        yarn install
        yarn run build
      displayName: 'yarn install'
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        tags: |
          $(tag)
## Attempted deploy stage - still missing here is using the above $(tag) to select the correct image
# - stage: Deploy
#   displayName: Deploy with helmfile
#   jobs:  
#   - job: Deploy
#     displayName: Deploy
#     container: cablespaghetti/helmfile-docker:3.3.4.1
#     steps:
#     - task: Kubernetes@1
#       displayName: kubectl login
#       inputs:
#         connectionType: Azure Resource Manager
#         azureSubscriptionEndpoint: $(azureSubscriptionEndpoint)
#         azureResourceGroup: $(azureResourceGroup)
#         kubernetesCluster: $(kubernetesCluster)
#         useClusterAdmin: $(useClusterAdmin)
#         command: login
#     - script: |
#         helmfile -environment=$(stage) sync

因为 helmfile 没有预装在管道代理中。您可以使用脚本任务手动安装 helmfile。检查以下示例:

- bash: |
   
   curl -L https://github.com/roboll/helmfile/releases/download/v0.135.0/helmfile_linux_amd64 > helmfile
    
   chmod +x helmfile
     
  displayName: 'Install helm'

以上 bash 任务将在默认工作目录中安装 hemlfile(您可以更改文件路径以将其安装到不同的文件夹)。然后你可以在下面的脚本任务中使用像这样 ./helmfile -environment=<my-environment> sync 的 helmfile 命令。见下文:

- bash: |
   az login --service-principal -u "$(AZURE_SERVICE_PRINCIPAL_USER)" -p "$(AZURE_SERVICE_PRINCIPAL_PW_OR_CERT)" --tenant "$(AZURE_SERVICE_PRINCIPAL_TENANT)"
   az aks get-credentials --resource-group "$(AZURE_RESOURCE_GROUP)" --name "$(CLUSTER_NAME)"
       
   ./helmfile -environment=<my-environment> sync

  displayName: 'helmfile  sync'