Skip to content

Commit 07ebe3f

Browse files
committed
#68 Added enviorment parameter for both build args and env pass through. Added proper testcase to test both use case
1 parent 39ddc66 commit 07ebe3f

File tree

9 files changed

+108
-17
lines changed

9 files changed

+108
-17
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM lambci/lambda:build-ruby2.7
2+
3+
RUN gem update bundler
4+
5+
ARG USE_HTTPARTY
6+
ENV USE_HTTPARTY=${USE_HTTPARTY}
7+
ARG NOKOGIRI_VERSION
8+
ENV NOKOGIRI_VERSION=${NOKOGIRI_VERSION}
9+
10+
CMD "/bin/bash"
11+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
source 'https://rubygems.org'
2+
if ENV['USE_HTTPARTY']
3+
gem 'httparty', '0.18.1'
4+
else
5+
gem 'http'
6+
end
7+
if ENV['NOKOGIRI_VERSION']
8+
gem 'nokogiri', ENV['NOKOGIRI_VERSION']
9+
else
10+
gem 'nokogiri'
11+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'httparty'
2+
3+
def hello(event:, context:)
4+
body = HTTParty.get("https://github.com").body
5+
6+
{ statusCode: 200, body: body }
7+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
service: use-docker-file-with-enviornment
2+
3+
plugins:
4+
- serverless-ruby-layer
5+
6+
custom:
7+
rubyLayer:
8+
use_docker: true
9+
docker_file: Dockerfile
10+
environment:
11+
- USE_HTTPARTY
12+
- NOKOGIRI_VERSION=1.10.10
13+
14+
provider:
15+
name: aws
16+
runtime: ruby2.7
17+
18+
functions:
19+
hello:
20+
handler: handler.hello
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
source 'https://rubygems.org'
2+
if ENV['USE_HTTPARTY']
3+
gem 'httparty', '0.18.1'
4+
else
5+
gem 'http'
6+
end
7+
if ENV['NOKOGIRI_VERSION']
8+
gem 'nokogiri', ENV['NOKOGIRI_VERSION']
9+
else
10+
gem 'nokogiri'
11+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'httparty'
2+
3+
def hello(event:, context:)
4+
body = HTTParty.get("https://github.com").body
5+
6+
{ statusCode: 200, body: body }
7+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
service: use-docker-with-enviornment
2+
3+
plugins:
4+
- serverless-ruby-layer
5+
6+
custom:
7+
rubyLayer:
8+
use_docker: true
9+
environment:
10+
- USE_HTTPARTY
11+
- NOKOGIRI_VERSION=1.10.10
12+
13+
provider:
14+
name: aws
15+
runtime: ruby2.7
16+
17+
functions:
18+
hello:
19+
handler: handler.hello

lib/bundle.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,19 @@ function bundleInstall(){
7070
fs.copySync(docker_file_path,
7171
path.join(this.ruby_layer,'Dockerfile'))
7272
buildDocker = true
73-
}else if (this.options.docker_yums) {
73+
}else if (this.options.docker_yums || this.options.environment) {
7474
docker_steps =['FROM lambci/lambda:build-'+this.serverless.service.provider.runtime]
75-
this.options.docker_yums.forEach(function(package_name) {
76-
docker_steps.push('RUN yum install -y '+package_name)
77-
})
75+
if (this.options.docker_yums) {
76+
this.options.docker_yums.forEach(function(package_name) {
77+
docker_steps.push('RUN yum install -y '+package_name)
78+
})
79+
}
7880
docker_steps.push('RUN gem update bundler')
79-
if(this.options.docker_env_variable){
80-
this.options.docker_env_variable.forEach(function(build_arg) {
81-
docker_steps.push('ARG ' + build_arg)
82-
docker_steps.push('ENV ' + build_arg + '=${' + build_arg + '}')
81+
if(this.options.environment){
82+
this.options.environment.forEach(function(env_arg) {
83+
env_arg_name = env_arg.split('=')[0]
84+
docker_steps.push('ARG ' + env_arg_name)
85+
docker_steps.push('ENV ' + env_arg_name + '=${' + env_arg_name + '}')
8386
})
8487
}
8588
docker_steps.push('CMD "/bin/bash"')
@@ -90,16 +93,10 @@ function bundleInstall(){
9093
this.cli.log("Building docker for bundle install")
9194
docker_name ='ruby-layer:docker'
9295
docker_args = ['build', '-t', docker_name, '-f', 'Dockerfile']
93-
if(this.options.docker_build_args){
94-
this.options.docker_build_args.forEach(function(build_arg) {
95-
docker_args.push('--build-arg')
96-
docker_args.push(build_arg)
97-
})
98-
}
99-
if(this.options.docker_env_variable){
100-
this.options.docker_env_variable.forEach(function(build_arg) {
96+
if(this.options.environment){
97+
this.options.environment.forEach(function(env_arg) {
10198
docker_args.push('--build-arg')
102-
docker_args.push(build_arg)
99+
docker_args.push(env_arg)
103100
})
104101
}
105102
docker_args.push('.')

test/test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ let test_data = [
5858
'/gems/', '/specifications/','/gems/httparty-0.18.1/', '/gems/mime-types-data-3.2020.1104/', '/gems/multi_xml-0.6.0/', '/gems/mime-types-3.3.1/',
5959
'/gems/nokogiri-1.11.3-x86_64-linux/', '/gems/racc-1.5.2/', '/extensions/x86_64-linux/', '/gems/mini_portile2-2.5.1/' ],
6060
function_files: ['Gemfile', 'Gemfile.lock', 'handler.rb'], include_functions: ['Hello'], exclude_functions:[], check_version: true },
61+
62+
{ folder: 'use-docker-with-enviornment', gem_zip_dirs: ['/','/bin/','/build_info/','/doc/','/extensions/', '/gems/', '/plugins/',
63+
'/specifications/','/extensions/x86_64-linux/','/gems/httparty/', '/gems/mime-types-data/', '/gems/multi_xml/', '/gems/mime-types/',
64+
'/gems/nokogiri/', '/gems/mini_portile2/'], function_files: ['handler.rb'], include_functions: ['Hello'], exclude_functions:[] },
65+
66+
{ folder: 'use-docker-file-with-enviornment', gem_zip_dirs: ['/','/bin/','/build_info/','/doc/','/extensions/', '/gems/', '/plugins/',
67+
'/specifications/','/extensions/x86_64-linux/','/gems/httparty/', '/gems/mime-types-data/', '/gems/multi_xml/', '/gems/mime-types/',
68+
'/gems/nokogiri/', '/gems/mini_portile2/'], function_files: ['handler.rb'], include_functions: ['Hello'], exclude_functions:[] },
6169
]
6270

6371
describe('serverless package', function () {

0 commit comments

Comments
 (0)