Whisk Deploy — Zip Actions with file Include & Exclude
This blog post is a follow up on a earlier post on how to create a zip action using Whisk Deploy.
OpenWhisk provides an ability to create an action using a .zip
archive which can contain any number of action files including all dependencies. Whisk Deploy has a support to specify function
pointing to a directory with action files for creating a .zip
archive from the entire directory content and then creating a zip action.
File Level Include
We recently introduced a feature in Whisk Deploy to specify include
equivalent of programming paradigm import
, for example, multiple actions having reference to common library such as:
$ cd actions/
$ ls -1 ./
common/
greeting1/
greeting2/
manifest.yaml
$ ls -1 common/
utils.js
$ ls -1 greeting1/
index.js
package.json
$ ls -1 greeting2/
index.js
package.json
Here, action file index.js
under greeting1/
and action file index.js
undergreeting2/
refers to the utils.js
from common/
.
index.js under actions/greeting1/:
index.js under actions/greeting2/:
manifest.yaml:
include
takes a list of files/directories which needs to be included in an action. Each list of item can either have source
and/or destination
, for example:
include:
- [source]
- [source, destination]
Note: (1)
source
is considered relative path to wheremanifest.yaml
file is located.destination
is considered relative path to the action directory i.e.actions/greeting1
andactions/greeting2
in the following example. (2) Ifdestination
is not specified, its considered same assource
.
include
can contain different combination of source
and destination
:
include
with justsource
:
include:
- [actions/common/utils.js]
With this specification, utils.js
will be copied under actions/greeting/actions/common/utils.js
and can be referred to from index.js
with:
var utils = require('./actions/common/utils.js')
include
with rename:
include:
- ["actions/common/utils.js", "./common/myUtils.js"]
With this specification, utils.js
will be copied as actions/greeting/common/myUtils.js
and can be referred to from index.js
with:
var utils = require('./common/myUtils.js')
include
with different path:
include:
- ["actions/common/utils.js", "./common/utility/utils.js"]
With this specification, utils.js
will be copied as actions/greeting/common/utility/utils.js
and can be referred to from index.js
with:
var utils = require('./common/utility/utils.js')
include
with wild character*
:
include:
- ["actions/common/*.js", "./common/"]
With this specification, utils.js
and any other file with .js
extension will be copied under actions/greeting/common/
and can be referred to from index.js
with:
var utils = require('./common/utils.js')
Directory Level Include
include
also supports specifying directory which is recursively copied to specified destination before including it the archive, for example, libs
has a list of libraries which are referenced by the index.js
under greeting3/
:
$ cd actions/
$ ls -1
libs/
greeting3/
manifest.yaml
$ ls -1 libs/
lib1/
lib2/
lib3/
$ ls -1 libs/lib1/
utils.js
$ ls -1 libs/lib2/
utils.js
$ ls -1 libs/lib3/
utils.js
$ ls -1 greeting3/
index.js
package.json
index.js under actions/greeting3/:
manifest.yaml under actions/:
Here, the entire libs
dir is recursively copied to actions/greeting3/libs/
.
include
with wild character*
:
Example 1:
include:
- ["actions/libs/*/utils.js", "libs/"]
With this specification, utils.js
file under any directory within libs
will be copied under libs/
and can be referred to from index.js
similar to the above example:
var lib1 = require('./libs/lib1/utils.js')
var lib2 = require('./libs/lib2/utils.js')
var lib3 = require('./libs/lib3/utils.js')
Example 2:
include:
- ["actions/*/*/utils.js"]
With this specification, utils.js
file within two directories under actions
will be copied under actions/
and can be referred to from index.js
with:
var lib1 = require('./actions/libs/lib1/utils.js')
var lib2 = require('./actions/libs/lib2/utils.js')
var lib3 = require('./actions/libs/lib3/utils.js')
Example 3:
include:
- ["actions/*/*/utils.js", "actions/"]
With this specification, utils.js
file within two directories under actions
will be copied under actions/
and can be referred to from index.js
with:
var lib1 = require('./actions/libs/lib1/utils.js')
var lib2 = require('./actions/libs/lib2/utils.js')
var lib3 = require('./actions/libs/lib3/utils.js')
Exclude
exclude
can be specified as a list of files or directories with wildcard characters *
:
exclude:
- actions/common/*.js
- actions/libs/*/utils.js
Here is a very common usage of combination of include
and exclude
:
manifest.yaml:
Good Luck 👍!