Configure yaml in azure devops to run groovy script on Mac - groovy

Earlier I was using JenkinsFile to run CI/CD pipeline in jenkins, but now we're migrating to Azure DevOps. So to build a pipeline in Azure DevOps on Mac, I'm using a Yaml file.
In jenkinsfile, I ran groovy script using the following syntax:
pipe = load ('path/to/groovy/script')
pipe.go()
,where "go()" is a function in the groovy script
But, I'm unable to configure the yaml file in similar way
What I found online was running this groovy via gradle build
I want to configure the yaml to run groovy script like in jenkinsfile, as in, without installing gradle or any third party.

Configure yaml in azure devops to run groovy script
If you are using private agent, you have to install on the agent machine:
Java 8 JDK
Apache Groovy 2.5.7 (Downloaded as zip and extracted to some local
folder)
Then set environment variables, open CMD and run these commands:
setx JAVA_HOME "C:\Program Files\Java\jdk1.8.0_101"
setx /M PATH "%PATH%;C:\Program Files\Java\jdk1.8.0_101\bin"
setx GROOVY_HOME "C:\Users\<UserName>\Desktop\apache-groovy-sdk-2.5.7" (the first path is when you extracted the Apache Groovy 2.5.7)
setx /M PATH "%PATH%;C:\Users\<UserName>\Desktop\apache-groovy-sdk-2.5.7\bin" (the first path is when you extracted the Apache Groovy 2.5.7 )
Now, we could run groovy scripts without installing gradle or any third party during the building, in the build definition add Command Line Task (not Bash) and choose your groovy script:
If you are using the hosted agent, you need more steps to download and unzip task Apache Groovy 2.5.7.
Hope this helps.

Related

Azure DevOps Pipeline unable to locate executable file: 'terraform'

I´m trying to setup a pipeline in Azure DevOps which initiates a Azure Resource Group. The configurations for which are saved in a .tf file in the DevOps repository.
The pipeline was created with the classic editor. Following tasks were added to the job agent in the same order: terraform init (Terraform CLI) and Build Project (.NET Core).
Terraform is already installed (file path was added to environment variables).
I´m really new to this and am trying to do my first steps. So any help would be appreciated. Also, you can tell me if any important information is missing.
This is the job agent´s log for the Terraform init task:
2023-01-19T15:15:19.3880770Z ##[section]Starting: terraform init
2023-01-19T15:15:19.3895740Z ==============================================================================
2023-01-19T15:15:19.3896080Z Task : Terraform CLI
2023-01-19T15:15:19.3896240Z Description : Execute terraform cli commands
2023-01-19T15:15:19.3896440Z Version : 0.7.8
2023-01-19T15:15:19.3896590Z Author : Charles Zipp
2023-01-19T15:15:19.3896770Z Help :
2023-01-19T15:15:19.3896890Z ==============================================================================
2023-01-19T15:15:21.3070520Z ##[error]Error: Unable to locate executable file: 'terraform'. 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.
2023-01-19T15:15:21.3186320Z ##[error]Error: Unable to locate executable file: 'terraform'. 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.
2023-01-19T15:15:21.5201550Z ##[section]Finishing: terraform init
I have tried running this in various Backend Type settings. Also have tried to change the Agent specifications multiple times.
Furthermore have I tried runnning this after putting the terraform.exe in the repository root.
My expection was that the pipeline creates a new resource group, but the task won´t even be executed.
It looks like you're using the Azure Pipelines Terraform Tasks extension for Azure DevOps which also includes an installer task for Terraform called "TerraformInstaller". Try adding that to your pipeline before "terraform init" to ensure that Terraform is installed (by default it will install the latest version unless you give it a specific one) and is correct for the agent's OS.
As to why the existing terraform binary isn't being found my guess is that you've been trying to use the windows version of terraform (terraform.exe) instead of the linux version which is just terraform and that the pipeline is running on a linux agent. I'm guessing this because the output of the task says that it can't find the file named terraform without the .exe extension.
Problem solved. Two solutions:
Add Terraform install task into the pipeline before adding other Terraform tasks. You can find useful links for this in the approved answer above.
Run pipeline with Ubuntu (any version I guess) agent (Pipeline -> Agent Specification -> ubuntu-latest).
I prefer the second solution because the ubuntu agent finished the tasks significantly faster than the Microsoft agent (and not because of the added install task which only took 7 seconds).
For comparison:
Ubuntu agent: 1 min 16 sec (first execution; the executions afterwards ~30 sec total)
Windows agent: 4 min 31 sec

Buildkite Windows agent - hook scripts location

Does anybody know where I should place my buildkite global hook scripts on a windows agent machine?
I place it in the hooks folder as specified in the buildkite-agent.cfg ... But it does not work.
It works just fine when using an Ubuntu agent!
Hooks support was added to the Windows release in Buildkite Agent version 3, which is currently in beta and available from the GitHub releases page. You need to download the latest beta.
You might also need to add .bat to the end of your hook file name, like ...\hooks\environment.bat.
To export your environment variables make sure you use set:
# ...\hooks\environment.bat
set FOO="bar"
Now in your job batch files or scripts you should be able to use the variable:
echo "%FOO%"
# outputs "bar"

How to auto-generate deploy.cmd in new Azure CLI?

I'm following this guide to create a web app with a custom deploy.cmd file. The article suggests that I can get a copy of the current deploy.cmd file (which I'll then modify) using the following command:
azure site deploymentscript --python
Unfortunately, when I install the Azure CLI using the MSI linked in the article, there is no azure binary on my path. I do have az -- is this a newer version of the same CLI? -- but I can't find an equivalent deployment script generation command for that executable.
I found a deploy.cmd file using Kudu (under D:\home\site\deployments\tools) but am not sure if that's the appropriate file to use. Can anyone suggest the right Azure CLI command for deployment script generation, or confirm that the deploy.cmd file I found is the right one to modify? Thanks in advance!
Based on my knowledge, there is not an equivalent to azure site deploymentscript in azure cli(2.0). So, you could not do deploy custom script with Azure CLI 2.0.
You had better know the difference between Azure cli 2.0(az) with Azure cli 1.0(azure).
Azure CLI 2.0: Our next-generation CLI written in Python, for use with
the Resource Manager deployment model.
Azure CLI 1.0: Our CLI written in Node.js, for use with both the
classic and Resource Managerdeployment models.
For your scenario, if you could install Azure ClI 1.0, you could refer to this link to install Azure CLI 1.0.
Instead of using the command line to generate a starter deployment script, there is an alternative approach that is often easier:
Deploy your repo without any deployment scripts.
Go to the site's Kudu Console.
From the Tools menu, choose 'Download deployment script'. You'll get a zip with a .deployment and deploy.cmd files.
Commit both files at the root of your repo
Tweak them as needed
More information please refer to this link.
You can use kuduscript to generate the deployment script.
npm install -g kuduscript
kuduscript --python
Here is the list of options
Options:
-h, --help output usage information
-V, --version output the version number
-r, --repositoryRoot [dir path] The root path for the repository (default: .)
--aspWAP <projectFilePath> Create a deployment script for .NET web application, specify the project file path
--aspNetCore <projectFilePath> Create a deployment script for ASP.NET Core web application, specify the project file path
--aspWebSite Create a deployment script for basic website
--go Create a deployment script for Go website
--node Create a deployment script for node.js website
--ruby Create a deployment script for ruby website
--php Create a deployment script for php website
--python Create a deployment script for python website
--functionApp [projectFilePath] Create a deployment script for function App, specify the project file path if using msbuild
--basic Create a deployment script for any other website
--dotNetConsole <projectFilePath> Create a deployment script for .NET console application, specify the project file path
-s, --solutionFile <file path> The solution file path (sln)
-p, --sitePath <directory path> The path to the site being deployed (default: same as repositoryRoot)
-t, --scriptType <batch|bash|posh> The script output type (default: batch)
-o, --outputPath <output path> The path to output generated script (default: same as repository root)
-y, --suppressPrompt Suppresses prompting to confirm you want to overwrite an existing destination file.
--no-dot-deployment Do not generate the .deployment file.
--no-solution Do not require a solution file path (only for --aspWAP otherwise ignored).

Run groovy scripts for jenkins in IDE

I wanted to run groovy scripts in my IDE instead of Jenkins Script console. is this possible? If so where i can download http://javadoc.jenkins-ci.org/ API so that i can put that in classpath.
I cannot refer hudson.model.* and other classes with jenkins CLI jar
There is a webpage that explains how to configure Eclipse on how to Write Groovy scripts for Jenkins with code completion
Short version:
create a new maven project,
add jenkins dependencies,
update settings.xml to include jenkins repo,
add Groovy support to Eclipse(optional),
convert project to groovy and you're good to go.
BTW, for Eclipse Mars, the Groovy plugin is here: http://dist.springsource.org/snapshot/GRECLIPSE/e4.5/

Run PreSync/PostSync commands via WPP deploy.cmd

I'm trying to figure out how to run a pre/post command using the deploy.cmd generated by VS/MSBuild. I understand there are pre/postsync commands which can be set on the command line with msbuild but this is fixed within the web deploy package inside of the x.deploy.cmd.
How do I go about customizing the output of this file so that I can run the deploy command with specific parameters?
The intention is a non-developer will pick up the package zip file and import the application into IIS. We use IIS to host some windows services and so to be able to deploy we need to stop and uninstall the service before deployment and then install restart in the post deploy stage.
For certain servers we allow auto deployments from TFS and hook this pre/post command using the .targets file of the msbuild WPP pipeline. However, we want to this to be available to the manual deploy command files.
PreSync/PostSync are features of the msdeploy command line and are not supported by the package/manifest providers, or even the API. They are equivalent to running msdeploy a second time, so there's no way you'll be able to include their functionality while directly importing the package into IIS.
I'd recommend having a batch/powershell file on the server that the user runs after copying the package into the same directory.
The .cmd file that MSBuild generates is boilerplate script that you can simply change to call your pre/post powershell scripts. Just overwrite the one generated by the build with your custom one.

Resources