-
Notifications
You must be signed in to change notification settings - Fork 0
184 lines (183 loc) · 6.87 KB
/
herokuish-tests-db-url.yaml
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
176
177
178
179
180
181
182
183
184
name: Test using herokuish (DB credentials in DATABASE_URL)
on:
workflow_call:
inputs:
registry_root:
required: true
type: string
image_tagged:
required: false
type: string
description: Deprecated, use image_name
image_name:
required: false
type: string
default_port:
required: true
type: string
fetch-depth:
required: false
type: number
default: 0
herokuish_base_image:
required: false
type: string
default: gliderlabs/herokuish:latest-22
BUILDPACK_URL:
required: false
type: string
default: ""
PGPASSWORD:
required: false
type: string
default: testing-password
POSTGRES_USER:
required: false
type: string
default: user
POSTGRES_DB:
required: false
type: string
default: test
POSTGRES_ENABLED:
required: false
type: string
default: "false"
POSTGRES_VERSION:
required: false
type: string
default: "11.21"
environment:
required: false
type: string
default: ''
submodules:
required: false
type: string
default: 'false'
PUBLIC_URL:
required: false
type: string
default: ''
workflow_dispatch: {}
repository_dispatch:
types: [run]
jobs:
test:
runs-on: ubuntu-22.04
environment: ${{ inputs.environment }}
permissions:
packages: write
contents: read
steps:
- name: checkout
uses: actions/checkout@v4
with:
submodules: ${{ inputs.submodules }}
- 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 tests herokuish and heroku buildpack
run: |
herokuish_base_image="${{ inputs.herokuish_base_image }}"
if [ "$herokuish_base_image"x = 'x' ]
then herokuish_base_image="gliderlabs/herokuish:latest-22"
fi
cat <<EOF > Dockerfile
FROM $herokuish_base_image
COPY . /tmp/app
ENV BUILDPACK_URL=${{ inputs.BUILDPACK_URL }} \\
USER=herokuishuser
EOF
rm -f .dockerignore
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64
push: true
tags: |
${{ inputs.registry_root }}tests/${{ inputs.image_tagged || inputs.image_name }}:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Create test services network
run: |
docker network create --internal test-services
- if: inputs.POSTGRES_ENABLED == 'true'
name: Set up a test database container
run: |
# The following service is isolated from the internet
docker run -d --name postgres --network test-services -p 5432:5432 -e POSTGRES_DB=${{ inputs.POSTGRES_DB }} -e POSTGRES_USER=${{ inputs.POSTGRES_USER }} -e POSTGRES_PASSWORD=${{ inputs.PGPASSWORD }} postgres:${{ inputs.POSTGRES_VERSION }}-alpine
cat > psql-test.sh <<EOF
export PGPASSWORD=${{ inputs.PGPASSWORD }}
run=1
psql -h 127.0.0.1 -U user -d test -c \\q 2>/dev/null
while [ $? == 2 ]
do sleep 1
run=$(($run+1))
if [ $run -gt 59 ]; then break; fi
echo -n .
psql -h 127.0.0.1 -U user -d test -c \\q 2>/dev/null
done
EOF
bash psql-test.sh
- name: Restore cached buildpack versions
id: cache-buildpack-restore
uses: actions/cache/restore@v4
with:
path: |
tmp/cache
key: ${{ runner.os }}-buildpack-cache
- name: Run tests using herokuish and heroku buildpack (DB credentials in DATABASE_URL)
env:
SECRETS_CONTEXT: ${{ toJson(secrets) }}
VARS_CONTEXT: ${{ toJson(vars) }}
PUBLIC_URL: ${{ inputs.PUBLIC_URL }}
# This is a bit more elaborate so the container has access to the internet via the host
# and in addition to the test services network which is isolated from the internet
run: |
echo -n "$SECRETS_CONTEXT" | jq -r '[to_entries[]|select(.key|startswith("K8S_SECRET_"))]|map("\(.key|sub("K8S_SECRET_"; ""))=\(.value|tostring)")|.[]' > secrets.env
echo -n "$VARS_CONTEXT" | jq -r '[to_entries[]|select(.key|startswith("K8S_SECRET_"))]|map("\(.key|sub("K8S_SECRET_"; ""))=\(.value|tostring)")|.[]' >> secrets.env
echo -n "$VARS_CONTEXT" | jq -r '[to_entries[]|select(.key == "SERVICE_ID")]|map("\(.key)=\(.value|tostring)")|.[]' >> secrets.env
if [ "${PUBLIC_URL}x" == "x" ]
then echo -n "$VARS_CONTEXT" | jq -r '[to_entries[]|select(.key == "PUBLIC_URL")]|map("\(.key)=\(.value|tostring)")|.[]' >> secrets.env
else echo "PUBLIC_URL=$PUBLIC_URL" >> secrets.env
fi
echo -n "$SECRETS_CONTEXT" | jq -r '[to_entries[]|select(.key|startswith("LC_K8S_SECRET_"))]|map("\(.key|sub("LC_K8S_SECRET_"; "")|ascii_downcase)=\(.value|tostring)")|.[]' >> secrets.env
echo -n "$VARS_CONTEXT" | jq -r '[to_entries[]|select(.key|startswith("LC_K8S_SECRET_"))]|map("\(.key|sub("LC_K8S_SECRET_"; "")|ascii_downcase)=\(.value|tostring)")|.[]' >> secrets.env
docker create -t --name test-run-step \
--env-file secrets.env \
-e DATABASE_URL=postgres://${{ inputs.POSTGRES_USER }}:${{ inputs.PGPASSWORD }}@postgres:5432/${{ inputs.POSTGRES_DB }} \
-e NODE_PORT=8080 \
-v $(pwd)/tmp/cache:/tmp/cache \
${{ inputs.registry_root }}tests/${{ inputs.image_tagged || inputs.image_name }}:${{ github.sha }} \
/bin/herokuish buildpack test
docker network connect test-services test-run-step
docker start -i test-run-step
- name: Get test artifacts and clean up
if: always()
run: |
mkdir -p output
docker cp test-run-step:/app/cypress/ output > /dev/null 2>&1 || /bin/true
docker cp test-run-step:/app/playwright-report/ output > /dev/null 2>&1 || /bin/true
rm secrets.env
sudo chmod -R a+rX tmp
- name: Save buildpack versions
if: always()
id: cache-buildpack-save
uses: actions/cache/save@v4
with:
path: |
tmp/cache
key: ${{ steps.cache-buildpack-restore.outputs.cache-primary-key }}
- name: Store test artifacts
uses: actions/upload-artifact@v4
if: always()
with:
name: test-artifacts
path: output