My project makes use of Amplify auth and analytics, and therefore has an aws-exports.js. I'm using GitHub actions to trigger tests on feature branches (to disable the merging of test-breaking code). The project uses the Amplify Console for CD. Before building the code, I execute the following script, which––if aws-exports.js exists––copies aws-exports.js to an aws-exports.ts. If aws-exports.js doesn't exist, the script writes export default {}; to aws-exports.ts. This way, we avoid a build error when running in GitHub's CI environment. This is also good for users who don't want to run amplify init to hack on this site locally.
import * as path from "path";
const clientDir = path.join(__dirname, "../client");
const awsExportsPathWithoutExtension = path.join(clientDir, "src/aws-exports");
const awsExportsJSPath = [awsExportsPathWithoutExtension, ".js"].join("");
const awsExportsTSPath = [awsExportsPathWithoutExtension, ".ts"].join("");
try {
const awsExportsJSContents = fs.readFileSync(awsExportsJSPath, {
encoding: "utf8",
});
fs.writeFileSync(awsExportsTSPath, awsExportsJSContents, {encoding: "utf8"});
console.log("aws exports found and copied!");
} catch (e) {
fs.writeFileSync(awsExportsTSPath, "export default {};", {encoding: "utf8"});
console.log("aws exports not found and supplemented");
}
Locally, the above script works as expected both when the aws-exports.js is and isn't there.
Although aws-exports.js is "supposed" to be present in the Amplify Console container's file system, the read operation fails––we see "aws exports not found and supplemented" in the logs. This should only be the case in the GitHub test workflow. To my knowledge, nothing should prevent the retrieval of aws-exports.js contents. Any ideas on what's happening here?
@harrysolovay please share your buildspec as well (App settings > Build settings)
version: 0.1
frontend:
phases:
preBuild:
commands:
- yarn install
build:
commands:
- yarn build
artifacts:
# IMPORTANT - Please verify your build output directory
baseDirectory: /
files:
- "**/*"
cache:
paths:
- node_modules/**/*
Other than not reading aws-exports.js, the site is built and deployed correctly.
Update your buildspec to add https://docs.aws.amazon.com/amplify/latest/userguide/build-settings.html#frontend-with-backend
@harrysolovay, did this work for you?
Yep! I added the following to my build spec:
backend:
phases:
build:
commands:
- amplifyPush --simple
Worked like a charm. Thanks!
Update your buildspec to add https://docs.aws.amazon.com/amplify/latest/userguide/build-settings.html#frontend-with-backend
Thanks, i had the same problem and this did it for me. I wonder why this step is never mentioned in the official docs https://docs.amplify.aws/
Same here @truefalse10 @harrysolovay - it feels as this is a critical component of cicd deploying and is brushed over. I'd suggest that it be in the build spec by default when detected to have an amplify backend.
version: 0.1
backend:
phases:
build:
commands:
- amplifyPush --simple
frontend:
phases:
preBuild:
commands:
- yarn install
build:
commands:
- yarn build
artifacts:
# IMPORTANT - Please verify your build output directory
baseDirectory: /
files:
- "**/*"
cache:
paths:
- node_modules/**/*