Azure Devops handle special character in yaml - azure-devops

I have problem with the Azure Devops yaml script, as it doesn't pick up my variable properly to build my ReactJS project. Below is the script, but somehow the build failed at git push, and the username does't get picked up
pool:
vmImage: 'Ubuntu 16.04'
steps:
- task: NodeTool#0
inputs:
versionSpec: '9.8.0'
displayName: 'Install Node.js'
- script: |
npm install --no-save
npm run build
git push https://"$(azure.Username)":$(azure.Password)#$(azure.AppName).scm.azurewebsites.net:443/$(azure.AppName).git master
displayName: 'npm install and build'

My solution as per the yml file below, and you can set the variable to have "$" sign in it. Another better way is to use the steps in Azure DevOps itself.
image: node:9.8.0
clone:
depth: full
pipelines:
default:
- step:
script:
- npm install --no-save
- npm run build
- git push https://$AZURE_LOGIN:$AZURE_PASSWORD#$AZURE_APP_NAME.scm.azurewebsites.net:443/$AZURE_APP_NAME.git master

The correct answer is to wrap the variable in single quotes as such: '$(myVariable)'. This prevents the yaml parser from parsing the contents of the variable

Related

How to run a pipeline on new PRs only if files in a certain directory are changed

I have a repository with 2 directories, one with python code and one with C code.
I want to run one pipeline on all PRs only when the files in the python folder (hello_app) change.
I have used the following yaml file but the pipeline still runs when a new PR contains changes (only) outside of the hello_app directory:
trigger:
- none
pr:
branches:
include:
- master
paths:
exclude:
- '*'
include:
- hello_app/*
pool:
vmImage: 'ubuntu-latest'
strategy:
matrix:
Python27:
python.version: '2.7'
Python36:
python.version: '3.6'
steps:
- task: UsePythonVersion#0
inputs:
versionSpec: '$(python.version)'
displayName: 'Use Python $(python.version)'
- script: |
python -m pip install --upgrade pip
pip install -r requirements.txt
displayName: 'Install dependencies'
- script: |
python -m pip install flake8
flake8 .
displayName: 'Run linter tests'
- script: |
pip install pytest pytest-azurepipelines
pytest
displayName: 'pytest'
I tried to search online, but seems like this should work. Is there something wrong with the yaml I am using?
Please refer to this Doc:
YAML PR triggers are supported only in GitHub and Bitbucket Cloud. If
you use Azure Repos Git, you can configure a branch policy for build
validation to trigger your build pipeline for validation.
If your are using the Azure Repo, you need to configure a branch policy for build validation to trigger your build pipeline for validation.
You could navigate to branch policy -> build validation and set the path filter(/hello_app/*).
Here is my example:
Then it could work as expected.

How to manually add "script" in an Azure CI Pipeline?

I have a node.js/typescript/angular project in BitBucket that I want to create a build (CI) pipeline for it on Azure Devops. I used the classic editor to create the pipeline (reason below) and am trying to add the following task(s)/step(s):
npm install #types/node#8.10.52
ng build (ng is angular)
If was to use the YAML configuration, the resulting YAML file looks like the following file below, so how do i create a "script" manually after the Node task i have in classic editor? I only have options to add "npm" as a task, which is why i have added 3 npm tasks as shown in image above with 3 separate custom commands to mimic the steps configuration in the YML file below. Is that the way to do it with custom command?
YAML file npm/angular representation via YAML configuration:
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
steps:
- task: NodeTool#0
inputs:
versionSpec: '10.x'
displayName: 'Install Node.js'
- script: |
npm install -g #angular/cli
npm install
ng build --prod
displayName: 'npm install and build'
Reason why Im using classic editor:
When i tried saving the YAML configuration pipeline, i got a "Error from bitbucket: something went wrong" error, which appears to be a write-permission issue based on what i found from Atlassian forums.
So i ended up just using the classic pipeline editor, and this way i was able to select a specific branch (i.e. dev) instead of master (prod) branch.
The way I've handled this is to add a script to your package.json:
"scripts": {
"ng": "ng",
"build": "ng build",
"build-prod": "ng build --configuration=production"
"build-dev": "ng build --configuration=dev"
},
...
Then, you just call run-script from the custom NPM task:
Or you could optionally on the task just call run-script build --prod since you can pass arguments on the task.
These same steps are available in YAML, it would look something like this:
steps:
- task: NodeTool#0
inputs:
versionSpec: '10.x'
displayName: 'Install node.js'
- task: Npm#1
inputs:
command: 'install'
displayName: npm install
- task: Npm#1
inputs:
command: 'custom'
customCommand: 'run-script build --prod'
displayName: 'npm build'

Where do the builds go after the pipeline is run in Azure DevOps?

I'm very new to Azure DevOps. I'm running npm run build in the pipeline.
I'm wonder where the dist folder goes? How do I get access to it for further processing?
The build completes without error.
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
steps:
- task: NodeTool#0
inputs:
versionSpec: '10.x'
displayName: 'Install Node.js'
- script: |
npm install
npm run build
displayName: 'npm install and build'
In the agent you have 3 folders: a for artifacts, s for sources and b for binaries.
When the build start all the code downloaded to the s folder, so if you run npm run build the dist folder created there.
How do you access it? there are environment variables for all the folders, to the s folder the variable is $(Agent.SourcesDirectory), so you can take the dist from there in another task by $(Agent.SourcesDirectory)/Your App/dist (or more deeper, depend your app structure).
You can find here the list of the environment variables.

Azure pipelines yaml permission denied

I'm getting an error when trying to deploy using azure pipelines.
Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
I think its becuase the node_modules folder is not being shared between stages. But I cant figure out what is proper way to do it.
Here is my yaml file:
variables:
- group: netlify
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
stages:
- stage: Build
jobs:
- job: ARM
steps:
- task: NodeTool#0
inputs:
versionSpec: '10.x'
displayName: 'Install Node.js'
- script: |
npm install
npm run unit
displayName: 'Setup and test'
- script: npm run build
- publish: $(System.DefaultWorkingDirectory)
artifact: dist
- stage: Deploy
dependsOn: Build
condition: succeeded()
jobs:
- job: APP
steps:
- bash: |
npm i -g netlify-cli
netlify deploy --site $(NETLIFY_SITE_ID) --auth $(NETLIFY_AUTH_TOKEN) --prod
After running npm install, package node_modules should appear somehwere in the directory but it seems its not properly shared.
You are using Ubuntu image, and trying to global install netlify-cli in Linux without sudo.
If the Ubuntu is the necessary system you must use, you'd better add sudo before this command:
sudo npm i -g netlify-cli
Command succeed on my pipeline
In this doc, Upgrading on *nix (OSX, Linux, etc.):
You may need to prefix these commands with sudo, especially on Linux,
or OS X if you installed Node using its default installer.
Same in VSTS, you must use sudo in the command to let you has password-less sudo rights for Ubuntu.
Another way is change the image to vs2017-win2016 if you do not has any special requirements for the build environment:
pool:
vmImage: 'vs2017-win2016'
When using this image, you could install anything and do not need use sudo.
In fact, we has been pre-installed many basic tools in all hosted images, including node.js
In our github description, we listed all tools that pre-installed for all images. You can check to know more about VSTS.

Trying to publish a build to Nuget.org

I'm new to Azure DevOps; however, what I'm doing seems like it should be straightforward: I simply want to compile a project and publish to NuGet.org. I'm hitting that many barriers to doing it that I feel that I'm probably mis-using the tool.
I have a build which looks like this:
trigger:
- master
pool:
vmImage: 'windows-2019'
variables:
buildConfiguration: 'Release'
steps:
- script: dotnet build --configuration $(buildConfiguration) /property:Version=$(Build.BuildNumber)
displayName: 'dotnet build $(buildConfiguration)'
- task: PublishBuildArtifacts#1
inputs:
pathtoPublish: '$(Build.ArtifactStagingDirectory)'
artifactName: 'drop'
This successfully builds. However, I do get the following warning:
##[warning]Directory 'd:\a\1\a' is empty. Nothing will be added to build artifact 'drop'.
When I try to deploy, I'm getting errors - the latest of which is:
No files matched the search pattern.
In dotnet pack, here's the release step for pack:
steps:
- task: DotNetCoreCLI#2
displayName: 'dotnet pack'
inputs:
command: pack
packagesToPack: '$(System.DefaultWorkingDirectory)/name.project'
This is what's currently failing, but the next step is intended to publish to NuGet (for completeness - and incase there's an easier way to do all this):
steps:
- task: DotNetCoreCLI#2
displayName: 'dotnet pack'
inputs:
command: pack
packagesToPack: '$(System.DefaultWorkingDirectory)/name.project'
The release has an artifact, which I've set up as the output from the build (I think); however, I get the error from this:
No version is available for name.project or the latest version has
no artifacts to publish. Please check the source pipeline.
My understanding of how this should work in general is that the Build should hold the steps involved in producing the binaries, etc, whereas the Release should be any steps involved in deploying the built. So, I should be able to take a 'Build' and 'Release' it several times to multiple locations. I feel like this understanding is not in-keeping with the errors that I'm seeing.
Is my understanding correct?
What could I be doing wrong here and, more importantly, what are the methods of diagnosing issues with this?
Trying to publish a build to Nuget.org
For the first error, that because you are mising the Copy Files task before using the Publish Build Artifacts task.
Check the Publish Build Artifacts task, you can view this task is used to publish build artifacts to Azure Pipelines, TFS, or a file share.
But after build the project/solution, the output are stored on the build agent, rather than the artifacts. So we need to add a copy task before PublishBuildArtifacts to copy the files from output to the artifacts:
steps:
- task: CopyFiles#2
displayName: 'Copy Files to: $(build.artifactstagingdirectory)'
inputs:
SourceFolder: '$(system.defaultworkingdirectory)'
TargetFolder: '$(build.artifactstagingdirectory)'
enabled: false
For the second error, you should specify the project in the repos instead of System.DefaultWorkingDirectory, where is use to build the project, change the Path to csproj in the repos:
steps:
- task: DotNetCoreCLI#2
displayName: 'dotnet pack'
inputs:
command: pack
packagesToPack: NetCoreDemo/NetCoreDemo/NetCoreDemo.csproj
Hope this helps.
You man need to provide the --output argument for dotnet build. Add the following
--output $(Build.ArtifactStagingDirectory)
so the command should look something like this.
dotnet build --configuration $(buildConfiguration) /property:Version=$(Build.BuildNumber) --output $(Build.ArtifactStagingDirectory)
this will put the output into the Artifact Staging Directory (d:\a\1\a)
which will include the *.nupkg file.
I am assuming you are using the new CSProj format and you are supplying the properties for the NuGet package and have the <GeneratePackageOnBuild>true</GeneratePackageOnBuild> property already set. If so the *.nupkg file should be in the output directory that you define in the dotnet build command. Then you don't need to use dotnet pack. In the release definition you just need to use the NuGet task and use push command to push the package to your NuGet Feed.
Use this blog post as reference. It does not have the full details since its focusing only on few aspects of the entire process. but it should be helpful to get the build aspect correct.