AWS Amplify - Automating Testing For A Dev Environment with Hosting - aws-amplify

Problem: A variety of tutorials from AWS for integrating automated testing with CI/CD look to integrate the testing stage after, or within the build process by using a localhost:3000 server.
However, as AWS Amplify developers know, the local environment can sometimes offer a different user experience to deploying to a development environment, and using that environment with AWS Amplify's hosting service.
Therefore, how do I add testing not only at the build stage with localhost:3000, but also for the development environment's hosted url?
I'm looking to build all my resources (backend and front-end), with a git push to the code commit repository.
Idea of Stages:
Source
Build
Test
Deploy Cloud Formation Stacks
Test
Roll back if failure
Current amplify.yml:
Note: this currently does not work so please do not copy it for your build. Please refer to the hyperlink above.
version: 0.2
frontend:
phases:
preBuild:
commands:
- npm ci
build:
commands:
- npm run build
artifacts:
baseDirectory: .next
files:
- '**/*'
cache:
paths:
- node_modules/**/*
test:
phases:
preTest:
commands:
- npm ci
- npm install wait-on
- npm install pm2
- npm install mocha mochawesome mochawesome-merge mochawesome-report-generator
- npx pm2 start npm -- dev
- 'npx wait-on --timeout 300 http://localhost:3000'
test:
commands:
- 'npx cypress run --reporter mochawesome --reporter-options "reportDir=cypress/report/mochawesome-report,overwrite=false,html=false,json=true,timestamp=mmddyyyy_HHMMss"'
postTest:
commands:
- npx mochawesome-merge cypress/report/mochawesome-report/mochawesome*.json > cypress/report/mochawesome.json
- npx pm2 kill
artifacts:
baseDirectory: cypress
configFilePath: '**/mochawesome.json'
files:
- '**/*.png'
- '**/*.mp4'

Related

How to deploy nx monorepo applications with AWS Amplify

Running into several problems while trying to deploy NextJS applications within my mono-repo to AWS Amplify-
Amplify has difficulty detecting that this is a NextJS application
Confusion on where to run build commands from / the location of dist
I found (limited) documentation on creating an amplify.yml spec for a monorepo deployment on AWS Amplify. I'm not quite sure what I want to configure particular variables as, since I'm not sure what Amplify is doing under the hood when you deploy.
Here's what I'm working with:
version: 1
applications:
- appRoot: ./apps/myNextJsApp
frontend:
phases:
preBuild:
commands:
- cd ../..
- yarn install
build:
commands:
- npx nx build myNextJsApp --prod
artifacts:
files:
- dist/apps/myNextJsApp/.next
discard-paths: yes
baseDirectory: location
cache:
paths:
- dist/apps/myNextJsApp/cache
test:
phases:
preTest:
commands:
- *enter command*
test:
commands:
- *enter command*
postTest:
commands:
- *enter command*
artifacts:
files:
- location
- location
configFilePath: *location*
baseDirectory: *location*
My specific questions:
What is the proper appRoot? It would seem to me that it would be the apps/myApp directory, however I see some documentation within Nx that it should be just ./.
Is yarn install my only necessary pre-build command? In that .yml I have it configured for starting in the apps/myApp directory and the cd'ing out to the root where my yarn.lock & package.json are located.
Is npx nx build myApp --prod sufficient?
Is this the correct location in my amplify.yml to specify my .next and cache directories?
What do baseDirectory and discard-paths do?
Thank you in advance to anyone who can help me demystify the amplify.yml.

Gitlab pipeline running into errors when adding NuGet source

I am trying to add a pipeline to my project. The project is a AWS Lambda written in dotnet. I want to build a pipeline so that it is deployed to AWS whenever I update the code of my Lambda. Here is my code for the pipeline:
image: mcr.microsoft.com/dotnet/core/sdk:3.1
stages:
- build
- deploy
- production
before_script:
- apt update
- apt-get -y install zip
- export PATH="$PATH:/root/.dotnet/tools"
- dotnet tool install -g Amazon.Lambda.Tools
build:
stage: 'build'
script:
- export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
- export AWS_DEFAULT_REGION=$AWS_DEFAULT_REGION
- export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
- echo `aws ecr get-login --no-include-email --region $AWS_DEFAULT_REGION` | sh
- dotnet nuget add sourceĀ url
- dotnet restore
- cd SmoothRunningsAuth
- dotnet lambda package -o publish/$CI_PIPELINE_ID.zip
when: always
artifacts:
name: "$CI_JOB_NAME"
paths:
- project/publish/$CI_PIPELINE_ID.zip
I have tested all of these commands locally and they work fine when I build the package. dotnet nuget add source url is required as I am using packages from a private repository I am using.
However when I push this to GitLab it returns the following errors:
I am not sure why this is happening, it runs all the other dotnet commands fine and this is the command I use when I am trying to add sources.

Cannot find file './aws-exports' in './src'

I'm on the third module of this AWS tutorial to build a React app with AWS, Amplify and GraphQL but the build keeps breaking. When I ran amplify push --y the CLI generated ./src/aws-exports.js and added the same file to the .gitignore. So I'm not surprised the build is failing, since that file isn't included when I push my changes.
So I'm not sure what to do here. Considering it's automatically added to the .gitignore I'm hesitant to remove it.
Any suggestions?
I'm assuming you are trying to build your app in a CI/CD environment?
If that's the case then you need to build the backend part of your amplify app before you can build the frontend component.
For example, my app is building from the AWS amplify console and in my build settings I have
version: 0.1
backend:
phases:
build:
commands:
- '# Execute Amplify CLI with the helper script'
- amplifyPush --simple
frontend:
phases:
preBuild:
commands:
- yarn install --frozen-lockfile
build:
commands:
- yarn build
artifacts:
baseDirectory: build
files:
- "**/*"
cache:
paths:
- node_modules/**/*
Note that the backend is building first with the amplifyPush --simple command. This is what generates the aws-exports.js file.
The 'aws-exports.js' file gets created automatically when AWS Amplify runs the CI/CD deployment build process and gets configured with the appropriate settings for the environment you are deploying to.
And for this reason it is included in the .gitignore. You don't want your local test configuration to be used in your production deployment for example.
As per Matthe's answer above the should be generated when the build script runs the 'amplifyPush' command. For some reason this is not working for me at the moment though!
AWS added support to automatically generate the aws-exports.js at build time to avoid getting the error: https://docs.aws.amazon.com/amplify/latest/userguide/amplify-config-autogeneration.html

Docker Image For Deploying DotNet Core Output to Firebase

I'm struggling to create a BitBucket Pipeline script which can compile a DotNet Core application, run it and then deploy the output html files to Firebase using the CLI.
image: microsoft/dotnet:sdk
pipelines:
default:
- step:
caches:
- dotnetcore
script:
- dotnet restore
- dotnet build MySolution.sln
branches:
master:
- step:
caches:
- dotnetcore
script:
- dotnet restore
- dotnet build MySolution.sln
- cd MySolution
- dotnet run MySolution.
- npm install -g firebase-tool
- firebase deploy --token "$FIREBASE_TOKEN"
The image microsoft/dotnet:sdk doesn't contain npm or the Firebase-Tools package I require. I'm struggling to find an alternative Docker Image which contains both dotnet and npm / Firebase-Tools. Is there a simpler way to deploy the output of the application to Firebase directly from BitBucket?
I think you should dig into the multi-step approach of Bitbucket pipelines. There you will be able to run each of the steps with different docker images.
More documentation you may find at Bitbucket here
For example:
pipelines:
default:
- step:
name: Build and test
image: microsoft/dotnet:sdk
script:
- dotnet restore
- dotnet build
- dotnet publish -o /out -c Release
artifacts:
- out/**
- step:
name: Run npm
image: node:8
script:
- npm install -g firebase-tool
- firebase deploy --token "$FIREBASE_TOKEN"
- step
name: Run app
image: microsoft/dotnet:sdk
script:
- dotnet run MySolution .

Error during deploy from pipeline on firebase

I try to deploy my angular application on firebase using gitlab pipeline.
I followed this guide and I write a gitlab-ci file more simplified for starting.
image: node:latest
cache:
paths:
- node-modules/
stages:
- deploy
deploy:
stage: deploy
environment:
name: development
script:
- npm install -g firebase-tools
- npm install
- npm run builddev
- npm run deploy
only:
- master
Everything work until last command when I have this error
but if from terminal I run
firebase deploy --project test-1c121
everything work fine
Check your package.json file in scripts section. You probably have a typo in deploy because npm runs into problem with running firebse command and you want to run firebase.

Resources