Set working directory of a project in mono repo in Azure Devops - azure-devops

My project is using microservices and in one repos we have multiple applications in Azure DevOps.
For Example, we have Repos named Microservice, where we have .NetProject, AngularUI Project, and Java Project code.The structure looks like this:
While setting up the CI pipeline, I have included the path like the below:
variables:
- name: working-dir
value: 'MicroserviceProject/AngularUI/ClientApp/'
trigger:
branches:
include:
- master
paths:
include:
- 'MicroserviceProject/AngularUI/ClientApp/*'
I don't see the code of AngularUI project being checkout properly and encountering the error, that they cannot locate the package.json file.
How can I set the working directory for different projects in a repo?
Update:
I am able to locate the file but the build isnot giving me any output files.

How I fixed this issue:
Initially I was not sure if the working directory was set properly.Even if it was , I was not sure whether the package.json file was read properly. To check that, I added the below script to the Azure CI pipeline, for example:
inputs:
targetType: 'inline'
script: dir
$(Build.ArtifactStagingDirectory)
displayName: 'Check'
This showed me that after building , the artifacts are not stored anywhere. hence I had to explicitly mention the outputpath. For that I ran the below command for build:
run-script build -- --output-path=$(Build.ArtifactStagingDirectory
This fixed the issue I was facing.

Related

Including the build of one repo to another repo in Azure DevOps

I have a repo in Azure devops which is using 'ant all' command to build an ear file. This ear file contain should a war file built from another repo in the azure.
How can I include this war file in the build?
Thanks.
If your war file is built from code hosted in another repo and not is a part of the repo itself. You can publish pipeline artifact and download it in your next pipeline. Here you have doc article about this. And here description of the task itself.
In short to publish you should add code like this:
steps:
- publish: $(System.DefaultWorkingDirectory)/bin/WebApp
artifact: WebApp
and to download:
# Download artifacts from a specific pipeline.
- task: DownloadPipelineArtifact#2
inputs:
source: 'specific'
project: 'FabrikamFiber'
pipeline: 12
runVersion: 'latest'
If you mean the war file is already in another repo in the same organization, you could check multiple checkout, by using multiple checkout steps in your pipeline, you can fetch and check out other repositories in addition to the one you use to store your YAML pipeline.
steps:
- checkout: git://MyProject/MyRepo # Azure Repos Git repository in the same organization
- checkout: self

How do I configure a build on VSTS to FTP deploy

I want to use VSTS to build and deploy my app (to FTP) when Update the master branch of my project. I have the script below. It works in that it triggers and builds but then fails to deploy because it can't find the files and I don't know what values to enter. I get the error below.
When VSTS builds, where does it put the build files?
I've watched youtube but all the examples are old and don't reflect how VSTS works right now so I'm totally stuck. There are no articles here that reflect how VSTS works right now and the Microsoft pages are no help either.
I'm running out of articles to review and am now pretty much guessing, so any help would be very much appreciated.
# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/dotnet-core
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
variables:
buildConfiguration: 'Release'
steps:
- script: dotnet build --configuration $(buildConfiguration)
displayName: 'dotnet build $(buildConfiguration)'
- task: FtpUpload#2
inputs:
credentialsOption: 'inputs'
serverUrl: 'ftp://xxxxxxx.xxxxxxxx.xxxxx/'
username: 'xxxxxxxxx'
password: 'xxxxxxxxxx'
rootDirectory: '/'
filePatterns: '**'
remoteDirectory: '/'
clean: false
cleanContents: true
preservePaths: false
trustSSL: false
I changed to this
rootDirectory: $(Agent.BuildDirectory)
and tried this
rootDirectory: $(Build.StagingDirectory)
And now the build succeeds but now I get this error/warning. Nothing is deployed.
When you set system.debug=true, then open the log for detailed compile process, you could see:
It is looking for directory '/' which you specified in the task. But, unfortunately, the content obtained with / is not any part of your repos. Then the server tell you sorry, can not find such file or directory in your repos.
This parameter is getting files or folders from your VSTS repos, so here you need use some words to let server know what you want to take from Repos.
(1) Since this task is in your build pipeline, you could use $(Build.SourcesDirectory) to represent for your whole repos.
(2) Also, if you just want to copy part of repos, such as folder or file, just input them with relative path. For example:
Upload the file upload.json which under the folder jobs, just
input rootDirectory: jobs/upload.json.
Upload one folder or file which in the root path of Repos, just input
rootDirectory: {folder name}, or rootDirectory: {folder name}. Like: rootDirectory: jobs, or rootDirectory: web.config

Can I have multiple build pipelines for the same repository?

I have a repository with two solutions in it. Both solution files exist in the root directory, essentially like this:
/WebsiteOneDirectory/
/WebsiteTwoDirectory/
/.gitignore
/WebsiteOne.sln
/WebsiteTwo.sln
Is it possible for me to build multiple pipelines pointed at this repository to build the different solutions? When I create my first pipeline it is generating a azure-pipelines.yml file for the repo, so I am unsure how/if I am going to be able to have multiple pipeline configurations if that is a fixed name it expects.
In addition to James Reed's answer, if you prefer using the .yml files, what I would recommend is to create multiple .yml definitions, one for each pipeline.
Here's what one would look like:
trigger:
branches:
include:
- master
paths:
include:
- WebsiteOneDirectory/*
exclude:
- WebsiteTwoDirectory/*
For building, you'd need to specify which solution to build. For a (.net core) example:
variables:
buildConfiguration: 'Release'
pool:
vmImage: 'Ubuntu-16.04'
steps:
- script: dotnet build WebsiteOne --configuration $(buildConfiguration)
Yes, you can use path filters in your trigger
Edit your build and go to the Triggers tab. Here you can add or remove branches, and also add path filters.
You have the option to either explicitly include or exclude paths. In the image below you can see that I'm explicitly excluding the "docs" folder from the master branch.

Why are there no files in my release artifacts directory?

I created a .netcore 2.1 api project and set up Azure DevOps to deploy it to an On-Prem computer using the Windows Agent.
The folder agent_work\r1\a was created on the machine running the agent, however there are no files in it.
I can see that a build did occur and that there is an artifact in my release
What did I miss doing?
[Update]
I can see in the agent job, there is a section which is about downloading an artifact.
It has the error message
There is no build available or the source is not accessible. You can select all artifacts or specify the list of artifacts to be downloaded below.
[Update]
Looks like I need to add a task to the job that is in a stage of my release.
And that task needs to copy the files I want to deploy.
Now the problem is knowing how to specify the source folder.
The strange thing is that it allows me to select a location and then rejects the selected location
There is help that says that if I leave the source folder empty, that will indicate the root of the repo. If I do this the deploy does not error but no files are deployed.
[Update]
I can see in the Get Sources of the Build the message "YOu are in 'detached Head' state. Maybe this has something to do with the problem.
[Update]
In the build pipeline I have
[Update]
When I try to create a new build pipeline I get the following
I select the location to be Azure Repos and the Repository to be vste
then I the azure-pipelines.yml auto-generates as
# ASP.NET Core
# Build and test ASP.NET Core web applications targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://learn.microsoft.com/vsts/pipelines/languages/dotnet-core
pool:
vmImage: 'Ubuntu 16.04'
variables:
buildConfiguration: 'Release'
steps:
- script: dotnet build --configuration $(buildConfiguration)
displayName: 'dotnet build $(buildConfiguration)'
[Update]
Looks like I might need to select Use The Visual Designer
[Update]
From ollifant's comment I have found the Publish Artifact task.
Now to figure out how to populate it
[Update]
Uh Uh not looking great
I had not used the ASP NET Core (.NET framework) template when creating the agent job in the build template, so the project was not building correctly.
New build pipeline -> Use the visual designer -> Continue -> ASP.NET Core (.Net Framework)

Gitlab Runner - New folder for each build

I'm using Gitlab CI for my project. When I push on develop branch, it runs tests and update the code on my test environment (a remote server).
But the gitlab runner is already using the same build folder : builds/a3ac64e9/0/myproject/myproject
But I would like to create a now folder every time :
builds/a3ac64e9/1/yproject/myproject
builds/a3ac64e9/2/yproject/myproject
builds/a3ac64e9/3/yproject/myproject
and so on
Using this, I could just update my website by changing a symbolic link pointing to the last runner directory.
Is there a way to configure Gitlab Runner this way ?
While it doesn't make sense to use your build directory as your deployment directory, you can setup a custom build directory
Open config.toml in a text editor: (more info on where to find it here)
Set enabled = true under [runners.custom_build_dir] (more info here)
[runners.custom_build_dir]
enabled = true
In your .gitlab-ci.yml file, under variables set GIT_CLONE_PATH. It must start with $CI_BUILDS_DIR/, e.g. $CI_BUILDS_DIR/$CI_JOB_ID/$CI_PROJECT_NAME, which will probably give you what you're looking for, although if you have multiple stages, they will have different job IDs. Alternatively, you could try $CI_BUILDS_DIR/$CI_COMMIT_SHA, which would give you a unique folder for each commit. (More info here)
variables:
GIT_CLONE_PATH: '$CI_BUILDS_DIR/$CI_JOB_ID/$CI_PROJECT_NAME'
Unfortunately there is currently an issue with using GIT_BUILDS_DIR in GIT_CLONE_PATH, if you're using Windows and Powershell, so you may have to do something like this as a work-around, if all your runners have the same build directory: GIT_CLONE_PATH: 'C:\GitLab-Runner/builds/$CI_JOB_ID/$CI_PROJECT_NAME'
You may want to take a look at the variables available to you (predefined variables) to find the most suitable variables for your path.
You might want to read the following answer Changing the build intermediate paths for gitlab-runner
I'll repost my answer here:
Conceptually, this approach is not the way to go; the build directory is not a deployment directory, it's a temporary directory, to build or to deploy from, whereas on a shell executor this could be fixed.
So what you need is to deploy from that directory with a script as per gitlab-ci.yml below, to the correct directory of deployment.
stages:
- deploy
variables:
TARGET_DIR: /home/ab12/public_html/$CI_PROJECT_NAME
deploy:
stage: deploy
script:
mkdir -pv $TARGET_DIR
rsync -r --delete ./ $TARGET_DIR
tags:
- myrunner
This will move your projectfiles in /home/ab12/public_html/
naming your projects as project1 .. projectn, all your projects could use this same .gitlab-ci.yml file.
You can not achieve this only with Gitlab CI runner configuration, but you can create 2 runners, and assign them exclusively to each branch by using a combination of only and tags keywords.
Assuming your two branches are named master and develop and two runners have been tagged with master_runner and develop_runner tags, your .gitlab-ci.yml can look like this:
master_job:
<<: *your_job
only:
- master
tags:
- master_runner
develop_job:
<<: *your_job
only:
- develop
tags:
- develop_runner
(<<: *your_job is your actual job that you can factorize)