diff --git a/Dockerfile b/Dockerfile index 2505df4..083c493 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 serverless@3.x -ENTRYPOINT ["serverless"] +COPY entrypoint.sh /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] diff --git a/README.md b/README.md index 37a74fa..fd46e62 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: @@ -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 && 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 ./ && 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/serverless-github-action@v1.0 + 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/serverless-github-action@v1.0 + 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/serverless-github-action@v1.0 + with: + working-directory: ./foo + args: deploy +``` ## License diff --git a/action.yml b/action.yml index 709b9d4..249ce3e 100644 --- a/action.yml +++ b/action.yml @@ -4,21 +4,36 @@ author: 'Serverless, Inc. (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: diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..729bd1d --- /dev/null +++ b/entrypoint.sh @@ -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