Azure DevOps Pipelines: If Expressions and Conditions

Developer Support

John Folberth continues his series on Azure DevOps Pipelines by taking a deep dive into If Expressions and Conditions.


At this stage in the series we’ve talked about tasks, jobs, stages, how to template them, and how to leverage environments in variables. This post will attempt to cover some basics around using if and conditions in your YAML Pipelines. 

If Expressions

If expressions are simple and easy enough in YAML pipelines, they are a powerful tool. In my experience I have leveraged if expressions to: 

  • Conditionally load templates 
  • Dynamically load variables 
  • Enable same pipeline for CI/CD 

The key to unlocking their power is the understanding that an if expression will evaluate at pipeline compilation. This means the pipeline has to leverage known values to apply the logic within. We should not use an if expression when relying on the output of another task/job, the status of another job, or a variable that is updated during pipeline execution. 

The other side of this, since the statement is evaluated at pipeline compilation time, is that we will not load any unnecessary templates into our pipelines. As opposed to conditions, which will we cover next, templates will not appear in the expanded pipeline YAML file. This leads to a cleaner and more secure experience since only what will be executed will appear in the pipeline logs.

Continues reading the full post here and check out the series on the Microsoft Health and Life Sciences Blog. 

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Bhagavan Reddy 0
     try {
          var siteURL = this.props.context.pageContext.web.absoluteUrl;
         
            this.props.context.spHttpClient
            .get(
              siteURL +
              // tokenresource +
              // `_api/v2.1/getNewsFeed?section=SharePointNewsFeedTargeted&$expand=analytics($expand=allTime),thumbnails&$skiptoken=${token}&$top=13`,
              `/_api/v2.1/getNewsFeed?section=SharePointNewsFeedTargeted&$expand=analytics($expand=allTime),thumbnails&$top=13`,
              SPHttpClient.configurations.v1,
              {
                headers: {
                  'authorization': `Bearer ${token}`,
                  'sphome-apicontext': `{"PortalUrl":"${siteURL}"}`
                  // 'sphome-apicontext': `{"PortalUrl":"${tokenresource}"}`
                }}
            )
            .then((responseObj: SPHttpClientResponse) => {
              responseObj.json().then((responseJSONObj) => {
               console.log(JSON.stringify(responseJSONObj));
              });
            });
        } catch (ex) {
          console.warn(ex);
        }

    Developer Team,

    I want to customize News webpart using SharePoint Framework. I need to get hub associated sites News into webpart and display, i am using v2.1 getNewsFeed api. didn’t find any article related to it. Could some one help me how to expose?

    try {
    var siteURL = this.props.context.pageContext.web.absoluteUrl;

    this.props.context.spHttpClient
    .get(
    siteURL +
    // tokenresource +
    // `_api/v2.1/getNewsFeed?section=SharePointNewsFeedTargeted&$expand=analytics($expand=allTime),thumbnails&$skiptoken=${token}&$top=13`,
    `/_api/v2.1/getNewsFeed?section=SharePointNewsFeedTargeted&$expand=analytics($expand=allTime),thumbnails&$top=13`,
    SPHttpClient.configurations.v1,
    {
    headers: {
    ‘authorization’: `Bearer ${token}`,
    ‘sphome-apicontext’: `{“PortalUrl”:”${siteURL}”}`
    // ‘sphome-apicontext’: `{“PortalUrl”:”${tokenresource}”}`
    }}
    )
    .then((responseObj: SPHttpClientResponse) => {
    responseObj.json().then((responseJSONObj) => {
    console.log(JSON.stringify(responseJSONObj));
    });
    });
    } catch (ex) {
    console.warn(ex);
    }

Feedback usabilla icon