Azure Devops Pipeline Agent can not connect to my network using OpenVPN anymore

Denny Pradipta 0 Reputation points
2025-04-16T08:41:53.7766667+00:00

I have this simple pipeline to deploy my web app using Dokku, using an OpenVPN in my Azure Pipeline agent. I am not using the self hosted agent, in case you need to know. Here is the part of the azure-pipelines.yml:

- stage: 'DeployToStaging'

    displayName: 'Deploy to Staging'

    condition: eq(variables['Build.SourceBranch'], 'refs/heads/main')

    jobs:

      - job: DeployDashboard

        steps:

          - task: InstallSSHKey@0

            inputs:

              knownHostsEntry: $(known_host_entry)

              sshPublicKey: $(ssh_public_key)

              sshKeySecureFile: $(ssh_private_key)

              addEntryToConfig: true

              configHostAlias: staging

              configHostName: $(dokku_staging_ip)

              configUser: $(server_user)

            env:

              PUBLIC_KEY: $(ssh_public_key)

            displayName: 'Add SSH Fingerprint'

          # Build and push Docker image

          # Needs separate step because build arguments are not supported in Docker@2 buildAndPush

          - task: Docker@2

            displayName: 'Build docker image'

            inputs:

              containerRegistry: $(containerRegistry)

              command: 'build'

              Dockerfile: './Dockerfile'

              repository: $(repository)

              arguments: $(stagingBuildArguments)

              tags: |

                $(Build.BuildId)-rc

                test

          - task: Docker@2

            displayName: 'Push docker image'

            inputs:

              containerRegistry: $(containerRegistry)

              command: 'push'

              repository: $(repository)

              tags: |

                $(Build.BuildId)-rc

                test

          # Install OpenVPN

          - script: |

              sudo apt-get update -y

              sudo apt-get install openvpn -y

            displayName: 'Install OpenVPN'

          - task: DownloadSecureFile@1

            name: ovpn_config

            displayName: 'Download OpenVPN Config'

            inputs:

              secureFile: 'dashboard.ovpn'

          - task: DownloadSecureFile@1

            name: ovpn_auth

            displayName: 'Download OpenVPN Password'

            inputs:

              secureFile: 'openvpn_auth.txt'

          # Deploy

          - script: |

              # Start OpenVPN in the background and capture its PID

              nohup sudo openvpn --config "$(ovpn_config.secureFilePath)" --askpass "$(ovpn_auth.secureFilePath)" > /dev/null 2>&1 &

              VPN_PID=$!

              # Wait for VPN connection to establish (check tunnel interface)

              echo "Waiting for VPN connection..."

              timeout=10  # Adjust timeout as needed

              while [[ $timeout -gt 0 ]]; do

                  if ip a | grep -q 'tun0'; then  # Replace 'tun0' with your actual interface if different

                      echo "VPN connected."

                      break

                  fi

                  sleep 1

                  ((timeout--))

              done

              if [[ $timeout -eq 0 ]]; then

                  echo "Failed to establish VPN connection."

                  exit 1

              fi

              # Deploy

              ssh staging dokku git:from-image dashboard $(image_name):$(Build.BuildId)-rc

              ssh staging docker image prune -f

              # Stop OpenVPN

              kill $VPN_PID

It was working fine until April 9th.

User's image Afterwards, it doesn't run anything at all.

User's image

I though to myself, maybe I need to upgrade the OpenVPN to OpenVPN3. So I did:

  - stage: 'DeployToStaging'
    displayName: 'Deploy to Staging'
    condition: eq(variables['Build.SourceBranch'], 'refs/heads/main')
    jobs:
      - job: DeployDashboard
        steps:
          - task: InstallSSHKey@0
            inputs:
              knownHostsEntry: $(known_host_entry)
              sshPublicKey: $(ssh_public_key)
              sshKeySecureFile: $(ssh_private_key)
              addEntryToConfig: true
              configHostAlias: staging
              configHostName: $(dokku_staging_ip)
              configUser: $(server_user)
            env:
              PUBLIC_KEY: $(ssh_public_key)
            displayName: 'Add SSH Fingerprint'

          # Build and push Docker image
          # Needs separate step because build arguments are not supported in Docker@2 buildAndPush
          - task: Docker@2
            displayName: 'Build docker image'
            inputs:
              containerRegistry: $(containerRegistry)
              command: 'build'
              Dockerfile: './Dockerfile'
              repository: $(repository)
              arguments: $(stagingBuildArguments)
              tags: |
                $(Build.BuildId)-rc
                test
          - task: Docker@2
            displayName: 'Push docker image'
            inputs:
              containerRegistry: $(containerRegistry)
              command: 'push'
              repository: $(repository)
              tags: |
                $(Build.BuildId)-rc
                test

          # Install OpenVPN
          - script: |
              sudo mkdir -p /etc/apt/keyrings && curl -fsSL https://packages.openvpn.net/packages-repo.gpg | sudo tee /etc/apt/keyrings/openvpn.asc
              DISTRO=$(lsb_release -c -s)
              echo "deb [signed-by=/etc/apt/keyrings/openvpn.asc] https://packages.openvpn.net/openvpn3/debian $DISTRO main" | sudo tee /etc/apt/sources.list.d/openvpn-packages.list
              sudo apt-get update -y
              sudo apt-get install openvpn3 -y
            displayName: 'Install OpenVPN'
          - task: DownloadSecureFile@1
            name: ovpn_config
            displayName: 'Download OpenVPN Config'
            inputs:
              secureFile: 'dashboard.ovpn'
          - task: DownloadSecureFile@1
            name: ovpn_auth
            displayName: 'Download OpenVPN Password'
            inputs:
              secureFile: 'openvpn_auth.txt'

          # Deploy
          - script: |
              # Start OpenVPN in the background and capture its PID
              cat $(ovpn_auth.secureFilePath) | openvpn3 session-start --config "$(ovpn_config.secureFilePath)"

              # Deploy
              ssh staging git:from-image dashboard $(image_name):$(Build.BuildId)-rc
              ssh staging cleanup
              ssh staging docker-direct image prune -f

              # Stop OpenVPN
              openvpn3 session-manage --config $(ovpn_config.secureFilePath) --disconnect
            displayName: 'Deploy Dashboard'

All I got was connected, but the connection is closed.

User's image

Any ideas why?

Azure DevOps
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.