Great! Now our secrets are never defined in code. We can convert these into environment variable references. Slack_client_secret: "xxxxxxxxxxxxxxxxxx" Slack_client_id: "xxxxxxxxxx.xxxxxxxxxxxx" Slack_verification_token: "xxxxxxxxxxxxx" Luckily, the Serverless framework has a way to do that outlined in their official docs.īefore, we had something like this where the x’s represented secrets. We need to change that so the variables are obtained from the environment. Step 2: Set Custom Serverless Variables Reference Environment Variables in Serverless ConfigurationĪs it stands, our example application has required Slack variables defined as custom variables in serverless.yml. This deploys our Serverless application! However, this works only if the Slack secrets are defined as text in the serverless.yml file.
Here’s the resulting Jenkinsfile that will successfully build and deploy our example application. However, a Test stage could easily be added that ran an npm testcommand. To keep things simple, we’re only going to define a Build stage and a Deploy stage for the Jenkinsfile. Simple Serverless Jenkinsfile Using Docker With this line removed, the Serverless deployment will search for the default AWS profile, which is what we have defined in the Jenkinsfile. However, they will only be referenced automatically if a profile is not defined in serverless.yml. So, go ahead and delete this line in the example application’s serverless.yml file. These environment variables will be referenced automatically when the Serverless application is deployed. You can also refer to the official documentation.ĪWS_ACCESS_KEY_ID = credentials('aws-secret-key-id')ĪWS_SECRET_ACCESS_KEY = credentials('aws-secret-access-key')
For details on how to add credentials to Jenkins, refer to the Adding Docker Hub Credentials to Jenkins section from Building with Docker in Jenkins Pipelines. Since we’re only using one image throughout the whole pipeline, we can define it just once in the beginning of the pipeline.įor the environment of the pipeline, we’re going to reference AWS credentials from Jenkins. We’re also going to need to be running as root in the container to properly run Serverless commands, so we’ll specify running as root in the arguments. (If you aren’t using Docker with Jenkins you can refer to the example on the Jenkins CI GitHub wiki that doesn’t use Docker.)
Since we want to keep it lightweight, we’re going with the Alpine version of Node, which comes in at roughly 71.1MB.
If you want to spin up a Jenkins container that has Docker and Jenkinsfile pipelines already configured, feel free to use Liatrio’s Alpine-Jenkins image.įor this Serverless application, we’re just going to use one Docker image as the agent.
Keep in mind you’ll need a Jenkins instance with Docker available and an Amazon Web Services account that may incur charges for deploying the serverless application. If you want to DIY as opposed to just reading, feel free to start with the example application. Step 1: Serverless Jenkinsfile Using Docker Example Application