Skip to content

Commit cd2ab6e

Browse files
committed
Add s2i buildpacks sample
1 parent 8b23663 commit cd2ab6e

File tree

7 files changed

+116
-12
lines changed

7 files changed

+116
-12
lines changed

s2i-buildpacks/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Code Engine Source-to-Image Buildpacks support
2+
3+
This sample shows how to use the Code Engine source-to-image Buildpacks feature
4+
to build your application from a git repo, push the image into a registry,
5+
and then deploy it as a Code Engine application. It doesn't require a Dockerfile in the git repo.
6+
7+
More details and supported runtimes and versions can be found in [Code Engine Docs](https://cloud.ibm.com/docs/codeengine?topic=codeengine-plan-build).
8+
9+
The exact steps taken in [`run`](./run) are:
10+
- Login and create an ICR (IBM Container Registry) namespace to store the resulting
11+
image
12+
- Create an IBM API Key that will be used by the build process to push
13+
the image to ICR
14+
- Define a build that points to a git repo for the source, and the
15+
defines where in ICR to store the image
16+
- Creates a Code Engine app from that image

s2i-buildpacks/package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"author": "Code Engine",
3+
"dependencies": {
4+
"express": "^4.17.0"
5+
},
6+
"description": "Sample Node.js Application using NPM",
7+
"license": "Apache-2.0",
8+
"main": "server.js",
9+
"name": "sample",
10+
"repository": {
11+
"type": "git",
12+
"url": "https://github.com/IBM/CodeEngine"
13+
},
14+
"version": "0.0.0"
15+
}

s2i-buildpacks/run

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
3+
function clean() {
4+
set +x
5+
echo Cleaning...
6+
ic cr namespace-rm "${ICR_NS}" -f > /dev/null 2>&1 || true
7+
ic ce app delete -n s2i-buildpacks -f --wto=0 > /dev/null 2>&1 || true
8+
ic iam api-key-delete s2i-sample -f > /dev/null 2>&1 || true
9+
ic ce registry delete -n icr -f > /dev/null 2>&1 || true
10+
ic ce buildrun delete -n myrun -f > /dev/null 2>&1 || true
11+
ic ce build delete -n mybuild -f > /dev/null 2>&1 || true
12+
rm -f out || true
13+
}
14+
15+
# Define our ICR Namespace env var for later use
16+
ID=$(ic account show | grep "Account ID:" | sed "s/^.*: *//")
17+
ICR_NS=s2i-${ID:0:25}
18+
19+
# Clean up previous run
20+
clean
21+
22+
set -ex
23+
24+
# Create an ICR namespace to hold our new image
25+
ic cr namespace-add $ICR_NS
26+
27+
# Create an apikey, put it in a registry secret. Used to push/pull image to ICR
28+
ic iam api-key-create s2i-sample | \
29+
grep "API Key" | sed 's/^.*Key *//' | \
30+
xargs ic ce registry create -n icr -s us.icr.io -u iamapikey -p
31+
32+
# Define the build of this dir in this github repo
33+
ic ce build create -n mybuild -i "us.icr.io/${ICR_NS}/app" --rs icr \
34+
--source https://github.com/IBM/CodeEngine --context-dir s2i-buildpacks --strategy buildpacks
35+
36+
# Now kick off the build itself
37+
ic ce buildrun submit -n myrun --build mybuild
38+
39+
# Wait for it to finish
40+
while true ; do
41+
ic ce buildrun get -n myrun > out
42+
grep Succeeded out && break
43+
grep "exited with" out && cat out && exit 1
44+
sleep 10
45+
done
46+
47+
# Test the image we just built - deploy an app and 'curl' it
48+
ic ce app create -n s2i-buildpacks --image "us.icr.io/${ICR_NS}/app" --rs icr
49+
URL=$(ic ce app get -n s2i-buildpacks -o jsonpath={.status.url})
50+
curl -s "${URL}"
51+
52+
# Clean
53+
clean

s2i-buildpacks/server.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const express = require('express');
2+
const port = process.env.PORT || 8080;
3+
4+
const app = express();
5+
6+
app.get('/', (request, response) => {
7+
response.send(`<!DOCTYPE html>
8+
<html>
9+
<head>
10+
<title>Powered By Code Engine</title>
11+
</head>
12+
<body>
13+
I was built by Buildpacks with Code Engine!
14+
</body>
15+
</html>`);
16+
});
17+
18+
app.listen(port);

s2i-dockerfile/README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
# Source-to-Image
1+
# Code Engine Source-to-Image Dockerfile support
22

3-
This sample shows how to use the Code Engine source-to-image feature
3+
This sample shows how to use the Code Engine source-to-image Dockerfile feature
44
to build your application from a git repo, push the image into a registry,
5-
and then deploy it as a Code Engien application. This will assume that there's
6-
a Dockerfile in the root of the git repo that will be used to build the image.
5+
and then deploy it as a Code Engine application. This will assume that there's
6+
a Dockerfile in the root of the git repo that will be used to build the image.
7+
8+
More details can be found in [Code Engine Docs](https://cloud.ibm.com/docs/codeengine?topic=codeengine-plan-build).
79

810
The exact steps taken in [`run`](./run) are:
9-
- Create an ICR (IBM Container Regsitry) namespace to store the resulting
11+
- Login and create an ICR (IBM Container Registry) namespace to store the resulting
1012
image
1113
- Create an IBM API Key that will be used by the build process to push
1214
the image to ICR

s2i-dockerfile/app.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func Handler(w http.ResponseWriter, r *http.Request) {
9-
fmt.Fprintf(w, "\n\nI was built with Code Engine!\n\n\n")
9+
fmt.Fprintf(w, "\n\nI was built by Dockerfile with Code Engine!\n\n\n")
1010
}
1111

1212
func main() {

s2i-dockerfile/run

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
function clean() {
44
set +x
55
echo Cleaning...
6-
ic cr namespace-rm $ICR_NS -f > /dev/null 2>&1 || true
7-
ic ce app delete -n s2i-app -f --wto=0 > /dev/null 2>&1 || true
6+
ic cr namespace-rm "${ICR_NS}" -f > /dev/null 2>&1 || true
7+
ic ce app delete -n s2i-dockerfile -f --wto=0 > /dev/null 2>&1 || true
88
ic iam api-key-delete s2i-sample -f > /dev/null 2>&1 || true
99
ic ce registry delete -n icr -f > /dev/null 2>&1 || true
1010
ic ce buildrun delete -n myrun -f > /dev/null 2>&1 || true
@@ -30,7 +30,7 @@ ic iam api-key-create s2i-sample | \
3030
xargs ic ce registry create -n icr -s us.icr.io -u iamapikey -p
3131

3232
# Define the build of this dir in this github repo
33-
ic ce build create -n mybuild -i us.icr.io/$ICR_NS/app --rs icr \
33+
ic ce build create -n mybuild -i "us.icr.io/${ICR_NS}/app" --rs icr \
3434
--source https://github.com/IBM/CodeEngine --context-dir s2i-dockerfile
3535

3636
# Now kick off the build itself
@@ -45,9 +45,9 @@ while true ; do
4545
done
4646

4747
# Test the image we just built - deploy an app and 'curl' it
48-
ic ce app create -n s2i-app --image us.icr.io/$ICR_NS/app --rs icr
49-
URL=$(ic ce app get -n s2i-app -o jsonpath={.status.url})
50-
curl -s $URL
48+
ic ce app create -n s2i-dockerfile --image "us.icr.io/${ICR_NS}/app" --rs icr
49+
URL=$(ic ce app get -n s2i-dockerfile -o jsonpath={.status.url})
50+
curl -s "${URL}"
5151

5252
# Clean
5353
clean

0 commit comments

Comments
 (0)