Skip to content

johnpapa/cloud-coding-with-codespaces

Repository files navigation

Cloud Computing with Codespaces

This is a project template for demonstrating github.dev, Codespaces, Co-pilot, Azure Static Web Apps, and Visual Studio Code.

Top Features

  • Start quickly with github.dev: a fast, free and lightweight VS Code editor in the browser
  • Enjoy git integration to create branches and pull requests
  • Continue working on Codespaces to build, run, test and debug
  • Test your app with Playwright
  • Share your running app by forwarding ports
  • Extend your code with GitHub Copilot
  • Deploy your app to Azure Static Web Apps

The Story and What You'll Learn

github.dev

Setup

Before presenting this demo, be sure to create your own repository from the template, following these instructions:

  1. Generate your repository in your organization, by clicking here.

    This will open in a new tab where you can fill out the name and other details

  2. Name your repository and continue

Demo Story

Just One More Change ... It's a familiar story ...

You just received an urgent request from your project stakeholders to fix the web app. You need to jump directly into the project and correct a mistake where the site is selling a bag of a 1000 lemons when it's only supposed to be 10 per bag!

Compounding the urgency, you need to make the changes and you don't have the local development environment with Node.js and all of the required project dependencies. Not to worry though as you know you can use github.dev in the browser to make these changes.

Existing App

Adding the product in github.dev

  1. Browse to the repository on GitHub at https://github.com/johnpapa/cloud-coding-with-codespaces.

  2. Press . and the code instantly opens in github.dev, a fast and free lightweight VS Code editor in the browser.

    github.dev

    Now you want to create your changes in separate branch and generate a new pull request.

  3. Press <F1> to open the command palette.

  4. Type and select Create New Branch and name it fix/products

  5. Press <ENTER> and select Switch to Branch and Leave Site to refresh to the new branch.

  6. Open the file containing the products in product-data.js.

  7. Edit the description for lemons to the proper quantity of 10 per bag.

       "description": "Bag of 1000 ripe lemons",

    to

       "description": "Bag of 10 ripe lemons",
  8. This is a simple change so you're comfortable to commit the file changes.

Transition to Codespaces to build and run

While you're editing the product-data.js file you are communicating with your stakeholders to let them know the change is being made. Since you're in there, they ask if you can add a new entry for a new product, hot peppers! Of course you can do this, but you'll feel more confident if you can build, run and view the app to verify that it works.

  1. While in the same branch, open the file containing the products in product-data.js.

  2. Add the missing product for hot peppers and save the file.

     {
       id: 40,
       name: 'Hot Peppers',
       description: '8 oz package of hot peppers',
       quantity: 711,
       imageClass: 'fas fa-pepper-hot fa-3x'
     }
  3. Now commit these changes.

  4. You want to see the application run to make sure the hot peppers are displayed, so you open press F1 and enter the command View: Toggle Terminal intending to build and run the app. Uh oh, there is no terminal!

    So far we've been working entirely with the files in the browser. But now you need compute power to run the app and debug it. You can seamlessly transition from github.dev to the full power of compute in the cloud. Let's see how.

  5. Press the button Continue Working On ... and then select Create New CodeSpace to be transitioned to Codespaces.

    The browser refreshes and you're now working with compute resources which allow you to run, debug, and use the terminal all with the environment you need in the cloud. It even ran npm install to install your dependencies!

    Note: You're using the default devcontainer for Node.js. You can create a custom devcontainer, by following the link in the terminal comments. But that's for another day.

    create codespace
  6. Now let's run our Angular app with npm run start and our API with npm run local.

  7. Select the Ports tab in the Terminal pane.

  8. Our app is running on port 4280. Click the globe icon to browse to it. We're now building, running, and browsing to our app entirely in the browser in the cloud!

Forward your ports publicly to your stakeholders to view them

  1. Select the Ports tab in the Terminal pane.

  2. Notice the visibility is set to private by default. You will right click and select Port Visibility and Public

    Setting the port to public allows your stakeholders to see the app from the URL.

    make the port public
  3. Now the URL can be shared with your stakeholders.

    Your stakeholders review the running app and they're quite pleased ... until they notice that the products are not sorted in the proper ascending order by name. Uh oh, you're going to have to fix that! But you can't remember the exact syntax to sort the JavaScript array. Maybe GitHub Copilot can help you do this.

Sort the products using the help of Copilot

  1. Press <F1> to open Codespaces' command palette.

  2. Select View: Show Extensions

  3. Search for GitHub Copilot, press Install, and agree to the terms

  4. Open the file api/products-get/index.js and put the cursor directly after the code gets the products.

  5. Type // to engage with Copilot

  6. Type // sort the products by name in ascending order, and hit <TAB> and <ENTER> to accept each line.

  7. You're prompted line by line for the code to sort the products. It looks appropriate, so click <ENTER> on each line to accept it, until it is complete.

  8. Comment the old line of code to render the products, now that you have replaced it.

    const data = require('../shared/product-data');
    
    module.exports = async function (context, req) {
      try {
        const products = data.getProducts();
        // sort the products by name in ascending order
        products.sort((a, b) => {
          if (a.name < b.name) {
            return -1;
          }
          if (a.name > b.name) {
            return 1;
          }
          return 0;
        });
    
        context.res.status(200).json(products);
      } catch (error) {
        context.res.status(500).send(error);
      }
    };

    Optional: Enter <^> + <ENTER> to see a list of suggestions that Copilot synthesizes for you! This requires enabling CodeLens in Settings.

  9. Open the browser tab, refresh, and see your changes worked!

    Copilot just helped you write the code to sort the products directly in Codespaces in the browser.

    final app
  10. Now let's run our end-to-end tests using Playwright to make sure we haven't broken anything, with npm run test.

Merge your changes

  1. Stop your app (in the terminals).
  2. Commit your changes and push them to the branch.
  3. Open the Command Palette <F1>
  4. Type and select GitHub Pull Requests: Create Pull Request
  5. Click the Create button to create the Pull Request
  6. Optionally, review, approve, and merge the pull request to the main branch.

Optional - Deploy to Azure Static Web Apps

You can browse to https://brave-tree-0bfbe121e.1.azurestaticapps.net

Open the Codespace in your local install of VS Code

  1. Click the Codespaces icon in the bottom left

  2. Choose Open in VS Code

  3. Accept the confirmation dialogs

Now you are using VS Code locally to develop with your codespace!

Recap

Let's look back at what we were able to accomplish, all without having the local dev environment. You can edit files, create a new branch, and merge a PR all from github.dev in the browser. Then when you need a robust dev invironment to run your application, you can continue working on it in Codespaces. Here you can debug it and use tools like GitHub Copilot to help write the code your app needs, all from within the browser.

Credits

About

Live demo using Angular, github.dev, codespaces, copilot, azure static web apps, and devcontainers

Resources

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published