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

Document capture of Outputs and sls info results #26

Open
matthewpoer opened this issue Apr 6, 2020 · 4 comments
Open

Document capture of Outputs and sls info results #26

matthewpoer opened this issue Apr 6, 2020 · 4 comments

Comments

@matthewpoer
Copy link

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/
@DavidWells
Copy link

DavidWells commented Apr 6, 2020

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

@matthewpoer
Copy link
Author

@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.

@marcelloromani
Copy link

Thank you @matthewpoer

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

@miltonhit
Copy link

miltonhit commented Sep 7, 2023

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 }}
      

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants