wtw - WebTask Workflow
A simple workflow for the Webtask CLI
Installation and quickstart
Assuming you already know and have a Webtask account:
npm i -g wt-clinpm i -g wtwwtw --helpwt init
This last command will go trough the account setup to use the remote runtimes provided by webtask.io
Now, let's create a remote webtask from an existing file (wraps 'wt create'):
wtw create myfile.js
Jump to Usage section to see more examples.
Motivations, approach and conventions
Why Webtask
Webtask is a general purpose Function as a Service (or serverless) provider. Simple NodeJS functions can be a task executed on a remote server almost instantly and has a public accessible URL. That's a Webtask
Why an addional CLI
Webtask comes with a convenient CLI that already offer all basic commands. As always, it get useful to have some basic best practices and standard workflows to deal with frameworks and service providers.
What problems addresses
wt-cli-workflow deals with
- providing easy to use CLI commands to create and run webtasks based on some conventions
- define a set standard run mode and make it easy to work with them: local, development, test, stage, production
- make it easy to setup a continuous testing, integration and deliver environment based on Webtask (TBD)
Conventions
- Use .env files to store keys, credentials and various secrets
- env is used as suffix in .env files (eg: .env.dev, .env.prod)
- env is used as suffix in deploying the webtask (eg: myfile-test, myfile-prod)
- task name is the file name without the '.js' extension
Usage
Pre-requisites
Obviously, Webtask is required. The setup takes 1 minute Create an account and a install a CLI with npm following this instructions
Once the basic environment has been setup, working with serverless functions is mostly about:
- Creating a new function (not very often)
- code, test, deploy, release (most of the time)
Well, exactly what happens in any other kind of development, right? Webtask cli offer great foundamentals but in real world there are 2 things we almost always need that are not immediate (for lazy people like me) to get with simple commands:
- secret environment variable to connect to databases, APIs...
- different environments for development, testing, staging, production...
Here comes wt-cli-workflow, or wtw.
Setup webtask profile
We want to create a new Node8 based container and test it:
wt init -p node8 --url https://sandbox.auth0-extend.com
echo "module.exports = function (cb) { cb(null, versions: process.versions); }" > hello.js
wt create hello.js
NOTE: there was a but in wt-cli that require some manual change in ~/.webtask. If the new profile to not run node8, then the bug is still there.
Create
wtw create myfile.js
This will create a new function named "myfile-dev", with a specific url that will look like this:
https://XXX-some-code-XXX.sandbox.auth0-extend.com/myfile-dev
And the .env file will be used
To publish a different version for example to be used for testing
wtw create myfile.js test
This will create a new function named "myfile-test", with a specific url that will look like this:
https://XXX-some-code-XXX.sandbox.auth0-extend.com/myfile-test
And the test.env file will be used
Run
Run the file after it has already been created
wtw run myfile.js
This will update the myfile-dev version with the latest local code and will stay open to publish any new local changes
Here as well, a second parameter will change the environment, affecting which .env file will be used and the remote URL
Scheduled functions with cron
Crete a function that with a specified schedule using cron
wtw cron myfile.js "*/10 * * * *"
That schedule mean every ten minues past the hour. Use Corntab to master cron :)
More utilities commands
wtw ls // accepts a search params that grep TS out of standard 'wt ls'wtw rm //accepts a series of webtasks named and removed them using basic 'wt rm'
Please let me know what commands you would like to see, or feel free to fork and make pull requests :)
Developers
Setup development environment
Checkout the code, then
npm install
npm link
This will create a symbolic link under user home, something like
~/.nvm/versions/node/{NODE_VERSION}/lib/node_modules/wt-cli-workflow -> ~/{project folder}/wt-cli-workflow
Now create a symbolic link to use the CLI globally. In Ubuntu I recommend:
// sudo update-alternatives --install /usr/bin/wtw-dev wtw-dev ~/.nvm/versions/node/{NODE_VERSION}/lib/node_modules/wt-cli-workflow/src/cli.js 1
sudo update-alternatives --install /usr/bin/wtw-dev wtw-dev /usr/local/bin/wtw 1
Change {NODE_VERSION} with your installed version of node I call this command wtw-dev so it will not be confused with released version on npmjs
Now this will show the cli help in the console.
$ wtw-dev -h
Use local version
Using generator is simple to create a basic Webtask function to be used as a test.
npm install -g generate generate-webtaskgenerate webtask:contexttouch .env.devwtw-dev create index.jswtw-dev run index.js