azure devops , checkout only the runner branch in CI - azure

how can I do it , I added this one but I not sure if is right:
steps:
- checkout: self
fetchDepth: 1
clean: true

resources:
repositories:
- repository: MyRepo
type: git
name: Myrepo
ref: master
steps:
- checkout: MyRepo
This should work

Related

Azure DevOps checkout only triggered repository

I was trying to check out only the repository that triggers the pipeline.
resources:
repositories:
- repository: repo2
type: git
name: branching/repo2
ref: dev
trigger:
- dev
- repository: repo1
type: git
name: branching/repo1
ref: main
trigger:
- main
pool:
vmImage: ubuntu-latest
steps:
- script: echo "$(Build.Repository.Name)"
- ${{ if in(variables['Build.Repository.Name'], 'repo1') }}:
- checkout: repo1
- ${{ if in(variables['Build.Repository.Name'], 'repo2') }}:
- checkout: repo2
But each time, this only checkout the source repository.
When the pipeline is triggered from repo1, I tried to checkout repo1, and when it is triggered from repo2, I tried to checkout repo2.
I don't want to keep changing the name of the checkout repository in the pipeline file.
Is there another way to have the checkout task choose the triggered repository automatically?
Checking out multiple repos involve different ways of calling checkout:
Check out the triggering repo with: checkout: self
Check out the other repos with: checkout: <reponame>
More info and options using checkout see:
https://learn.microsoft.com/en-us/azure/devops/pipelines/repos/multi-repo-checkout?view=azure-devops#specify-multiple-repositories
Edit
Assuming your trigger repo is repo1 or repo2, your YAML example would look like:
resources:
repositories:
- repository: repo2
type: git
name: branching/repo2
ref: dev
trigger:
- dev
- repository: repo1
type: git
name: branching/repo1
ref: main
trigger:
- main
pool:
vmImage: ubuntu-latest
steps:
- script: echo "Triggered repo: $(Build.Repository.Name)"
- checkout: self
- ${{ if in(variables['Build.Repository.Name'], 'repo1') }}:
- checkout: repo2
- ${{ if in(variables['Build.Repository.Name'], 'repo2') }}:
- checkout: repo1

Azure Devops: how configure my pipeline script to trigger another pipeline with different repo?

I have my own pipeline p-A with repository r-A, and pipeline p-B with another repo r-B.
I want to update the pipeline script for p-A only, to trigger the p-B actively, without any modification in p-B.
Below is the yaml pipeline script for p-B, which is already set up to run with schedule
pool:
name: 'workflow_test_pool'
schedules:
- cron: "0 19 * * *"
displayName: run test every day at 8PM CET
branches:
include:
- main
always: true
trigger: none
jobs:
- job:
timeoutInMinutes: 30
steps:
- script: |
python -m pytest tests/ -s
displayName: 'Run the test'
below is the pipeline script main.yaml for p-A
pool:
name: 'workflow_test_pool'
stages:
#########################
- template: pipeline2/p1.yaml
############################
- template: pipeline2/p2.yaml
parameters:
dependsOn:
- FirstPipeline
so the question is, how to trigger the pipeline p-B in pipeline2/p2.yaml(from p-A)?
You can create PowerShell script task as the last step of the pipeline to trigger pipeline p-B through REST API. You will have to maintain Personal Access Token, ideally as secret variable.
REST API call you will use:
https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/runs/run-pipeline?view=azure-devops-rest-7.1
Detailed step-by-step guide:
https://blog.geralexgr.com/cloud/trigger-azure-devops-build-pipelines-using-rest-api
Azure DevOps supports multiple repositories checkout, you can just reference resources function in your YAML script and call another repository to trigger from the pipeline.
YAML code :-
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
pool:
vmImage: ubuntu-latest
workspace:
clean: all
resources:
repositories:
- repository: repo_a
type: git
name: InternalProjects/repo_a
trigger:
- main
- release
- repository: repo_b
type: git
name: InternalProjects/repo_b
trigger:
- main
steps:
- checkout: repo_a
- checkout: repo_b
- script: dir $(Build.SourcesDirectory)
I am running this pipeline from repo_a and the repo_a and repo_b both ran successfully like below:-
Output :-
You can directly run any task from pipeline with multiple repositories like below:-
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
pool:
vmImage: ubuntu-latest
workspace:
clean: all
resources:
repositories:
- repository: repo_a
type: git
name: InternalProjects/repo_a
trigger:
- main
- release
- repository: repo_b
type: git
name: InternalProjects/repo_b
trigger:
- main
steps:
- checkout: repo_a
- checkout: repo_b
- task: AzureCLI#2
inputs:
azureSubscription: 'Subscription-name(sub-id)'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: 'az resource list --location uksouth'
Output:-
References :-
Check out multiple repositories in your pipeline - Azure Pipelines | Microsoft Learn
Trigger azure Devops pipeline from another repository - GeralexGR
Multiple Repositories in a Single Azure Pipeline - DEV Community 👩‍💻👨‍💻

How to design a script github sync to gitlab specific destination branch?

Such as:
source repo on github Branch master -> destination repo on gitlab Branch main
I'm newbie for github action.
How to design a script entrypoint.sh or new pipeline or example for me, please.
Note: sorry, my english not strong.
Workflows: git-repo-sync.yml
name: build
on:
- push
- delete
jobs:
sync:
runs-on: ubuntu-latest
name: Git Repo Sync
steps:
- uses: actions/checkout#v2
with:
fetch-depth: 0
- uses: chayanbank/git-sync#main
with:
target-url: 'https://gitlab.com/chayanbank/test-react-web.git'
target-branch: 'main'
target-username: 'githubsync'
target-token: ${{ secrets.GITHUBSYNC }}
action.yml
name: 'Git Repo Sync'
description: 'Git Repo Sync enables you to synchronize code to other code management platforms, such as GitLab, Gitee, etc.'
branding:
icon: upload-cloud
color: gray-dark
inputs:
target-url:
description: 'Target Repo URL'
required: true
target-branch:
description: 'Target Repo Branch'
required: true
target-username:
description: 'Target Repo Username'
required: true
target-token:
description: 'Target Token'
required: true
runs:
using: "composite"
steps:
- run: ${{ github.action_path }}/entrypoint.sh
shell: bash
env:
INPUT_TARGET_URL: ${{ inputs.target-url }}
INPUT_TARGET_BRANCH: ${{ inputs.target-branch }}
INPUT_TARGET_USERNAME: ${{ inputs.target-username }}
INPUT_TARGET_TOKEN: ${{ inputs.target-token }}
GITHUB_EVENT_REF: ${{ github.event.ref }}
entrypoint.sh
git remote add target https://${INPUT_TARGET_USERNAME}:${INPUT_TARGET_TOKEN}#${INPUT_TARGET_URL#https://}
case "${GITHUB_EVENT_NAME}" in
push)
git push -f --all target
git push -f --tags target
;;
delete)
git push -d target ${GITHUB_EVENT_REF}
;;
*)
break
;;
esac

How do I build the right project when checking multiple projects in Azure devops

I am reading this doc: https://learn.microsoft.com/en-us/azure/devops/pipelines/repos/multi-repo-checkout?view=azure-devops
According to the link above, we can have multiple projects in one pipeline. So I have something like this:
trigger:
- none
resources:
repositories:
- repository: repo1
type: git
name: org/repo1
ref: test_multi_repo_pipeline
trigger:
- test_multi_repo_pipeline
- repository: repo2
type: git
name: org/repo2
ref: test_multi_repo_pipeline
trigger:
- test_multi_repo_pipeline
stages:
- stage: Build_and_Deploy
pool:
name: 'myAgent'
jobs:
- job: Build
condition: always()
steps:
- checkout: repo1
displayName: 'repo1'
- checkout: repo2
displayName: 'repo2'
- script: dir $(Build.SourcesDirectory)
displayName: 'Build.SourcesDirectory'
- script: |
npm run build:project_win_dev
displayName: Building the project...
- script: |
npm run test:coverage
displayName: Testing the skill...
- script: dir $(Build.SourcesDirectory)
displayName: 'Build.SourcesDirectory'
name: Test
So when I execute this yaml I get the next output on this task "Build.SourcesDirectory":
Directory of E:\Builds_VNext\Agent2_Builds\3046\s
03/24/2022 11:24 AM <DIR> .
03/24/2022 11:24 AM <DIR> ..
03/24/2022 11:24 AM <DIR> pipelines
03/24/2022 11:24 AM <DIR> repo1
03/24/2022 11:24 AM <DIR> repo2
0 File(s) 0 bytes
5 Dir(s) 878,801,965,056 bytes free
So once the "Build" task gets executed it fails because it gets executed in the root rather than in the specific project which I made the commit. So I was wondering if there is a way to know that if I made the commit in the project repo1, then the build gets done under the folder repo1 and if I made the change on repo2 then the build gets done inside repo2 folder
Thanks in advance for your help.
Greetings
So, you could write a conditional to determine which repo you're editing. But, I'd advise against it. You'd have to query the git history and detect changes.
The path of lease resistance is to build both every time. You'd just need to update your code to cd to the correct folder before building:
trigger:
- none
resources:
repositories:
- repository: repo1
type: git
name: org/repo1
ref: test_multi_repo_pipeline
trigger:
- test_multi_repo_pipeline
- repository: repo2
type: git
name: org/repo2
ref: test_multi_repo_pipeline
trigger:
- test_multi_repo_pipeline
stages:
- stage: Build_and_Deploy
pool:
name: 'myAgent'
jobs:
- job: Build
condition: always()
steps:
- checkout: repo1
displayName: 'repo1'
- checkout: repo2
displayName: 'repo2'
- script: dir $(Build.SourcesDirectory)
displayName: 'Build.SourcesDirectory'
- script: |
cd repo1
ls
echo '----'
npm run build:project_win_dev
echo 'Running Tests'
npm run test:coverage
displayName: 'Build and Test: Repo 1'
- script: |
cd repo2
ls
echo '----'
npm run build:project_win_dev
echo 'Running Tests'
npm run test:coverage
displayName: 'Build and Test: Repo 2'
- script: dir $(Build.SourcesDirectory)
displayName: 'Build.SourcesDirectory'
name: Test

Migrating azure-pipelines.yaml to separate repo, but running on code of other repo

Ultimately, I'm trying to do this:
Move azure-pipelines.yaml and associated templates out of the code repository (code-repo).
Move them into a separate dedicated repository (pipeline-repo).
Have the pipeline look at the config for the pipeline in pipeline-repo, but run the pipeline on the code in the code-repo.
I'm referring the following documentation:
Use other repositories: this one refers to "templates in other repositories," but I'm trying to remove any pipeline configs so the code-repo is just purely application code... and the Dockerfile.
Define a repositories resource
For testing, I have this simple test.yaml:
# Triggers when PR is created due to branch policies
trigger: none
resources:
repositories:
- repository: code-repo
type: git
name: code-repo
pool:
vmImage: 'ubuntu-latest'
stages:
- stage: Testing
displayName: Test stage
jobs:
- job: ParallelA
steps:
- bash: echo Hello from parallel job A
displayName: 'Run a one-line script'
When I create a PR on code-repo, it is triggering the pipeline, which is to say branch policies are configured to refer to that pipeline. I do get the print out the Hello from parallel job A print out.
But I don't see in the run logs it pulling code-repo.
I do see the following, however:
My actual PR pipeline would look something like this:
trigger: none
resources:
repositories:
- repository: code-repo
type: git
name: code-repo
variables:
- template: templates/variables.yaml
pool:
vmIMage: $(vmImageName)
stages:
- template: templates/build/buildStage.yaml
...
Testing that, it confirms that it isn't running on the code-repo PR, but the pipeline-repo so everything fails.
So it is unclear to me what I need to do from here to get the pipeline to run on the PR code from code-repo.
Suggestions?
Ok, I think I have it sorted out, at least some of my stages are now succeeding.
I came across this documentation which informed me of checkout.
So in addition to doing something like:
resources:
repositories:
- repository: code-repo
type: git
name: code-repo
Then you need to add a step called checkout like the following:
# Triggers when PR is created due to branch policies
trigger: none
resources:
repositories:
- repository: code-repo
type: git
name: code-repo
pool:
vmImage: 'ubuntu-latest'
stages:
- stage: Testing
displayName: Test stage
jobs:
- job: ParallelA
steps:
- checkout: code-repo
- task: task1
- task: task2
The checkout should set the context for the subsequent steps.

Resources