How can i include folders from other repos when running a pipeline in Azure Devops - azure-devops

I have a repo called REPO A and have created a YAML pipeline in here but I also have some code in REPO B which I need to use in this pipeline
In YAML how can tell the pipeline in REPO A to use files/folders from REPO B?
Im a newbie to this so can you please keep it simple?
resources:
repositories:
- repository: automation
type: git
name: MYORG/ automation
ref: develop
endpoint: YourEndpoint
steps:
- checkout: self
path: "s/Source" : ***>>>>>> what should this be??***
- checkout: automation

You should check out multiple repositories on your YAML pipeline. You can define a new resource repository and checkout this repository along with your source (the one that triggers the pipeline).
resources:
repositories:
- repository: devops_sf
type: github
name: Organization/Repository
ref: master
endpoint: YourEndpoint
steps:
- checkout: self
path: "s/Source"
- checkout: devops_sf
Then you will be able to access your repositories under
$(Pipeline.Workspace)/s/Repository
$(Pipeline.Workspace)/s/Source
Multi repo checkout for Azure DevOps:
https://learn.microsoft.com/en-us/azure/devops/pipelines/repos/multi-repo-checkout?view=azure-devops

ok simply put at the top of the yaml i added
> resources:
> repositories:
> - repository: automation
> type: git
> name: name of yourpoject/name of repo you want to use
> ref: whatever branch of that repo(under repository above) you want to use
THEN!! under steps in the YAML (if you havent one you need to add it!)
> steps:
> - checkout: self
> - checkout: your repository you want to use as above
Also if you use npm to run a task you need to add the below as well to not get any file not found errors! a common error if you dont add the below code is : npm WARN saveError ENOENT: no such file or directory, open '/home/vsts/work/package.json
The below code is put in the -Task section
> > npm install
> > displayName: 'a task using npm'
> > workingDirectory: '$(Build.SourcesDirectory)/your repository you want to use as above
ALSO be aware of indentation when inserting code in YAML it's annoying and a pain if you havent got the indentation right it gives you errors just tab until they go away

Related

is there any way to select branches from multiple repositories in CI build

I have trigger in azure-pipelines.yaml like below.
resources:
repositories:
- repository: APPLICATION
type: git
name: AAA/APPLICATION
ref: master
- repository: TESTS
type: git
name: AAA/TESTS
ref: master
STAGES:
- stage : BuildApplication
// checkout branch & build necessary things
- stage : BuildTests
// checkout branch & build necessary things
Since the yaml resides in Application repository, While creating manual CI build I am able to select the Branches in Application repository & for Tests repository the branch checkout will be master always.
is there any was I can able to set the branch details of Tests repository before creating release ?
Is there any was I can able to set the branch details of Tests repository before creating release ?
From your YAML sample, you need to select the Resource Repo branch when manually running the pipeline.
I am afraid that there is no out-of-box method can select the resource repo branch. The branch is defined at resources field. When you running the pipeline, it will use the default branch.
For a workaround, you can change to define the repo in check out field. You can use paramter to define the repo branches and then you can select the branch when you running the pipeline.
Refer to this doc: Inline syntax checkout
Here is an example:
parameters:
- name: test
values:
- refs/heads/main
- refs/heads/test1
pool:
vmImage: ubuntu-latest
steps:
- checkout: git://azure/Repo13#${{ parameters.test }}
Result:

Azure Pipeline configurations

I am facing a problem that is confusing me a bit and I would like to find the best approach to avoid to repeat my pipelines.
in this GitHub repos I have my yaml file to build the project, and this yaml targets a folder template on which it runs the C# Build and Publish. Approximately the GitHub repo is structure as follow:
- Folder 1
- Folder 2
- Folder 3
- Azure-Pipelines(build and Publish)
- Azure-pipeline.yaml
During the pipeline run, my yaml targets the Àzure-Pipelines(Build and Publish) folder and build the project. This is my Azure-pipeline.yaml file
stages:
- stage: Build
displayName: 'Build'
jobs:
- template: Azure-Pipelines/build.yaml
parameters:
solution: 'Solution-to-build'
- stage: Publish
displayName: 'Release and Push'
jobs:
- template: Azure-Pipelines/publish.yaml
parameters:
<All the parameters configured for this yaml file>
The template and the structure of my GitHub, keeps repeating themselves, as in each gitrepo I have that Azure-pipeline folder. What I am trying to to. Is to have a GitHub repo Where I keep the build.yaml and publish.yaml. and make all the other repos refer to this folder when the pipeline runs.
Is there any way how I can achieve this?
Please if I am missing any details to make my point clear, just ask. Thank you so much in advance
Is there any way how I can achieve this?
You could try to use the Resources in YAML Pipeline.
Here are the steps:
Step1: Create a Github Service Connection in Project Settings -> Service connection .
Step2: You could try to use the following sample to use the yaml template from another Github Repo.
Example
resources:
repositories:
- repository: MyGitHubRepo # The name used to reference this repository in the checkout step
type: github
endpoint: serviceconnectionname
name: githuborg/reponame #e.g. yy/test
ref: main
pool:
vmImage: ubuntu-latest
stages:
- stage: Build
displayName: 'Build'
jobs:
- template: Azure-Pipelines/build.yml#MyGitHubRepo

Multiple Repostories in a Pipeline from the same organisation

I got 2 projects within my Azure Organisation. I have created a pipeline from one repository which it's job is to promote code from one repo in the current project to another repo in a different project. I am using YAML and have built up the service connections
# Deploy to PREPROD
resources:
repositories:
- repository: target
type: git
name: 'Other Project/ThisRepo'
trigger:
branches:
include: [
azure-pipelines
]
pool:
name: 'My Pool'
demands:
- agent.computerName -equals MYPC
steps:
- checkout: self
path: source
- checkout: target
path: target
So on the GUI, it shows the target repo and I can browse to it. However when I run the pipeline, I get the following: -
remote: TF401019: The Git repository with name or identifier ThisRepo
does not exist or you do not have permissions for the operation you
are attempting. fatal: repository
'https://dev.azure.com/myOrg/Other%20Project/_git/ThisRepo/' not found
I can't figure out why it can't access it. I've seen in the docs about if I can access it then when the pipeline is created it should be given permission... I don't understand :/
TIA
Please check if your <your project> Build Service accounts belongs to Project Collection Service Accounts on organization level.
I found similar issue solved here
Another option would be to add access only to particular repo to Build Service account from another project:
To do that go to:
Other Project
Settings
Repos -> Repositories
select ThisRepo
select Security tab and type name of the project which wants to use this repo and add Read permission

azure pipeline pull dependency projects

I have a project which depends on 2-3 other projects, is there a way to pull them together with the master project?
When the build process starts projects will be on the file system and the master project can locate the other dependency projects?
As #Kehinde's said in comment, what you want could be achieved by the feature Multi-repo checkout.
Note:
Multi-repo checkout is the feature which only supported YAML. Because what the design logic is Checkouts from multiple repos in combination with YAML builds enable focusing the source level dependency management to one structured descriptor file in Git (the YAML biuld definition) for good visibility.
But for pipeline that configured by classic UI, you had to add those other repositories/projects as submodules, or as manual scripts to run git checkout in pipeline.
For personal, I strongly suggest you use YAML to achieve what you want.
Simple sample YAML definition:
resources:
repositories:
- repository: tools
name: Tools
type: git
steps:
- checkout: self
- checkout: tools
- script: dir $(Build.SourcesDirectory)
Here, image I have a repository called "MyCode" with a YAML pipeline, plus a second repository called "Tools".
In above third step(dir $(Build.SourcesDirectory)), it will show you two directories, "MyCode" and "Tools", in the sources directory.
Hope this helps.
For Bitbucket:
resources:
repositories:
- repository: MyBitBucketRepo
type: bitbucket
endpoint: MyBitBucketServiceConnection
name: {BitBucketOrg}/{BitBucketRepo}
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
steps:
- checkout: self
- checkout: MyBitBucketRepo
- script: dir $(Build.SourcesDirectory)

Cannot trigger build via push to another repository in a YML Pipeline

I have the following repositories:
my-app-repo - Contains the code for the app
pipeline-repo - A collection of pipelines that build my-app-repo as well as others
I'm trying to build a yml pipeline within pipeline-repo that will be triggered on a commit to my-app-repo.
According to the official yml documentation, it sounds possible but I'm not able to get it working.
Here's what I've tried:
pipeline-repo/my-app-repo-build.yml
resources:
repositories:
- repository: target_repo
type: git
name: my-project/my-app_repo
trigger:
branches:
include:
- master
jobs:
- job:
steps:
- script: echo "Should be triggered from a push to my-app-repo!"
The build is not triggering when I push to my-app-repo. It only kicks off for commits to the source repo (pipeline-repo) which I cannot change since that holds the yml definition.
Am I missing something easy?
Edit:
I see you've added an issue to the github repository you mentioned. I agree that it seems to be broken.
While you aren't exactly trying to provide template functionality to your pipelines, you might use it as a work around until the issue you created is addressed.
I've tested this in my playground and it seems to work.
In the app repository
resources:
repositories:
- repository: templates
type: git
name: Pipeline-Templates
trigger:
branches:
include:
- master
jobs:
- template: azure-pipelines-template.yml#templates
In the pipelines repository
jobs:
- job: Get_Last_10_Commits
pool:
vmImage: 'vs2017-win2016'
steps:
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
cd $(Build.SourcesDirectory)
Write-Host "Show git log (last 10):"
git log --oneline -10
One "advantage" of this work around is that you no longer need to specify that the repository to checkout is the resource repository and not the self (your template-repository) as the self repository is the app-repo.
This would allow you to restrict changes to the pipeline core by having it in a separate repo like you want, but would still trigger on the app-repo master.
Not ideal b/c you now have 2x .yml files for each build you need, but that's basically the definition of work around: not ideal.
It looks like your include syntax is wrong. Have you tried to use the simple syntax?
All of the examples (and my experience) show that you should use the wildcard syntax when your trigger has an include or exclude specification.
resources:
repositories:
- repository: myPHPApp
type: GitHub
connection: myGitHubConnection
source: ashokirla/phpApp
trigger:
branches:
include:
- features/*
exclude:
- features/experimental/*
paths:
exclude:
- README.md