linuxazure-devopsazure-pipelines-release-pipeline

Azure DevOps releases - how to properly perform a release pipeline (Linux)


I am trying to to perform a release of some build artifact on a self hosted Linux Ubuntu agent. I created a deployment group and installed the Azure agent on the Linux and added that single agent to the deployment group (so that way I can always perform the release to that specific server).

Now I've created a release pipeline and I want to add tasks that will actually deal with nginx and systemctl - stop the currently running API service, remove the old files, copy the newly extracted build artifact files into the same directory and then run the API service again using systemctl start MyApp.

However, on the very first step, I ran into an issue where I am unable to stop the current app by using systemctl stop MyApp.service because it fails:

2024-08-20T20:17:46.5603236Z systemctl stop MyApp.service
2024-08-20T20:17:46.5605104Z ========================== Starting Command Output ===========================
2024-08-20T20:17:46.5716903Z [command]/usr/bin/bash --noprofile --norc /agents/tfs/_work/_temp/4bd66e7e-d175-4b2e-8c97-d462550ed6dd.sh
2024-08-20T20:17:46.6313304Z Failed to stop MyApp.service: Interactive authentication required.
2024-08-20T20:17:46.6315936Z See system logs and 'systemctl status MyApp.service' for details.

When I try to do the same using sudo sudo systemctl stop MyApp.service), it fails as well:

2024-08-20T20:25:08.1783842Z sudo systemctl stop MyApp.service
2024-08-20T20:25:08.1784026Z ========================== Starting Command Output ===========================
2024-08-20T20:25:08.1828785Z [command]/usr/bin/bash --noprofile --norc /agents/tfs/_work/_temp/f4435275-bb73-4378-81a3-498a293b989b.sh
2024-08-20T20:25:08.1829490Z sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
2024-08-20T20:25:08.1829704Z sudo: a password is required

I do not know what the proper workaround for this is nor if I am taking the wrong approach for releasing / deploying the latest API version. Is there another approach that I can take or?

Azure DevOps release pipeline


Solution

  • Failed to stop MyApp.service: Interactive authentication required.

    When you run systemctl without sudo, you are scheduling a systemd job as your current user. The current user has no access to do the action.

    To solve this issue, we can add the sudo to run the command: sudo systemctl stop MyApp.service

    sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper

    I can reproduce the same issue when using linux deployment target.

    enter image description here

    The cause of the issue is that when we change to use sudo command, it needs to input the password of the current user account. But Pipeline doesn't support interactive input.

    To solve this issue, you need to add the current user which is used to run agent to the root group.

    sudo usermod -aG sudo tfs
    

    Then you can change to use the following command:

    echo "$(password)" | sudo  -S systemctl stop MyApp.service
    

    Note: You need to input the password of the current user account which is used to run Azure Agent.

    Result:

    After running command: systemctl status servicename, I can see the service has been stopped successfully.

    enter image description here