Skip to content
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

Add customization options for Serverless action #90

Open
wants to merge 9 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ LABEL "com.github.actions.description"="Wraps the Serverless Framework to enable
LABEL "com.github.actions.icon"="zap"
LABEL "com.github.actions.color"="red"

RUN npm i -g [email protected]
ENTRYPOINT ["serverless"]
COPY entrypoint.sh /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
78 changes: 52 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@

This Action wraps the [Serverless Framework](https://serverless.com) to enable common Serverless commands.

## This project is looking for maintainers!

If you would like to be a maintainer of this project, please reach out to one of the active [Serverless organization](https://github.com/serverless) members to express your interest.

Welcome, and thanks in advance for your help!

## Usage

An example workflow to deploy a project with serverless v3:

An example workflow to deploy a project with the Serverless Framework:

```yaml
name: Deploy master branch
Expand All @@ -36,7 +29,7 @@ jobs:
node-version: ${{ matrix.node-version }}
- run: npm ci
- name: serverless deploy
uses: serverless/github-action@v3.2
uses: ryanlawson/serverless-github-action@v1.0
with:
args: deploy
env:
Expand All @@ -46,36 +39,69 @@ jobs:
# AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
```

## Usage with serverless plugins
Change your action in this way, according to [this issue](https://github.com/serverless/github-action/issues/28), thanks to @matthewpoer:
## Configuration

| `with:` | Description | Required | Default |
| --- | --- | --- | --- |
| `args` | Arguments passed to `serverless` | `true` |
| `aws-credentials` | Whether to use credentials stored in the local environment (`AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`) | `false` | |
| `install-packages` | Space-separated list of packages to install prior to running `serverless {args}` | `false` | |
| `serverless-version` | Version of the Serverless Framework to use | `false` | `latest` |
| `working-directory` | Folder where your configuration is located | `false` | `.` |

## Examples

### Minimal example
Basic deployment with no customization
```yaml
- name: Install Plugin and Deploy
uses: serverless/github-action@v3.2
- name: Deploy
uses: ryanlawson/serverless-github-action@v1.0
with:
args: -c "serverless plugin install --name <plugin-name> && serverless deploy"
entrypoint: /bin/sh
args: deploy
```

## Fix "This command can only be run in a Serverless service directory" error
Change your action in this way, according to [this issue](https://github.com/serverless/github-action/issues/53#issuecomment-1059839383), thanks to @nikhuber:
### Use local credentials
Ensures `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` are present and uses them to authenticate
```yaml
- name: Enter dir and deploy
uses: serverless/github-action@v3.2
- name: Deploy with local credentials
uses: ryanlawson/serverless-github-action@v1.0
with:
args: -c "cd ./<your-dir> && serverless deploy"
entrypoint: /bin/sh
aws-credentials: true # or yes
args: deploy
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
```


## Use serverless v1 or v2
Change the action with one of the following:
### Install packages and deploy
Installs any additional packages (usually [Serverless plugins](https://www.serverless.com/plugins)) prior to deploying
```yaml
uses: serverless/github-action@v1
- name: Install packages and deploy
uses: ryanlawson/[email protected]
with:
install-packages: serverless-offline serverless-prune-plugin
args: deploy
```

### Use a particular Serverless Framework CLI version
Installs a specific version of the Serverless Framework
```yaml
uses: serverless/github-action@v2
- name: Deploy using a particular version of serverless
uses: ryanlawson/[email protected]
with:
serverless-version: 2
args: deploy
```

### Change your working directory
Sets a specific working directory (usually the root of the repository) for your Serverless configuration
```yaml
- name: Deploy from a particular working directory
uses: ryanlawson/[email protected]
with:
working-directory: ./foo
args: deploy
```

## License

Expand Down
27 changes: 21 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,36 @@ author: 'Serverless, Inc. <[email protected]> (https://serverless.com)'
branding:
icon: 'zap'
color: 'red'

inputs:
args:
description: 'Serverless cli arguments'
description: 'Arguments passed to `serverless`'
required: false
entrypoint:
description: 'Serverless entrypoint. For example: `/bin/sh`'
default: none
aws-credentials:
description: 'Whether to use credentials stored in the local environment (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)'
required: false

default: 'false'
install-packages:
description: 'Space-separated list of packages to install prior to running `serverless {args}`'
required: false
default: none
serverless-version:
description: 'Version of the Serverless Framework to use (default: latest)'
required: false
default: latest
working-directory:
description: 'Folder where your configuration is located'
required: false
default: .
runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.aws-credentials }}
- ${{ inputs.working-directory }}
- ${{ inputs.serverless-version }}
- ${{ inputs.install-packages }}
- ${{ inputs.args }}
entrypoint: ${{ inputs.entrypoint }}

outputs:
version:
Expand Down
37 changes: 37 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/sh -l
if [ "$5" = "none" ]; then
echo "You need to specify at least one argument, like deploy"
exit 5
fi

WITH_LOCAL_CREDENTIALS=""
if [ "$1" = "true" ] || [ "$1" = "yes" ]; then
if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
echo "You have aws-credentials set without AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY"
exit 1
fi
WITH_LOCAL_CREDENTIALS="--use-local-credentials"
fi

cd $2
if [ "$?" != 0 ]; then
echo "Unable to cd into $2"
exit 2
fi

npm i -g serverless@$3
if [ "$?" != 0 ]; then
echo "Unable to install serverless@$3"
exit 3
fi

if [ "$4" != "none" ]; then
npm i -g $4
if [ "$?" != 0 ]; then
echo "Unable to install packages: $4"
exit 4
fi
fi

echo serverless $5 $WITH_LOCAL_CREDENTIALS
serverless $5 $WITH_LOCAL_CREDENTIALS