GitHub Actions for MongoDB Atlas App Services
Tags: githubhack23, atlasappservices, mongodbatlas, astro
Published: 2023-05-23 09:11:59

## What I built 
I created GitHub Actions for MongoDB Atlas App Services. There are two jobs: 

1. build Astro to generate static files
2. copy static files and push them to MongoDB Atlas App Services.

### Category Submission: 
**DIY Deployments**

### App Link: 
[Click](https://frontend-app-hqqpl.mongodbstitch.com)

### Screenshots 
There are two parts: GitHub Codespaces and GitHub Actions.

## GitHub Codespaces

1. Open GitHub Codespaces link to GitHub
![Setting up GitHub Codespaces](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/246n8iyis5if4cc6bnft.png)

2. Open the terminal inside GitHub Codespaces and run below command
`npm create astro@latest -- --template framework-svelte`

3. Update svelte.config.js
![Update svelte config](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kcpz83g187iy1l0h20xt.png)

4. Update package.json
![Update package.json](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bxv9vcjdqdir7qp1hib7.png)

5. Run `npm run dev`

6. you can open browser after run no 5.
![website](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1s1ejzi0cykq74gmyhc0.png)

7. create a GitHub Actions workflow.
![gha](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ovwd27ditk64e5hencnp.png)

```YAML
name: deploy-mdb-atlas-app-service
run-name: ${{ github.actor }} deploys an app on the Atlas App Service
on: [workflow_dispatch]
jobs:
  build-astro-svelte:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: '18'
      - run: npm i
        working-directory: ./fe 
      - run: npm run build
        working-directory: ./fe
      - run: mkdir -p ./data/files
      - run: cp -R ./fe/dist/* ./data/files
      - uses: actions/upload-artifact@v3
        with:
          name: build-artifact
          path: ./data
  deploy-to-aas:
    runs-on: ubuntu-latest
    needs: build-astro-svelte
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: '18'
      - run: npm install -g mongodb-realm-cli
      - run: mkdir -p build-artifact
      - uses: actions/download-artifact@v3
        with:
          name: build-artifact
          path: build-artifact
      - run: realm-cli login --api-key ${{ secrets.API_KEY }} --private-api-key ${{ secrets.PRIVATE_API_KEY }} -y
      - run: realm-cli pull --remote ${{ vars.APP_ID }}
      - run: cp -R ./build-artifact/* ./${{ vars.APP_NAME }}/hosting
      - run: realm-cli push --remote ${{ vars.APP_ID }} -s -c -y
        working-directory: ./${{ vars.APP_NAME }}
```

After create dev and GitHub Actions on GitHub Codespaces, we will prepare MongoDB Atlas App Services.

1. Sign-in on https://cloud.mongodb.com/.

2. Go to Data services and "Create"
![mdb1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/apcpu8y5rjy19gyyrm3d.png)

3. Create a paid MongoDB cluster.
![mdb2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8wwg5bw5byw2ckjnxhkb.png)

4. Create an app services
![mdb3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xcu6xc6rrbnatm7tdw9f.png)

5. Enter required information and "Create".
![mdb4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/po6qyx7hgejyud0j9tzo.png)

6. Enable hosting.
![mdb5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r075wecq59vyjjsbqqyw.png)

7. Copy APP ID.
![mdb6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z1z4se9u1okom8yeof4u.png)

8. Create API KEY for GitHub Actions.
![mdb10](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/25g8qki5pf3nzvgbqknl.png)

9. Enter required information.
![mdb11](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2z9zzhrelliu6xbmlduh.png)

10. Copy and Save the "Public and Private API key"
![mdb12](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9eel2n0txnlkrt8inbcn.png)

After prepare MongoDB Atlas App Services, we will create secrets & variables on GitHub settings.

1. Go to "New repository variable"
![mdb7](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aysz6pfyvgrq1qnfb7h6.png)

2. Create APP_NAME variable.
![mdb8](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d6p5v2skd0wuz2hgqtjo.png)

3. Create APP_ID variable
![mdb9](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6efmcm4mytbkz0jhcbd3.png)

4. Create APP
![mdb13](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s0cmj980ewh9489ctfsn.png)

5. Go to "New repository secret"
![gha1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eolfgvbeu066losjrtl6.png)

6. Create API_KEY secret.
![gha2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/treauk80j8zkhk9am0bm.png)

7. Create PRIVATE_API_KEY secret.
![gha3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lezigj8f148x3vregtli.png)

8. Run workflow manually for deployment.
![gha4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5aj0gwe0wjbflr5g3gi3.png)

9. If workflow runs passed, you can see files in hosting and can see the result on the website.

![gh5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/la4pcjjvzihkdlja8vza.png)

10.
 Website
![website](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1s1ejzi0cykq74gmyhc0.png)


### Description 
This application runs on MongoDB Atlas App Services (Hosting). It is a service for static files. I will show how to use GitHub Actions to create CI/CD pipeline for Atlas App Service deployment. I also demonstrate how to use GitHub Codespaces for testing deployment to run Astro.

### Link to Source Code 
[GitHub repo](https://github.com/ninefyi/astro-gha-mdb-hosting)

### Permissive License: 
**MIT**

## Background (What made you decide to build this particular app? What inspired you?) 
My inspiration is the less content about MongoDB Atlas App Services and how to use GitHub Codespaces and GitHub Actions to create an automated deployment frontend to MongoDB Atlas App Services.

### How I built it (How did you utilize GitHub Actions or GitHub Codespaces? Did you learn something new along the way? Pick up a new skill?) 
I used GitHub Codespaces for development and GitHub Actions to deploy static files to MongoDB Atlas App Services. I learned how to test my front-end website using GitHub Codespaces. It is a free tier, and I do not need to install any software on my laptop. I learned how to use secrets and variables to hide my key from MongoDB Atlas for GitHub Actions. It looked seamless between GitHub Codespaces and GitHub Actions.

### Additional Resources/Info
- [Expose port 3000 for GitHub Codespaces](https://dev.to/kkoziarski/react-vite-github-codespaces-5529)
- [Realm-Cli](https://www.mongodb.com/docs/atlas/app-services/cli/)