Posted on Leave a comment

Success pipeline state on GitHub with pipeline failure

When your pipeline fails on Azure DevOps and the source code is gathered from GitHub (using a service connection) you will notice a failure icon on the repository because the status of the run from Azure DevOps is reported to GitHub. This is the normal behavior and we want it to work like that.

Lets take as example the below run.

The failure will be visible on the github along with the particular commit.

However sometimes we need to allow tasks fail without reporting a failure state on GitHub. You can do that by using continueOnError keyword. With this functionality you instruct the DevOps platform to allow failure for a particular task or job without stopping the pipeline.

trigger:
- none

pool:
  vmImage: ubuntu-latest

jobs:
- job: job1

  steps:
  - script: this script will fail
    continueOnError: true
    displayName:  task 1

  - task: PowerShell@2
    displayName: task 2
    inputs:
      targetType: 'inline'
      script: |
        Write-Host "Hello World"

This will result on a warning state for your pipeline on Azure DevOps.

This means that the pipeline has been executed successfully but some steps have failed.

Nevertheless the state on GitHub will be success as the pipeline is not in a failed state on Azure DevOps.

Posted on Leave a comment

Dynamically checkout github repository based on parameter

In some cases, the DevOps team may need to checkout multiple github repositories in the current working directory of the pipeline. By default the pipeline will download the code of the repository where it belongs.

The below pipeline will download a github repository based on a parameter that user provides. In order to accomplish that you will need a service connection with your github account/organization.

Then using the below pipeline you can download a repository with a specific branch by providing the repository name.

trigger:
- main

pool:
  vmImage: ubuntu-latest

parameters:
- name: repoparam
  type: string
  default: reponame
- name: branchparam
  type: string
  default: main

variables:
  - name: repositoryvar
    value: ${{parameters.repoparam}}
  - name: branchvar
    value: ${{parameters.branchparam}}


resources:
  repositories:
    - repository: gitrepo
      type: github
      name: geralexgr/$(repositoryvar)
      ref: $(branchvar)
      endpoint: geralexgr

steps:
- checkout: gitrepo
  displayName: download repository

For example given that I have created a repository geralexgr/terraform-az-lin-win I will provide only the name of it terraform-az-lin-win and the main branch.

When running the pipeline the selected repository will be downloaded into C:agent/work/buildID/s or /home/vsts/work/buildID/s

Posted on Leave a comment

Automatically update your GitHub repositories with a powershell script

Developers often have a lot of repositories stored on their local machines. These repositories get updated from other developers and they stay outdated. In many cases developers forget to fetch and pull the latest changes on those repositories and when they commit code, the IDE will notify of the new changes. When this is the case, the commit will get an non explanatory message as the latest of the commit and you will have to navigate on the actual commit to verify the changes and commit message.

Commit message
Merge branch test/v3.0.0 of https://github.com/org/repository

In order to resolve this issue, you can create a powershell script that can automatically fetch the latest changes of your local repositories. You will need to change your repositories base location.

#change your github location
$github_directory = "C:\Users\galexiou\Documents\GitHub"
Get-ChildItem $github_directory | ForEach-Object {
if($_.Attributes -eq "Directory")
{
Write-Host $_.FullName
Set-Location $_.FullName
git fetch
git pull
}
}

As you can see from the output below this script will go and fetch the latest changes on the repositories that have been updated.

You can also create a cron job or an automated windows task in order to run this job automatically on computer startup or on your work schedule start. For example

On task scheduler press create task

and select your triggers (when this task will run) along with the action. This will be the run of the powershell script. On the argument you must specify the -File location (where you stored your powershell script).

Posted on Leave a comment

Build pipeline on tag push – Azure DevOps build triggers

There are multiple ways to define your continuous integration trigger on a pipeline depending on your needs. One common approach is to trigger a build whenever a new merge or push is done on your branch.

For example with the below notation you could trigger a new build every time a new push is merged on the uat branch.

trigger:
- uat

Another approach could be the pull request. Every time a new pull request is created for a specific branch your build could be initiated. In order to accomplish that you should use the pr keyword.

The below example will trigger when a new pull request is created and the merge destination is main branch. This approach could help you identify if the code of a specific feature/branch actually builds and can be merged on your main branch.

pr:
  branches:
    include:
    - main

Another approach is the tags functionality. You could run a build only if a specific tag is pushed along with the commit.

The below example will only build when the tag release.* is pushed on the branch on which the pipeline is located.

trigger:
  tags:
    include:
    - release.*

Some tags that could trigger my build are: release.v1 , release.master, release.v2

In order to push a tag on your branch using cmd you should

git add .
git commit -m "commit message"
git tag mytag
git push --tags

Then on tags section of your repository you can locate your new tag.

Documentation of triggers for Azure pipelines:

https://docs.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/trigger?view=azure-pipelines

Video tutorial on YouTube: