The goal of this assignment is to set up both a continuous integration (CI) service and a continuous deployment (CD) for your project repository. Both services can be triggered using Github Actions (recommended). The CI service will execute entirely on Github, while the CD service will additionally require an account on a cloud provider such as Microsoft Azure.
We will start by setting up CI, and then in the second part, move on to CD.
Part I: Continuous Integration
Which CI service should I use? In our context, GitHub Actions is my first option (as demonstrated in class). Other good options are TravisCI and CircleCI, especially if you have a public repo. Most of the other cloud providers (Google, AWS, Azure) will probably ask you to add a credit card to your account (even in a free-tier), so I’d avoid that for now.
What should my remote build run? The remote build should check out the code, install any dependencies, build it, and test it.
- You should install dependencies with the command
npm cifor clean install (more info about this command here). - If any code needs to be compiled and bundled, the remote build will run
npm run build. (If you are using the same stack we used before, only the frontend will require building.) - Next, the build will check code style compliance. The command depends on which tool you use, but we will define ~npm run lint` to perform these checks.
- Finally, the build will run any automated tests (command
npm test).
Also, note that the CI server doesn’t host a database in its file system. So, your test cases must either make calls to the cloud database, mock the database calls, or use the in-memory db.
Step 1. Edit your package.json file
If you have test cases, make sure your test command is running on the “test” target under npm scripts in your package.json file. If you don’t have test cases, that’s fine. You can also run prettier and eslint checks.
You will configure the CI server will run both your “lint” and “test” scripts. Note that the default “test” script added by npm init returns a non-zero status, which will cause the CI build to fail.
"test": "echo \"Error: no test specified\" && exit 1",
So if you don’t have any tests you will either have to edit this line (e.g., remove the && exit 1), or remove the test line from the YAML file (see below).
Your “lint” target in the package.json should run eslint and prettier checks.
Here’s the script section of my package.json file (replace filenames according to your project files):
`"scripts": {
"lint": "npx eslint . && npx prettier --check ."
"test" "npx jest",
"dev": "nodemon backend_with_db.js",
"start": "node backend_with_db.js"
},
Step 2. Create the YAML file
The YAML file is the configuration file that will inform the CI server on how to spin up the remote environment by loading up libs and running tests and any sort of checks (e.g., coding style checks).
If using GH Actions, you can either generate this file by using the GH web interface (GH Actions tab, then choose Node.js action) or by locally creating this file and then pushing it to remote (which is what I recommend).
Name your YAML file as ci-testing.yml and have it created in the following folder <your proj root folder>/.github/workflows. If you use GH interface, it’ll automatically place the file in that folder. Note that, if you have multiple branches, you have to choose which branch(es) you want your remote builds.
Here is a sample YAML file you can use to get started:
name: CI Testing
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm run -w express-backend lint
- run: npm run -w react-frontend lint
- run: npm run -w react-frontend build
Notes:
- The YAML format is similar to JSON, except that it uses indentation instead of braces to indicate nesting, and you don’t need quotes around most things. YAML is commonly used for DevOps tools because it is easier to manually edit and read.
- This YAML file specifies to run the job whenever there is a push on branch
main. You can specify other branches, and also trigger on pull requests instead of (or in addition to) pushes. For Node.js builds, you can use theactions/setup-node@v3which allows you to specify the node version you want to use. Try runningnode --versionin your development environment and select the same version here. The example provided runs all thenpmscripts in the root folder of the target branch. This is wherenpmworkspaces become handy, because we can run each of our subpackages’ scripts from the project root using the-woption. For more info, check out (Building and testing Node.js - GitHub Docs).
Step 3. Trigger remote builds
At this point, if your YAML file is correct, placed in the right folder and branch, and pushed to your Github remote, then all future pushes (and/or pull requests) will trigger remote builds.
Go ahead and push a change. Navigate to the GitHub Actions tab and look through the remote build. If the build doesn’t pass, you should be able to inspect the log and find out the reason. If it passes, great job, and take advantage of this feature for your dev workflow.
Part II: Continuous Deployment
Deployment should be triggered every time the CI build passes. So, you need to have Part I working first. Once your CI is able to produce a successful build, you are ready to proceed with the next step.
Step 1. Create a Microsoft Azure Account
Microsoft Azure is a cloud services provider that includes web app hosting. You can use free credits with Azure for Students Sign up with your calpoly.edu email address (no credit card required).
if your app connects to a database, you need to use a cloud database (such as MongoDB Atlas or Airtable) or host the database yourself on a remote server. This is because your app will not be able to access localhost once it is running in the cloud. Therefore, you will need to modify the code to connect to a remote database. Azure Web Apps supports custom environment variables, So, you can use environment variables to define the database URI. Do not store any database credentials in your git repo!
Step 2. Set up an Azure Web App
You will need to create an Azure Web App for your project and link it to your GitHub repo. Let’s start with the backend app, and set up an Azure Web App for hosting your REST API. (We’ll deploy the frontend later.)
From the Azure dashboard, click + Create a resource and click ‘create’ under "Web App."Then, fill out the Name of your app. This name will be part of the URL. For instance, the app name “people-api” will lead to the URL “people-api.azurewebsites.net”. Then choose your runtime stack (the highest version of Node, probably 18 LTS), and your region (I used “West US 3” as there were no instances available for “West US” or “West US 2”).
Next, click Next: Deployment at the bottom of the page. Change Github Actions Settings -> Continuous Deployment to “Enable”. Next, select Authorize and log in to the same Github account where your repository is. Then select your organization (probably the same as the account), the repo where your project is, and branch you want deployed (probably main).
You can now click Review + create at the bottom of the screen. Review the details on the next screen and then click Create. If all goes well, this will link to your repo and create a GitHub action to deploy to Azure, and will also create an API key so that GitHub can communicate with your Azure account.
Once you see the screen saying that the deployment is complete, click on Go to Resource to see the dashboard for your deployed instance. You will need to copy the URL listed under Default Domain and put it in your frontend code in place of localhost:8000 for your frontend to use your new API server.
Note: These instructions follow the Azure web interface, and give Azure authorization to push to your repo (e.g., it’ll push a new YAML file). If you want to do that manually, follow these other instructions: (Configure CI/CD with GitHub Actions - Azure App Service | Microsoft Learn).
Step 3. Assigning Environment Variables on Azure
Since the cloud host is hosting the app only, not the database, you’ll need another remote server to host your database. If you haven’t moved your database from local to the cloud, work on that part before you continue here. There are separate lab instructions to do so.
If you have your database hosted on the cloud, you should have at this point the connection string that points to your remote database (the environment variable and the connection string you use in your local environment will be used here).
On Azure dashboard, on the left-side menu, click on Configuration under Settings. This should navigate to the page “Application Settings.” You will want to enter any settings that you have in your .env file here. Click on “New Application Setting” to create a new environment variable. On the name field, type your environment variable name (e.g., MONGODB_URI), and for the value from your .env file. Then click Ok and Save on the top. With that, the remote cloud machine should provide the same environment variable(s) that you use when running locally.
Step 4. Deployment: Changing the Express app port
If you are deploying the Express backend project we need to make a slight change to the port used. Azure will assign the port number rather than us having a port number hard-coded. Azure does that automatically using their own environment variable named PORT. So, our app.listen() function call will be:
app.listen(process.env.PORT || port, () => {
console.log("REST API is listening.");
});
You can still have your statement const port = 5000 but note that we added an OR. It’ll be either whatever port is assigned in the environment (env var PORT) or what you defined in the const port. If your code already has it, great, no changes are required.
Commit and push these changes to the repo. At this point, you can also check if these changes triggered a successful build.
Modify deployment workflow to work with monorepo
When you connected Azure to your Github repo, above, it pushed a new workflow YAML file into your .github/workflows folder. We need to make a change there to tell Azure which workspace to build.
Pull the main branch from Github so you can edit this new file. You will see that one of the steps in the workflow looks like this:
- name: npm install, build, and test
run: |
npm install
npm run build --if-present
npm run test --if-present
You probably want to make sure that only your backend package (or NPM workspace) is built and tested at this point, because we are only deploying the backend. In addition, you want to only deploy this one package, and not the entire monorepo! To make that work, you will need to add some options to these npm commands, in addition to a working-directory folder . So the “npm install, build, and test” step should look like this:
- name: npm install, build, and test
run: |
npm install --workspaces=false
npm run build --if-present
npm run test --if-present
working-directory: packages/express-backend
Be careful with the indentation, because indentation is significant in YAML files. In the above example, name, run, and working-directory should all align vertically (same character position on line).
There is one more change to make to the YAML workflow that Azure created in your repo. In the step after “npm install, build, and test”, Github actions will create a zip file of your built package, which is the artifact it will copy to Azure. You want the root of this artifact to be your backend package, not your monorepo’s root. As you did above for “npm install, build, and test”, add a working-directory setting to the “Zip artifact for deployment” step. Also, you want the artifact itself to be created in the root of your project, so add ../.. before the release.zip. It should end up looking like this:
- name: Zip artifact for deployment
run: zip ../../release.zip ./* -r
working-directory: packages/express-backend
Deploying the frontend app as static web pages
The process is somewhat similar. The difference is that for the frontend, we’re deploying the compiled version of your React code, which means all the static content in HTML and JavaScript. This is generated if you run npm build on your frontend project (a build folder is created). The cloud VM will do the same and host what’s generated after the build.
On the Azure web interface Home page, click on Create a Resource as we did for the backend, but this time click on Create a Static Web App. If you don’t see this option, on the search box, type “static web app.” On the new page, give a name to your app, choose a region which is close to your backend (e.g., US West), and make sure the source is GitHub (and your GH account should be assigned if you did the backend set up previously). Then, point to your repository and branch.
Finally, click on Review + create to finish the process. Once the deployment is complete, click on Go to Resource. You will see the URL of your frontend server listed there, as well as a link labeled View app in browser. Click this link and you will see a splash screen that says welcome to your app. Unfortunately, you don’t see your app itself. To get point it at your app, you need to modify the YAML file that Azure created.
Find the file in .github/workflows that starts with azure-static-web-apps-. (You will need to do a git pull because the file is created on GH origin directly.) Now look for the lines that say app_location: and output_location. You need to set these to ./packages/react-frontend and dist, respectively. You do not need to set api_location:. It should look like this:
###### Repository/Build Configurations - These values can be configured to match your app requirements. ######
# For more information regarding Static Web App workflow configurations, please visit: https://aka.ms/swaworkflowconfig
app_location: "./packages/react-frontend" # App source code path
api_location: "" # Api source code path - optional
output_location: "dist" # Built app content directory - optional
###### End of Repository/Build Configurations ######
Push this change, which will trigger a redeploy. When it finishes you should see your app at the location shown for your static web app URL.
Note: Your deployed frontend app should be making calls to the deployed backend URL. Check in Dev Tools whether your frontend is attempting to make calls to the localhost backend, which won’t work for security reasons. If this is the case, change the URL accordingly on your frontend code. Instead of
localhost:8000, use the URL given in Azure on the dashboard for your backend deployment.
Turning it In
To turn in this assignment, you will need to provide the URL of your team’s Github repo (main branch, unless you have configured your CI/CD to run off another branch), as well as the URL of your deployed app (on Azure or other cloud provider you are using).
Only one submission per team is required.