New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deployment messes up the dev environment because everything happens in the same folder #4563
Comments
Hi @mnapoli , Could you explain the problem at a more detailed level? The Serverless framework uses a temporary directory As I understand, you're writing a plugin - if your plugin creates and owns the cache files, it is also its responsibility to keep them somewhere separated by stages. |
Thanks for the detailed answer I really appreciate it. Here is an example of the plugin I'm writing: class PhpServerlessPlugin {
constructor(serverless, options) {
this.hooks = {
'before:package:createDeploymentArtifacts': this.build.bind(this),
};
}
build() {
execSync('composer install --no-dev', {stdio: 'inherit' });
}
} Composer is a bit like NPM for PHP. What this does is optimize PHP dependencies (basically The command seems to be run in the directory of the project because after This will be a problem if the PHP plugin "messes up" the dev environment on every deploy.
Maybe I'm missing something here but it seems that |
Tip: When writing a plugin you should return a promise in any case as your hook, so that the action invoked there is awaited properly - and change to execution to use const BbPromise = require('bluebird');
const { exec } = require('child_process');
class PhpServerlessPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.hooks = {
'before:package:createDeploymentArtifacts': () => BbPromise.bind(this)
.then(this.build),
};
}
build() {
return BbPromise.fromCallback(cb => {
exec(
'composer install --no-dev',
{ cwd: XXXXXXX },
cb
);
});
}
} The question now is, where (in which directory) do you want to execute the composer command? You just have to set the cwd option in the exec command above to the CWD for composer. You can set that to anything, derive it from some variable in serverless.yml (e.g.
Somehow. Everything contained in the |
Right! It should be executed at the project root so it generates the caches correctly, but in a temporary directory so that the dev environnement is not affected. Basically Imagine it were a C application: I would like to upload the compiled executable to the lambda, not the source code. And I don't want the executable to be generated in my project because my project is a dev environment. So I want to generate an archive and create and edit files (in my plugin) during the deployment process without affecting the actual project. As such, I think Trying to explain it another way: git clone https://github.com/my/project
cd project/
npm install
# […] work on the project
serverless deploy # that will run `npm install --only=production`
# try to work again on the project => ERROR, dev dependencies are not installed anymore! |
@mnapoli Understood what you what to achieve In this case I would go s slightly different way - do not force the framework to do things differently, but use a plugin, that already does the packaging in a way that you want and let your plugin hook into that plugin. I'm talking about That should be quite easy: The project will create an independent dependency hive at I think this is the easiest and fastest way to achieve exactly what you want to achieve, without having implementations in the Serverless framework for a specific rare problem. |
OK thank you, so to summarize before closing the issue:
Thanks! |
I have had a look at all those options and the first seems best. I had a good lock at Is there a simpler solution than what Do you think it is doable to write a plugin that would:
? (I'm trying to gather your opinion before trying it, I spend so much time on that that I figure maybe you can save me a few days of work :p) If that is doable, which before event do you think I should do that? That would be simpler than option 3 (which means completely hiding Also to be honest the current behavior is (IMO) very limiting. Even for JS applications it makes no sense to generate production artifacts inside the development environment. All the logic you have in |
This is a Bug Report
Description
I am writing a plugin for deploying PHP applications. My plugin basically optimizes caches for production when running
serverless deploy
.Expected: the optimized cache files are uploaded to lambda, and my local project is not affected.
Actual: the optimized cache files are generated in my project, are correctly uploaded to lambda but then they are left in my project.
This messes up the dev environment because those production files should not exist in local dev environment.
Idea: duplicate the project in a temporary directory, then execute the "packaging" step in that directory before zipping the directory to create the archive?
The text was updated successfully, but these errors were encountered: