how to restrict the access on build.yml file from developers in Azure DevOps repository - azure-devops

In Azure DevOps we have created both build and release pipeline using classic way and now we are planning this to convert to yaml file.
But it seems in yaml method, the code can be put only on the root of the repo, where we want to keep the build yaml files in a separate repo, where the developers won't have access.
How can achieve this?

You can use templates, put in the main repo only the minimal yaml that refers to a template with all the steps, the template exits in another repo.
For example, your main repo yaml:
resources:
repositories:
- repository: templates
type: git
name: Contoso/BuildTemplates
jobs:
- template: common.yml#templates # Template reference
In in the repo: Contoso/BuildTemplates put the full yaml:
# Repo: Contoso/BuildTemplates
# File: common.yml
parameters:
vmImage: 'ubuntu 16.04'
jobs:
- job: Build
pool:
vmImage: ${{ parameters.vmImage }}
steps:
- script: npm install
- script: npm test
Restrict the access to the second repo (unless the agent pipeline user).
Read here more info about the resources.

I agree that one solution could be the one proposed by #Shayki Abramczyk
but to have standalone *.yml in dedicated repository you can use 'git clone' while using 'Git Credentials' to access the other repository that contains the files you want to build by the pipeline.
If your repository dedicated for *.yml is in the same Azure Devops project then you should not have any problem with the release definition.
Please see example *.yml that works for us as described:
pool:
vmImage: 'your-preferred-image'
variables:
solution: '$(Agent.BuildDirectory)/**/YourSolution.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Debug'
urlWithCreds: 'https://YourUser:YourPassword#dev.azure.com/YourOrganization/YourProject/
_git/YourOtherRepository'
steps:
- task: CmdLine#2
inputs:
script: |
git --version
git clone --quiet $(urlWithCreds)
git checkout master
- task: VSBuild#1
inputs:
solution: '$(solution)'
msbuildArgs: 'your build args'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'

You don't have to keep the YAML files in the root of the repository; ours are in a dedicated sub-folder:
That's crucial, because it means that we can add a PR policy which restricts who can approve changes to any of the pipeline YAML files.

Related

I can't get the Azure Devops trigger to work

I will try to explain in the best way.
First situation: I didn't find any task to insert my yml file which is in ruby. If I choose Ruby, I can't edit the yaml file. If I can edit that file, I should make this trigger fire. It actually fires, not with the yml file I want
If I use the other way I can't save because I don't have permission to run pr on the master branch. My pipeline doesn't run automatically just manually
enter image description here
enter image description here
My yml:
# Ruby
# Package your Ruby project.
# Add steps that install rails, analyze code, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/ruby
trigger:
branches:
include:
- homolog_gso
pool:
vmImage: 'ubuntu-latest'
steps:
- task: UseRubyVersion#0
inputs:
versionSpec: '>= 2.5'
- script: |
gem install bundler
bundle install --retry=3 --jobs=4
displayName: 'bundle install'
- script: bundle exec cucumber -t #incluir_setor_resp_em_branco
displayName: 'bundle exec cucumber'
- task: PublishTestResults#2
inputs:
testResultsFormat: 'NUnit'
testResultsFiles: '**/TEST-*.xml'
mergeTestResults: true
searchFolder: '$(Common.TestResultsDirectory)'
testRunTitle: 'Regression Test Geocall Gab'
When creating a new YAML pipeline and selecting the provided template to generate a initialized YAML file, by default this YAML file will be saved into the default branch of source repository (such as master, main).
If you do not have the permissions to directly edit files in the default branch, you are surely not able to edit the YAML file saved in the default branch.
you should copy / create the YAML to another branch where you have the permissions to edit files (e.g. homolog_gso). Then create a PR to merge the changes from this branch to the default branch.
About the PR trigger in your case, you can set the PR trigger like as below in the YAML file from the homolog_gso branch:
pr:
branches:
include:
- master
. . .
By this way, if you push some changes to the homolog_gso branch and then create a PR to merge the changes to the master branch, the PR trigger will fire to run the YAML pipeline.

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

Azure static web apps

Azure static web apps(preview) currently only works with a github account, and as a company policy we have to use Azure for repos and everything else (pipelines, releases, etc..) We are going to use the static web app just for viewing a simple angular website however all the source code must remain in the azure devops repo.
Is is possible to create a private github account and upload to it only the compiled angular files to make use of the static web app? for example we already have a pipeline to compile and deploy the angular website to an Azure web app service, can this pipeline be modified to publish the same files to the github account? and if so how?
You can do it in Azure Devops. Try put this YML in Azure DevOps Pipeline
trigger:
- main
pool:
vmImage: 'ubuntu-latest'
name: 'yourApplicationName'
steps:
- task: NodeTool#0
inputs:
versionSpec: '12.x'
displayName: 'Install Node.js'
- script: |
npm install -g #angular/cli
npm install
displayName: 'npm install'
workingDirectory: '$(Build.SourcesDirectory)/yourApplicationName'
- task: AzureStaticWebApp#0
inputs:
app_location: "/yourApplicationName"
api_location: "api"
app_build_command: $(build_command)
output_location: "dist/yourApplicationName"
env:
azure_static_web_apps_api_token: $(deployment_token)
You can put build_command and deployment_token in the variables
Azure SWA is only available for code in Github at the moment.
However, you can get something pretty close with Azure App Service. Microsoft even has a couple blog posts about how to configure Azure App Service, Azure Repos and Azure Pipelines together.
You can deploy a React Static Web Apps from Azure using Pipelines as follows:
name: Azure Static Web Apps CI/CD
#
# Trigger off of changes in master or another branch(s)
#
trigger:
branches:
include:
- TestBranch
jobs:
- job: build_and_deploy_job
displayName: Build and Deploy Test Web App
condition: or(eq(variables['Build.Reason'], 'Manual'),or(eq(variables['Build.Reason'],'PullRequest'),eq(variables['Build.Reason'], 'IndividualCI')))
pool:
vmImage: ubuntu-latest
variables:
- group: <Name of your Static Web App Resource Group>
steps:
- checkout: self
submodules: true
- task: AzureStaticWebApp#0
inputs:
azure_static_web_apps_api_token: $(AZURE_STATIC_WEB_APPS_TOKEN_#####)
# Details at https://aka.ms/swaworkflowconfig
app_location: "/" # App source code path
api_location: "" # Api source code path - optional
output_location: "build" # Built app content directory - optional
app_build_command: 'chmod 755 scripts/*.sh;./scripts/WebAppBuild.sh prod'
You will first need to set up a connection in Azure->Settings->Pipelines->Service Connections. Use the Pipeline wizard to set up all the initial YML.
The key for me was to run my own build in WebAppBuild.sh which basically runs "npm run build". My repo has a scripts folder. The job/task performed the rest of the install

Cypress Integration with DevOps

What I want to achieve:
I have a repository on Azure DevOps which hosts my web application. I wrote a test suite for UI Automation using Cypress. I created a separate repository for my test cases to check if they are working properly or not. I created a pipeline which has the following content:
trigger:
- manual-tests
pool:
vmImage: 'ubuntu-latest'
steps:
- task: NodeTool#0
inputs:
versionSpec: '10.x'
displayName: 'Install Node.js'
- script: |
npm install
displayName: 'npm install'
- task: Npm#1
inputs:
command: 'custom'
customCommand: 'run test'
continueOnError: true
- task: PublishTestResults#2
inputs:
testResultsFormat: 'JUnit'
testResultsFiles: '**/test-output-*.xml'
testRunTitle: 'My Test Cases'
I have a trigger set to a branch of the repository in which my UI Automation code is stored. What I want is, to trigger my automation script, when there is a push on some branch of the web application repository. Is there a way of doing this? Can we store our test case files in the application repository and give the path of the test script?
It seems that the UI Automation Repo and Web Application Repo are two separate repos.
To trigger my automation script, when there is a push on some branch of the web application repository. Is there a way of doing this?
The function: "trigger a pipeline from a different repo" is not available now.
This feature is still under development. Multi-repository support for YAML pipelines will be available soon for azure devops service.
Please check the function:"Multi-repository support for YAML pipelines" in Azure DevOps Feature Timeline 2020 Q2. This feature will roll out to everyone by the end of July 2020.
Workaround:
You could try to use the Pipeline triggers.
Here are the steps:
Step1: Create a pipeline with web application repository, then you could set the trigger branch.
Step2: Add the Pipeline trigger in the Yaml file (UI Automation Repo).
For example:
resources:
pipelines:
- pipeline: Name
source: Pipeline name
trigger:
branches:
- releases/*
- master
When you make changes in web application repository, the pipeline with the web application will be triggered.
After running the pipeline , the pipeline with UI Automation repo will be triggered.
Can we store our test case files in the application repository and give the path of the test script?
Of cource. You can do it.
If you want to use the test file in the pipeline (UI Automation repo), you could add the repo resouces in the pipeline.
For example:
resources:
repositories:
- repository: MyAzureReposGitRepository
type: git
name: MyProject/WebapplicationRepo
...
steps:
- checkout: MyAzureReposGitRepository
Note: the repo will be check out to the Agent Source Folder.
Hope this helps.

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