Run local executable file on remote servers with Capistrano - capistrano

I have a pretty long .sh file in my Capistrano tools directory that is intended to be executed on servers, not locally. Is there a simple way to execute this file on servers inside a Capistrano task without manually copying it on each server?

Assuming that the script is in your repository, you can create a custom task which allows you to do exactly that. Something like:
namespace :deploy do
desc 'Run custom command'
task :run_custom_command do
on roles(:app) do
# Your restart mechanism here, for example:
execute release_path.join('tools/command.sh')
end
end
before :publishing, :run_custom_command
end

Related

Azure DevOps ssh script file task

I'm just wondering if it's possible to use a script file in the SSH task in the Releases that will be populated with the environment variables from azure.
Script:
TEST=$(test)
I saved this script as an artifact, successfully download it, and select this script in the SSH task as a file, but the problem is the environment variables is not unwrapped, does someone have some approach?
If I put this same script as an inline script, then it's working. But if I chose script file then not.
I want to have this script in the git repo, so I can easily edit the script.
Does someone have this working?
I'm just wondering if it's possible to use a script file in the SSH task in the Releases that will be populated with the environment variables from azure
I am afraid there is no such out of way to use a script file in the SSH task to populated with the environment variables from azure.
As workaround, we could use the task Replace Tokens to update the value in the script file:
The format of variable in .sh file is #{TestVar}#.
Hope this helps.

Azure DevOps: How can I run powershell on onPrem servers after deployment of all IIS Sites

How can I run a powershell script after all stages have completed deployment? I have currently selected a deployment group job but am not 100% sure if this is what I need. I have included the script as part of the solution that is being deployed so that it will be available on all machines. Based on what I can find in the UI there seem to be 2 tasks that could work.
The first option would be to execute the task "Powershell Script" but it is asking for a path in the drop directory. The problem with this is that the file that I am interested in is in a zip file and there does not seem to be a way to specify a file in the zip file.
The other task I see is "PowerShell on Target Machines" and then it asks for a list of target machines. I am not sure what needs to be entered here as I want to run the powershell script on the current machine in the deploy group. It seems like this task was intended to run powershell scripts from the deployment machine to another remote machine. As a result this option does not seem like it fits my use case.
From looking the answers that I have come across talk about how to do this as part of an Azure site using something called "Kudu" (not relevant) or don't answer my other questions related to these tasks or seem like they are out of date.
A deployment group job will run on all of the servers specified in that deployment group. Based on what you have indicated, it sounds like that is what you are looking for.
Since you indicated that the file in question is a zip, you are actually going to need to use 2 separate tasks.
Extract Files - use this to extract the zip file so that you can execute the script
Powershell script - use this to execute the script. You can set the working directory for the script to execute in if necessary (under advanced options). Also remember that you don't have to use the file/folder selector 'helper' as it wont work in your case if the file is inside a zip. This is just used to populate the text box which you can manually do starting with the $(System.DefaultWorkingDirectory) variable and adding the necessary path of the script.

How to run a capistrano task within a whenever task?

I have whenever gem setup properly. How can I run that capistrano from my whenever schedule.rb?
my schedule.rb
every 1.minute, roles: [:app] do
# how to run here a capistrano task
# invoke 'my_app:test'
end
My capistrano task:
namespace :my_app do
desc 'test'
task :test do
on roles(:web) do
puts "the task runs"
end
end
end
Or should I move that task into a rake task. And should I run that rake task within whenever and capistrano?
Jan, you may find documentation very useful https://github.com/javan/whenever. The code example below - I just copied and edited it from the doc.
schedule.rb
# run this task only on servers with the :app role in Capistrano
# see Capistrano roles section below
every :day, at: '12:20am', roles: [:web] do
rake "app_server:task"
end
lib/tasks/test_task.rb
namespace :my_app do
desc 'test'
task :test do
puts "the task runs"
end
end
I believe it's easier to create Rake task and run it via Whenever. You can choose the Role, so basically you don't need to use Capistrano task (I believe you want to use it just because of Role).
I'd suggest your latter option, moving the logic to a rake task and executing it from both whenever and Capistrano. It'll be vastly easier and cleaner to do.

Powershell execution in Octopus

I have a powershell file test.ps1 which takes 3 parameters. I am trying to call/execute this file from a script step/task in octopus.
Can I do this, if I can how to achieve this.
I tried a few things to achieve this.
Use a batch file (test.bat) powershell E:\somefolder\test.ps1 %1 %2 %3 to call the below in the script task in octopus to execute this E:\somefolder\test.bat p1 p2 p3
The above does not work and says cmdlet does not identify the command.
Use the ps1 file itself i.e. call E:\somefolder\test.ps1 p1 p2 p3 to call in the script task in octopus.
This gives the same error as above.
I changed the code a little bit to use param([string]$param1, [string]$param2, [string]$param3) and then I get the parameter cannot be found.
Can someone please help me with this issue.
In Octopus Deploy you can use a "Run Script" step to execute scripts. This step is designed to execute PowerShell on the deployment target, which removes the need to find ways of kicking of the script. It also means that the step becomes part of the deployment process and you can end the deployment if it fails.
In the nextversion of Octopus Deploy (v3.3) you can have your PowerShell script in source control, or you can edit the script in the step itself. You have access to project variables and Octopus system variables in the script.
In previous versions, only the option to edit the script within the step was available.
Go to your Project's process in Ocotpus Deploy and...
Click "Add step"
Select "Run a Script"
Give it a name and enter the PowerShell script
Add any conditions
Click "Save"

Stop Copy Task immediately if one file was not copied successfully

Just imagine regular Deploy Target which copies thousand files to the remote network folder using MSBuild Copy Task, I believe pretty common scenario. So when folder is not accessible or there are some access privilegies problems - obviously Copy Task would not be able to copy files, but it will try to copy each file anyway, I want to prevent this to speed up Deploy Target for this case and report Failed status immediately and do not wait 30-60 mins whilst it process all files in the queue...
How to force MSBuild Copy Task to stop immediately in case when a file was not copied successfully and do not try to copy all other files?
If this is not possible using Copy Task perhaps this could be achieved using other facilities?
You can use Exec task instead. Like this:
<Exec Command="xcopy /s "from with spaces" $(WebDeployFolder)\$(WebDeployName)"/>
Would it not be better to use robocopy to copy, its has a plenty of options for similar things. See the task in the extension pack:
http://www.msbuildextensionpack.com/help/4.0.4.0/index.html
The options property of the task takes a number of parameters:
http://technet.microsoft.com/en-us/library/cc733145%28WS.10%29.aspx