├── .gitignore ├── .gitlab-ci.yml ├── README.md ├── handler.js ├── package.json └── serverless.yml /.gitignore: -------------------------------------------------------------------------------- 1 | # package directories 2 | node_modules 3 | jspm_packages 4 | 5 | # Serverless directories 6 | .serverless -------------------------------------------------------------------------------- /.gitlab-ci.yml: -------------------------------------------------------------------------------- 1 | # .gitlab-ci.yml 2 | image: node:9 3 | 4 | # define pipeline stages 5 | stages: 6 | - test 7 | - deploy 8 | 9 | # install build tools 10 | before_script: 11 | - apt-get update 12 | - apt-get install -y build-essential 13 | - yarn -D 14 | 15 | # run dummy test command 16 | test: 17 | stage: test 18 | script: yarn run test 19 | 20 | deploy_production: 21 | stage: deploy 22 | 23 | # tag deploy to an environment for tracking 24 | environment: 25 | name: production 26 | script: 27 | - yarn run deploy --stage production 28 | 29 | # save build artifacts for publishing 30 | artifacts: 31 | paths: 32 | - .serverless 33 | 34 | # only build when updates are made to "master" branch 35 | only: 36 | - master 37 | 38 | deploy_staging: 39 | stage: deploy 40 | 41 | # tag deploy to an environment for tracking 42 | environment: 43 | name: staging 44 | script: 45 | - yarn run deploy 46 | 47 | # save build artifacts for publishing 48 | artifacts: 49 | paths: 50 | - .serverless 51 | 52 | # only build when updates are made to "staging" branch 53 | only: 54 | - staging 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # serverless-gitlab-ci 2 | 3 | ## Description 4 | 5 | Basic nodejs CI setup for gitlab's CI service. This configuration can be used with their free tier usage or their self-hosted solution. This lets devs collaborate on simple serverless projects with automatic deployment on merge. This also tags each merge to an environment for automatic tracking in the `CI / CD > Environments` tab. 6 | 7 | This setup also includes a dummy test command that will run on all branches. Ideally this would be replaced with real tests to enable a true CI experience. 8 | 9 | ## Usage 10 | 11 | Navigate to [gitlab.com](https://gitlab.com/) and create a new repo. 12 | 13 | Then go to `settings > CI / CD` and expand the `Secret Variables` section. Click `Add New Variable` and add 14 | 15 | ```yml 16 | Key: AWS_ACCESS_KEY_ID 17 | Value: YOUR_ACCESS_KEY 18 | Protected: ✓ 19 | 20 | Key: AWS_SECRET_ACCESS_KEY 21 | Value: YOUR_SECRET_VALUE 22 | Protected: ✓ 23 | ``` 24 | 25 | Enabling protected limits these variables to specific branches that we define (eg: staging and master, since they'll be the only ones that deploy anything). See `settings > Repository` and expand protected branches to set the access (Can enable user, or group level access). 26 | 27 | Next up, template this repo with 28 | ```bash 29 | serverless create --template-url https://github.com/bvincent1/serverless-gitlab-ci/master --path myService 30 | ``` 31 | 32 | And set the remote to the gitlab url 33 | 34 | ```bash 35 | cd myService 36 | git init # init repo if needed 37 | git remote add origin git@gitlab.com:username/myrepo.git 38 | git add -A 39 | git commit -a -m 'Init repo from template' 40 | git push -u origin master 41 | ``` 42 | 43 | This should create the repo and automatically create the ci pipeline. 44 | 45 | ## Special Notes 46 | 47 | - Ideally you could just use AWS IAM to create a new admin user and use those keys and not your own. This lets you track usage and keeps your keys separate. Best practice even suggests you rotate keys regularly, but thats a whole different story. 48 | 49 | - This project uses `yarn` as the node_modules install tool. This can be easily changed by modifiying the `.gitlab-ci.yml` file and changing the `yarn` command to `npm`. 50 | 51 | - Additional Gitlab CI example [RestfullSheets](https://gitlab.com/dotslashsolve/RestfulSheets/) 52 | -------------------------------------------------------------------------------- /handler.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports.hello = (event, context, callback) => { 4 | const response = { 5 | statusCode: 200, 6 | body: JSON.stringify({ 7 | message: 'Go Serverless v1.0! Your function executed successfully!', 8 | input: event, 9 | }), 10 | }; 11 | 12 | callback(null, response); 13 | 14 | // Use this code if you don't use the http event with the LAMBDA-PROXY integration 15 | // callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event }); 16 | }; 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "serverless-gitlab-ci", 3 | "version": "1.0.0", 4 | "description": "Simple serverless gitlab ci template", 5 | "main": "handler.js", 6 | "author": "Ben Vincent ", 7 | "license": "MIT", 8 | "private": false, 9 | "scripts": { 10 | "test": "echo \"serverless invoke -f hello\n\" " 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | service: serverless-gitlab-ci # NOTE: update this with your service name 2 | 3 | provider: 4 | name: aws 5 | runtime: nodejs6.10 6 | 7 | functions: 8 | hello: 9 | handler: handler.hello 10 | --------------------------------------------------------------------------------