Tuesday, May 31, 2022

Build and deploy .net framework / .net core to IIS with Gitlab and some awesome templates

In this blog I'll explain the way we do our builds with gitlab. It took some time to get to this, so I'm happy to share it with you. At first we set the ground-rules:
  1. You use GitLab
  2. You have GitLab runners on windows machines on powershell (core)
  3. Make use of merge requests to merge code changes to the default branch
  4. Create release tags in the form of semver '1.0.0'
  5. The default branch always gets deployed to your development IIS server
  6. After creating a release in GitLab, a production build is generated
  7. Manual trigger starts the deployment to your production IIS server
  8. There is a solution file in the root of the project
  9. There is only 1 deployable webproject in your solution
  10. The dotnet-* templates reside in a shared repository
Challenges we faced we're especially with the msdeploy command which behaves really funky under powershell (I dare you to do a duckduckgo search on this and see what you'll find). I really like the way everything is parameterized, so the core just works, but if you need a bit more, you just adjust where needed without breaking all the defaults in the build an deploy pipelines. This makes maintaining them a lot easier when environments change and forces you to use a standardized way of work. 

I also like the validation of the release tag numbering using the .pre step validate_tag job. It only runs when a tag is set and it's not in the form of a semver. You can never build a production packages without a proper number.

The .gitlab.yml is the ci file for your end project, it sets urls and can customize some configuration. The dotnet-framework.v1.gitlab-ci.yml and dotnet-core.v1.gitlab-ci.yml files are the base templates you can inherit in the .gitlab-ci.yml. 

Below are all the gists needed to get you going or just to get inspired. If you have any feedback, I'm happy to learn about your experiences. Comment on the gist or this blog!

Happy deploying,
Luuk

Wednesday, February 24, 2021

Umbraco modelsbuilder line endings

Everytime the modelsbuilder detects a change in LiveAppData moe, the generated file will be touched.

Git uses auto line-endings, meaning CRLF on windows, while the umbraco models builder uses LF (https://github.com/modelsbuilder/ModelsBuilder.Original/blob/v4/dev/src/Our.ModelsBuilder/Building/CodeWriterBase.cs#L65) This is seen as a change, but when running git add this change is corrected by git and then dropped.

This is very annoying, but easy to fix with a .gitattributes file. As a base I've used the one in the UmbracoCms repository, and extended it with the path to the models builder output:

Tuesday, December 4, 2018

Remove format on paste in SXA Experience Editor

The experience editor uses the contenteditable feature. To remove formatting on paste you cannot use the Telerik RTE feature, because that one isn't used.



To solve, I've created a new Editing Theme in the medialibrary\Base Themes:



Upload the editor.js file containing:

window.$xa(document).ready(function () {
    $('[contenteditable]').on('paste', function(e) {
     e.preventDefault();
     var text = '';
     if (e.clipboardData || e.originalEvent.clipboardData) {
       text = (e.originalEvent || e).clipboardData.getData('text/plain');
     } else if (window.clipboardData) {
       text = window.clipboardData.getData('Text');
     }
     if (document.queryCommandSupported('insertText')) {
       document.execCommand('insertText', false, text);
     } else {
       document.execCommand('paste', false, text);
     }
 });
});

Then add My Editing Theme to the Editing Theme of your site:




Now you can do paste from word without keeping the content. Happy content editors, happy developers!

Cheers, Luuk

Wednesday, November 21, 2018

Kendo UI Invalid template message

Just in case you'll get:

Invalid template:'<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><dc:creator>${creator}</dc:creator><cp:lastModifiedBy>${lastModifiedBy}</cp:lastModifiedBy><dcterms:created xsi:type="dcterms:W3CDTF">${created}</dcterms:created><dcterms:modified xsi:type="dcterms:W3CDTF">${modified}</dcterms:modified></cp:coreProperties>' Generated code:'var $kendoOutput, $kendoHtmlEncode = kendo.htmlEncode;with(data){$kendoOutput='<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><dc:creator>'+($kendoHtmlEncode(creator))+'</dc:creator><cp:lastModifiedBy>'+($kendoHtmlEncode(lastModifiedBy))+'</cp:lastModifiedBy><dcterms:created xsi:type="dcterms:W3CDTF">'+($kendoHtmlEncode(created))+'</dcterms:created><dcterms:modified xsi:type="dcterms:W3CDTF">'+($kendoHtmlEncode(modified))+'</dcterms:modified></cp:coreProperties>';}return $kendoOutput;' 

With firefox I got the message 'Content Security Policy: The page’s settings blocked the loading of a resource at eval (“script-src”).'

So in your Content Security Policy policy add 'unsave-eval' to the script-src.

Cheers,
L

Tuesday, November 6, 2018

Extend Sitecore SXA metadata with hreflang and better favicon support (pt. 2 - hreflang)

When you're creating a multi lingual site, you probably want to tell the search engines to tell where to find the content in a different language. The way to do this is using hreflang.

Using the hreflang from the MetadataExtended this is really easy peasy!

You'll only have to add the hreflang rendering to the metadata partial design (like with the FaviconExtension in the previous post) manually, the rest is in the config.

The idea is that you don't use the language cookie, but always use the language from the url. To do so we need to enable the custom link manager and add a pipeline to remove the language cookie:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <linkManager defaultProvider="sitecore">
      <providers>
        <add name="ExtendedLinkProvider" 
             type="SXA.Feature.MetadataExtended.Providers.ExtendedLinkProvider, SXA.Feature.MetadataExtended" 
             cacheExpiration="5" 
             addAspxExtension="false" 
             alwaysIncludeServerUrl="false" 
             lowercaseUrls="true" 
             encodeNames="true" 
             languageEmbedding="always" 
             languageLocation="filePath" 
             shortenUrls="false" 
             useDisplayName="true">
        </add>
      </providers>
      <patch:attribute name="defaultProvider">switchableLinkProvider</patch:attribute>
    </linkManager>
    <pipelines>
      <httpRequestProcessed>
        <processor type="SXA.Feature.MetadataExtended.Pipelines.HttpRequestProcessed.LanguageCookieRemover, SXA.Feature.MetadataExtended" resolve="true" />
      </httpRequestProcessed>
    </pipelines>
  </sitecore>
</configuration>

Then enable the ExtendedLinkProvider in your SXA site, and disable language cookie support. Browse to /sitecore/content/{tenant}/{site}/Settings/Site Grouping/{site} and update the Link Provider Name. You'll also need to set the disableLanguageCookie to true:


The ExtendedLinkProvider does a bit of magic, it adds the language to the url for all languages, except for 'en'. You can always modify this behavior, but I like it this way.

To get the right localized url which uses the displayname instead of the item name (see LinkManager config above 'useDisplayName'), you'll have to do something weird, I would expect that it works when you use the language switcher it will pick up the displayname in that language. Too bad it doesn't do that, you'll need to get the language item. But the language item itself doesn't add the language in front of the url, so wrapping it around a LanguageSwitcher does the magic:

var language = Language.Parse(languageCode);

// We have to get the language item for the right display-name
var languageItem = item.Database.GetItem(item.ID, language);

// And use the Language switcher to get the right language in the url
using (new LanguageSwitcher(language))
{
    return LinkManager.GetItemUrl(languageItem); 
    // NOTE: The actual code has an option to return the full server url
    // which is needed for hreflang
}

With this, you'll get the hreflang:


If you have any questions or issues, please hit me a message below or file a ticket on:
https://github.com/luuksommers/sitecore-sxa-metadataextended

[dutch]Computer ze![/dutch]

Luuk

Saturday, November 3, 2018

Extend Sitecore SXA metadata with hreflang and better favicon support

Sitecore SXA only supports a simple favicon OOTB. Because we have a full blown mobile supported website, we want to support all the native font formats as generated by https://realfavicongenerator.net/. For this I've created MetadataExtended feature for SXA.

To use it add the _FaviconExtended template to the Settings template that has been generated for your website:


After adding this template, you'll get a lot more options in the favicon section of your settings:


Here you can upload all the files that are generated by https://realfavicongenerator.net/.

Please note that the site.webmanifest and browserconfig.xml contain a link to an icon. There files are not automatically generated in the initial version, so update them with the right media path.

Now all we need to do is add the right rendering to the medatdata partial design (/sitecore/content/{tenant}/{site}/Presentation/Partial Designs/Metadata). You can do this by adding the rendering to the /sitecore/content/{tenant}/{site}/Presentation/Available Renderings and add it using the experience editor, or directly using the Presentation Details. And voila on the favicon checker, all is green!


On the next post I'll explain the hreflang extension.

All code is available on https://github.com/luuksommers/sitecore-sxa-metadataextended

Happy faviconning,

Luuk

Friday, June 1, 2018

Patching Sitecore web.config on Azure using VSTS WebApp deploy

With Sitecore, it's a best practice to not copy the web.config to your project, but use transformations. This is a really nice idea, but when deploying Sitecore using the ARM templates to Azure and apply your solution on top of it, you cannot edit the web.config. With the solution below I want to show how to apply config transformations on Azure using the VSTS Web App Service Deploy task. The method is kinda easy once you know how to do it.

First, add the code from configtransfrom to your App_Data\tools folder, the binaries and postdeployment command are stored here. Secondly add a web.azure.config to the project, with the build action set to Content. Lastly, update the release definition and add the postdeployment task to the Web App Service Deploy.



The line in the postdeploy.cmd that does the magic is:
"%WEBROOT_PATH%\App_Data\tools\configtransform\SlowCheetah.Xdt.exe" "%WEBROOT_PATH%\web.config" "%WEBROOT_PATH%\web.azure.config" "%WEBROOT_PATH%\web.config"

To see all other available environment variables run 'set' from the kudu console.

Offcourse the source is available on Github: https://github.com/luuksommers/sitecore-azure-configtransform

Happy transforming!
Luuk