Azure DevOps self-hosted Pipeline Agent that can perform a docker build and push command

49 Views Asked by At

I'm trying to create a self-hosted Agent to build and deploy my code on AKS.

All my JAVA code is in an Azure DevOps git repository. I created a pipeline (you can take a look at the yaml at the bottom) that performs a gradle shadowJar command to build my code, then builds a docker image and pushes it to a docker hub registry and, in the end, it deploys my application on an AKS cluster.
I wanted to try using a self-hosted agent deployed on the same AKS cluster to run the pipeline in question as well.

I created the agent following the instructions on the Microsoft pages (https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/docker?view=azure-devops#linux, https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/docker?view=azure-devops#configure-secrets-and-deploy-a-replica-set) and succesfully deployed the agent.
At the beginning the pipeline kept failing due to the JAVA_HOME variable being missing. I resolved this adding the necessary jdk tar.gz (in this case jdk 17) to the repo and used JavaToolInstaller to set the JAVA_HOME variable. (Maybe it's not a great solution, but i solved the problem and for now my main goal was for it to work)

The main problem now is that the pipeline keeps failing with this error:

##\[error\]Unhandled: Unable to locate executable file: 'docker'. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also check the file mode to verify the file is executable.

I need docker to perform docker login / docker build and push commands and I can't find a solution to install docker in my self-hosted agent.Could you please help me with a solution?

Thank you for your help


My pipeline:

trigger:
  batch: false
  branches:
    include:
      - main
pool:
  name: test-observability-pool
stages:
  - stage: Build
    jobs:
      - job: Build
        displayName: Build and Publish to Docker Hub
        steps:
          - task: JavaToolInstaller@0
            inputs:
              versionSpec: "17"
              jdkArchitectureOption: x64
              jdkSourceOption: LocalDirectory
              jdkFile: jdk-17_linux-x64_bin.tar.gz
              jdkDestinationDirectory: /builds/binaries/externals
              cleanDestinationDirectory: true
          - task: Gradle@3
            displayName: Gradle Build
            inputs:
              gradleWrapperFile: gradlew
              tasks: shadowJar
              publishJUnitResults: false
              javaHomeOption: JDKVersion
              sonarQubeRunAnalysis: false
              spotBugsAnalysis: false
          - task: Docker@2
            displayName: Docker Login
            inputs:
              containerRegistry: Docker Hub Connection
              command: login
          - task: Docker@2
            displayName: Docker BuildAndPush
            inputs:
              containerRegistry: Docker Hub Connection
              repository: elettrogewi/kubepublisher-otel
              command: buildAndPush
              Dockerfile: "**/Dockerfile"
              tags: latest
  - stage: Deploy
    jobs:
      - job: Deploy
        displayName: Deploy on AKSObservability01
        steps:
          - task: Kubernetes@1
            inputs:
              connectionType: Kubernetes Service Connection
              kubernetesServiceEndpoint: aks-observability02
              namespace: default
              command: apply
              arguments: -f publisher-otel.yaml --force
              secretType: dockerRegistry
              containerRegistryType: Azure Container Registry
              outputFormat: yaml
1

There are 1 best solutions below

0
Miao Tian-MSFT On

You can add a DockerInstaller@0 task to install the Docker CLI on the agent machine before the docker login / docker build and push commands.

- task: DockerInstaller@0
  displayName: Docker Installer
  inputs:
    dockerVersion: 17.09.0-ce
    releaseType: stable