forked from pivotal-cf/cf-redis-example-app
-
Notifications
You must be signed in to change notification settings - Fork 0
175 lines (163 loc) · 6.98 KB
/
build-using-cnb-buildpack.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
name: "Build using cnb buildpack"
permissions:
packages: write
on:
push:
branches:
- '*'
tags:
- "v?[0-9]+.[0-9]+.[0-9]+*"
env:
CNB_IMAGE_NAME: cnb-app
# SERVICE_VERSION: '6.0'
SERVICE_IMAGE: "redis:6"
SERVICE_HOST: 172.17.0.3 #localhost #redis-service
SERVICE_PORT: 6379
# SERVICE_USERNAME: '22032e25-4aba-417f-a394-8bbd78d920cd'
SERVICE_PASSWORD: 'StbQ4EovUpwQjD0cT1Hr7PKBG'
# DATABASE_NAME: my-redis-db
DEBUG: 0
jobs:
package-app:
name: "Package ${{ github.repository }} as CNB app"
runs-on: ubuntu-latest
# services:
# redis-service:
# Docker Hub image
# image: redis:6
# Set health checks to wait until redis has started
# options: >-
# --health-cmd "redis-cli ping"
# --health-interval 10s
# --health-timeout 5s
# --health-retries 5
# ports:
# Maps port 6379 on service container to the host
# - 6379:6379
steps:
-
uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5
with:
# Number of commits to fetch. 0 indicates all history for all branches and tags.
# Default: 1
fetch-depth: 1
-
name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
-
name: Setup pack for ${{ github.repository }}
uses: buildpacks/github-actions/[email protected]
# -
# name: Start MongoDB testing server
# uses: supercharge/[email protected]
# with:
# mongodb-version: ${{ env.SERVICE_VERSION }}
# mongodb-port: ${{env.SERVICE_PORT}}
# mongodb-username: ${{env.SERVICE_USERNAME}}
# mongodb-password: ${{env.SERVICE_PASSWORD}}
# mongodb-db: ${{env.DATABASE_NAME}}
# mongodb-container-name: mongodb-${{ env.SERVICE_VERSION }}
-
name: Packaging app from ${{ github.repository }} pushed by ${{ github.actor }}
shell: bash
run: |
echo "Pack Build ${{env.CNB_IMAGE_NAME}} (${{github.repository}})"
pack build ${{env.CNB_IMAGE_NAME}} --builder paketobuildpacks/builder-jammy-base
echo "Pack Inspect ${{env.CNB_IMAGE_NAME}} (${{github.repository}})"
pack inspect ${{env.CNB_IMAGE_NAME}}
-
name: Install redis tools
run: sudo apt-get install -y redis-tools
-
name: Testing app
shell: bash
run: |
./setup-prerequisite.sh #
./vcap-services-template-reformat.sh
cat vcap-service.env
echo "Starting sample app"
container_name="cnb-app-container"
container_id=$(docker run -d -e PORT=80 -p 8080:80 --env-file vcap-service.env --name "$container_name" ${CNB_IMAGE_NAME})
echo "Cnb app started (id: $container_id)"
echo "Waiting to ensure app is up and running"
while [ "$( docker container inspect -f '{{.State.Status}}' $container_name )" != "running" ]; do
echo "waiting for $container_name to be running current: $(docker inspect -f '{{.State.Status}}' $container_name)"
sleep 1
done
sleep 3 # to ensure app is up and running
#docker inspect -f '{{.HostConfig.LogConfig.Type}}' $container_id
redis_container_name="$(docker ps -f "ancestor=$SERVICE_IMAGE" --format "{{.Names}}")"
CONTAINER_APP_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $container_name)
echo "CONTAINER_APP_IP: $CONTAINER_APP_IP"
if [ $DEBUG -eq 1 ]; then
echo "----------------- $container_name --------------------"
docker inspect -f '{{json .NetworkSettings}}' $container_name
echo "----------------- $redis_container_name --------------------"
docker inspect -f '{{json .NetworkSettings}}' $redis_container_name
fi
echo "=== Redirect logs to cnb-app-container.log ==="
docker logs -f cnb-app-container &> cnb-app-container.log &
docker ps -a
echo "=== Check connectivity ==="
if nc -vz 127.0.0.1 8080;then echo "port 8080 available";else echo "port 8080 UNAVAILABLE";exit_status=1;fi
if nc -vz 127.0.0.1 ${SERVICE_PORT};then echo "port ${SERVICE_PORT} available";else echo "port ${SERVICE_PORT} UNAVAILABLE";exit_status=1;fi
echo "Checking redis server 127.0.0.1 : PING ==> $(redis-cli -a ${SERVICE_PASSWORD} -h 127.0.0.1 -p ${SERVICE_PORT} --no-warnings ping)"
echo "Checking redis server $SERVICE_HOST : PING ==> $(redis-cli -a ${SERVICE_PASSWORD} -h ${SERVICE_HOST} -p ${SERVICE_PORT} --no-warnings ping)"
function check_service() {
type="$1"
cmd="$2"
cmd_prefix="$3"
if [ -z $cmd_prefix ];then
cmd_prefix="curl"
fi
exit_status=0
echo "$type using > $cmd <"
if $cmd;then
echo ""
echo "$type failed: retry in verbose mode"
echo $cmd_prefix -vvv ${cmd##$cmd_prefix}
exit_status=1
else
echo ""
echo "$type successful"
fi
return $exit_status
}
export APP="http://127.0.0.1:8080"
exit_status=0
! create_service="$(check_service "Create" "curl -sSLf -X PUT $APP/foo -d data=bar")"
! get_service="$(check_service "Get" "curl -sSLf -X GET $APP/foo")"
! delete_service="$(check_service "Delete" "curl -vvv -sSL -X DELETE $APP/foo")"
exit_status=$((create_service + get_service + delete_service))
echo "==================================================="
echo "==================================================="
echo "Dumping logs using docker logs cnb-app-container"
docker logs cnb-app-container 2>&1
ls -lrt *.log
exit $exit_status
-
name: Tag version ${{github.ref_name}}
if: github.ref_type == 'tag'
run: |
docker tag ${{env.CNB_IMAGE_NAME}}:latest ghcr.io/${{ github.repository }}:${{github.ref_name}}
-
name: Tag with common data
run: |
docker tag ${{env.CNB_IMAGE_NAME}}:latest ghcr.io/${{ github.repository }}:latest
docker tag ${{env.CNB_IMAGE_NAME}}:latest ghcr.io/${{ github.repository }}:${{github.sha}}
echo "cleanup built image"
docker image rm ${{env.CNB_IMAGE_NAME}}:latest
echo "dump existing images"
docker images
-
name: Publish ghcr.io/${{ github.repository }}
run: |
echo "publish all tags"
docker push --all-tags ghcr.io/${{ github.repository }}