Hi there,
I’m trying to deploy a function to Netlify using NetlifyAPI, but there was an “Initial file records don’t match the uploaded ones” error which I have no idea about.
Here’s what I’ve done:
- Create a new site using API
curl --location --request POST 'https://api.netlify.com/api/v1/sites' \
--header 'Authorization: Bearer xxx' \
--header 'Content-Type: application/json' \
--data-raw '{
"name":"hello-world-1111"
}'
Response:
{
"id": "d7d6dfb0-d116-4e80-a6c2-625f84a1e00e",
"site_id": "d7d6dfb0-d116-4e80-a6c2-625f84a1e00e",
"plan": "nf_team_dev",
"name": "hello-world-1111",
"sso_login": false,
"url": "http://hello-world-1111.netlify.app",
"admin_url": "https://app.netlify.com/sites/hello-world-1111",
"deploy_url": "http://63835e99adf1a07070911039--hello-world-1111.netlify.app",
"state": "current",
"created_at": "2022-11-27T12:56:57.399Z",
"updated_at": "2022-11-27T12:56:57.479Z",
"user_id": "5ccd27cab98081949c79a700",
"ssl": false,
"ssl_url": "https://hello-world-1111.netlify.app",
...
}
-
Run
netlify functions:create
to init a sample ts function using “hello-world” template in netlify CLI
-
Run
netlify build
create a zip file -
Get the hash of the zip file by passing the file path to this function:
function checksumFile(path) {
return new Promise(function (resolve, reject) {
let hash = createHash('sha256').setEncoding('hex');
fs.createReadStream(path)
.once('error', reject)
.pipe(hash)
.once('finish', function () {
resolve(hash.read());
});
});
}
- Create a deploy using API
Request:
curl --location --request POST 'https://api.netlify.com/api/v1/sites/d7d6dfb0-d116-4e80-a6c2-625f84a1e00e/deploys' \
--header 'authorization: Bearer xxxx' \
--data-raw '{"functions":{"helloo":"a0436a6b46585ebb5e59b98c5b2daa5633b37b0ddbf52c7f05ef1c7204c3e344"}}'
Res:
{
"id": "63836616603c3c62e02c0231",
"site_id": "d7d6dfb0-d116-4e80-a6c2-625f84a1e00e",
"build_id": null,
"state": "uploading",
"name": "hello-world-1111",
"url": "http://hello-world-1111.netlify.app",
"ssl_url": "https://hello-world-1111.netlify.app",
"admin_url": "https://app.netlify.com/sites/hello-world-1111",
"deploy_url": "http://63836616603c3c62e02c0231--hello-world-1111.netlify.app",
"deploy_ssl_url": "https://63836616603c3c62e02c0231--hello-world-1111.netlify.app",
"created_at": "2022-11-27T13:28:54.108Z",
"updated_at": "2022-11-27T13:28:54.326Z",
"user_id": "5ccd27cab98081949c79a700",
"error_message": null,
"required": [],
"required_functions": [
"a0436a6b46585ebb5e59b98c5b2daa5633b37b0ddbf52c7f05ef1c7204c3e344"
],
...
}
- Finally uploading the ZIP file
Request:
curl --location --request PUT 'https://api.netlify.com/api/v1/deploys/63836616603c3c62e02c0231/functions/helloo?runtime=js' \
--header 'authorization: Bearer xxxx' \
--data-binary '@/Users/hughdo/Desktop/test/nextauth_netlify/functions-template/helloo.zip'
Response:
{
"name": "helloo"
}
- There was no error returned but when I went to the
deploys
page, I saw this:
“Production: Initial file records don’t match the uploaded ones”
Note: I’ve tried several ways to create ZIP file such as: zip the whole folder(except node_modules) or use esbuild
to build the function then zip it.
Does anyone know about this issue? Did I do something wrong?
Thanks in advance.