Exercise - Create and merge a branch

Completed

Back at the toy company, your website developers plan to add a new Azure Cosmos DB database to store data about the toys that the company sells. The developers asked you to update the Bicep code to add the Cosmos DB resources. However, they're not ready to make the changes yet. They just want you to get the changes ready for when they finish the modifications.

In this exercise, you'll add a new Bicep module on a branch of your repository. During the process, you'll:

  • Create a branch and switch to it.
  • Change your Bicep code on the branch.
  • Switch back to your main branch.
  • Merge your branch to main.

Create and check out a branch in your repository

  1. By using the Visual Studio Code terminal, run the following command to create and check out a new branch:

    git checkout -b add-database
    
  2. Run the following command to check the status of the repository:

    git status
    

    The output looks similar to the following example:

    On branch add-database
    nothing to commit, working tree clean
    

    The first line of the output tells you that Git is on the add-database branch.

  3. In Visual Studio Code, look at the status bar at the bottom, left side of the window. Notice that the branch name changed to add-database.

    As with the other Git commands you've run, Visual Studio Code stays up to date with the changes in your Git repository, including when you check out a branch.

Update a file on your branch

Now that you've created a branch, you'll add a new Bicep module for your website's Azure Cosmos DB account.

  1. In the deploy folder's modules subfolder, create a new file named cosmos-db.bicep.

  2. Open and save the empty cosmos-db.bicep file so that Visual Studio Code loads the Bicep tooling.

  3. Copy the following code into cosmos-db.bicep:

    @description('The Azure region into which the resources should be deployed.')
    param location string
    
    @description('The type of environment. This must be nonprod or prod.')
    @allowed([
      'nonprod'
      'prod'
    ])
    param environmentType string
    
    @description('The name of the Cosmos DB account. This name must be globally unique.')
    param cosmosDBAccountName string
    
    var cosmosDBDatabaseName = 'ProductCatalog'
    var cosmosDBDatabaseThroughput = (environmentType == 'prod') ? 1000 : 400
    var cosmosDBContainerName = 'Products'
    var cosmosDBContainerPartitionKey = '/productid'
    
    resource cosmosDBAccount 'Microsoft.DocumentDB/databaseAccounts@2022-05-15' = {
      name: cosmosDBAccountName
      location: location
      properties: {
        databaseAccountOfferType: 'Standard'
        locations: [
          {
            locationName: location
          }
        ]
      }
    }
    
    resource cosmosDBDatabase 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2022-05-15' = {
      parent: cosmosDBAccount
      name: cosmosDBDatabaseName
      properties: {
        resource: {
          id: cosmosDBDatabaseName
        }
        options: {
          throughput: cosmosDBDatabaseThroughput
        }
      }
    
      resource container 'containers' = {
        name: cosmosDBContainerName
        properties: {
          resource: {
            id: cosmosDBContainerName
            partitionKey: {
              kind: 'Hash'
              paths: [
                cosmosDBContainerPartitionKey
              ]
            }
          }
          options: {}
        }
      }
    }
    
  4. Save and close the cosmos-db.bicep file.

  5. Open the main.bicep file.

  6. Add the following parameter definitions below the appServiceAppName parameter definition:

    @description('The name of the Cosmos DB account. This name must be globally unique.')
    param cosmosDBAccountName string = 'toyweb-${uniqueString(resourceGroup().id)}'
    
  7. Add the following module definition below the appService module definition:

    module cosmosDB 'modules/cosmos-db.bicep' = {
      name: 'cosmos-db'
      params: {
        location: location
        environmentType: environmentType
        cosmosDBAccountName: cosmosDBAccountName
      }
    }
    
  8. Save and close the main.bicep file.

Review the differences and commit the changes

After you review the file differences, stage and commit your changes. You can choose whether to use the Git CLI or Visual Studio Code to commit the files. This example uses the Git CLI.

  1. Using Source Control in Visual Studio Code, look at the differences for both files.

    Notice the changed lines highlighted in the main.bicep file.

  2. Review the files that are ready to commit.

    git status
    

    The output will look like the following example.

    On branch add-database
     Changes not staged for commit:
       (use "git add <file>..." to update what will be committed)
       (use "git restore <file>..." to discard changes in working directory)
             modified:   deploy/main.bicep
    
     Untracked files:
       (use "git add <file>..." to include in what will be committed)
             deploy/modules/cosmos-db.bicep
    
     no changes added to commit (use "git add" and/or "git commit -a")
    
  3. Stage the changes for both files.

    git add .
    

    The dot (.) stages all files that were changed.

  4. Commit the changes.

    git commit --message "Add Cosmos DB module"
    

    The output will look like the following example.

    [add-database 513f700] Add Cosmos DB module
      2 files changed, 71 insertions(+)
      create mode 100644 deploy/modules/cosmos-db.bicep
    

Switch branches

Now that you've made the changes on your branch, you can verify that the changes are visible only on the add-database branch.

  1. Check out the main branch. You can choose either of the following approaches:

    • In the Visual Studio Code terminal window, enter the following command:

      git checkout main
      
    • In the Visual Studio Code status bar at the bottom of the window, select the branch name that currently displays add-database.

      A list of branches appears. Select the main branch.

  2. In the Visual Studio Code Explorer pane, open the main.bicep file.

    Notice that none of the Azure Cosmos DB changes you made are included. Now that you've switched to the main branch, the database module isn't there. Don't worry, they're safely stored on your add-database branch.

Merge your branch

Your website team has tested the changes, and is now ready to launch the updated website with the Azure Cosmos DB database included. You'll merge the add-database branch into the main branch.

  1. Verify that you're on the main branch by running git status and by looking at the branch name in the status bar.

  2. In the Visual Studio Code terminal, enter the following command to merge the changes from the add-database branch onto the main branch:

    git merge add-database
    
  3. In the Visual Studio Code Explorer pane, open the main.bicep file.

    Notice that the database module now appears in the file. You've now updated your known-good Bicep files, on your main branch, to include the changes from your add-database branch.

  4. In the Visual Studio Code terminal, enter the following command to delete the add-database branch because you no longer need it:

    git branch -d add-database