github-action icon indicating copy to clipboard operation
github-action copied to clipboard

Document capture of `Outputs` and `sls info` results

Open matthewpoer opened this issue 4 years ago • 4 comments

This took a while to figure out so I'd like to share and advise that it'd be nice to see in the README or elsewhere in the documentation.

My use case was that I needed to capture some of my resources.Outputs from a serverless deployment for use with later scripting. For example, here's an excerpt from my serverless.yml file:

resources:
  Resources:
    host:
      Type: AWS::EC2::Instance
      Properties:
        ImageId: ami-0f2b4fc905b0bd1f1
        InstanceType: t2.medium
  Outputs:
    PublicDnsName:
      Description: "host public URL"
      Value: !GetAtt host.PublicDnsName

With that in place I can run sls info --verbose and capture in that output is the desired PublicDnsName but I have to grep and adjust for that, e.g.

sls info --verbose | grep PublicDnsName | cut -c 16-

All that said, I found it difficult to perform that scrub-and-grab sequence in a Github Action using this container, but finally landed on overwriting the entrypoint and prefixing my command with a -c, which is appended as an argument to the bash invocation. It's a bit confusing to me still, but it's functional:

name: Deploy to Amazon EC2
on:
  push:
    branches:
      - master
jobs:
  deploy:
    name: Deploy
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v2
    - name: Run serverless deployment
      uses: serverless/[email protected]
      with:
        args: deploy --stage=dev --verbose
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID_DEV }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY_DEV }}
        SLS_DEBUG: 1
    - name: Export sls/aws-ec2 host
      uses: serverless/[email protected]
      with:
        args: -c "serverless info --stage=dev --verbose | grep PublicDnsName | cut -c 16- > host.txt"
        entrypoint: /bin/bash
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID_DEV }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY_DEV }}
        SLS_DEBUG: 1
    - name: Show site URL for review and testing
      run: |
        export host=$(cat host.txt)
        echo http://$host/

matthewpoer avatar Apr 06 '20 18:04 matthewpoer

Solid tip

I made a plugin that outputs Outputs into a "manifest file" https://www.npmjs.com/package/serverless-manifest-plugin

This might help avoid some hardcore greps.

I'd love if sls deploy had a --json flag to output a simple structure to parse with jq

DavidWells avatar Apr 06 '20 20:04 DavidWells

@DavidWells thanks, and nice plugin. I think we've identified the same need. The first time I saw you could set output params. this way in sls I thought it was great. When I found the only way to access them was re-running sls, requiring re-authorization and again setting the stage and the verbose flag, then having to dig for the result, I was not pleased.

matthewpoer avatar Apr 07 '20 12:04 matthewpoer

Thank you @matthewpoer

Just want to add that I'm using v2.18.0 and had to change the shell to /bin/sh.

marcelloromani avatar Jan 07 '22 22:01 marcelloromani

Thank you so much for this plugin!!! My github workflow looks like this, and worked fine!

  deploy-backend:
    needs: build
    runs-on: ubuntu-latest
    environment:
      name: aws
      url: ${{ steps.manifest.outputs.url }}

    steps:
      - uses: actions/checkout@v2
      - name: Download artifacts
        uses: actions/download-artifact@v2
        with:
          name: jar-artifacts
          path: app/target

      - name: Install Plugin and Deploy
        uses: serverless/[email protected]
        with:
          args: -c "serverless plugin install --name serverless-manifest-plugin && serverless deploy --stage prd"
          entrypoint: /bin/sh
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
    
      - name: manifest
        id: manifest
        run: |
          url=$(cat .serverless/manifest.json | jq -r '.prd.urls.apiGateway')
          echo "::set-output name=url::$url"

      - run: echo ${{ steps.manifest.outputs.url }}
      

miltonhit avatar Sep 07 '23 19:09 miltonhit