-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yaml
209 lines (188 loc) · 6.45 KB
/
action.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
name: Build-Action
description: 'build and push to ghcr'
inputs:
PAT:
description: 'PAT from repo'
required: true
NAMESPACE:
description: 'Namespace'
required: true
NAME:
description: 'Name'
required: true
ACTION_ENABLE_TESTING:
description: 'if set to true, testcontainer will be created and tests will be run'
required: false
default: 'false'
GITHUB_TOKEN:
description: 'Github Token'
required: true
ACTIVATE_DEV_RUNNER:
description: 'if set to true, devRunner container will be created'
required: false
default: 'false'
LOCAL_TESTING_SECRET:
description: 'local-testing.yaml secret'
required: false
default: 'false'
WORKING_DIRECTORY:
description: 'Working directory of the project'
required: false
default: '.'
DOCKER_CONTEXT:
description: 'Docker context'
required: false
default: '.'
DOCKERFILE:
description: 'Dockerfile'
required: false
default: './Dockerfile'
DBPORT:
description: 'port of the database'
required: false
default: '5432'
outputs:
version:
description: 'version of the container'
value: ${{ steps.set-output.outputs.version }}
env:
description: 'environment of the container'
value: ${{ steps.set-output.outputs.env }}
runs:
using: 'composite'
steps:
- name: Checkout code
uses: actions/checkout@v4
# - name: Check COPY . . in Dockerfile
# run: |
# if grep -q "COPY.*\. \." ${{ inputs.DOCKERFILE }}; then
# echo "COPY . . is not allowed in Dockerfile"
# exit 1
# fi
# shell: bash
- name: Login to Github Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ inputs.GITHUB_TOKEN }}
logout: false
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: get-npm-version
id: package-version
run: |
PACKAGE_VERSION=$(cat ${{ inputs.WORKING_DIRECTORY }}/package.json | jq '.version' | tr -d '"')
echo "current-version=${PACKAGE_VERSION}" >> $GITHUB_OUTPUT
shell: bash
- name: Build Container name for develop
if: github.ref == 'refs/heads/develop'
run: |
echo "IMAGE_TAG=dev" >> $GITHUB_ENV
echo "ENV=stage" >> $GITHUB_ENV
shell: bash
- name: Build Container name for staging
if: startsWith(github.ref, 'refs/heads/release/')
run: |
echo "IMAGE_TAG=staging" >> $GITHUB_ENV
echo "ENV=stage" >> $GITHUB_ENV
shell: bash
- name: Build Container name for master
if: github.ref != 'refs/heads/develop' && !startsWith(github.ref, 'refs/heads/release/')
run: |
echo "IMAGE_TAG=${{ steps.package-version.outputs.current-version}}" >> $GITHUB_ENV
echo "ENV=prod" >> $GITHUB_ENV
shell: bash
- name: Check file existence (next)
id: check_files_next
uses: andstor/file-existence-action@v3
with:
files: '${{ inputs.WORKING_DIRECTORY }}/.env.production, ${{ inputs.WORKING_DIRECTORY }}/next.config.*'
- name: Check file existence (astro)
id: check_files_astro
uses: andstor/file-existence-action@v3
with:
files: '${{ inputs.WORKING_DIRECTORY }}/.env.production, ${{ inputs.WORKING_DIRECTORY }}/astro.config.mjs'
- name: Remove production environment file on develop for next/astro projects
if: github.ref == 'refs/heads/develop' && (steps.check_files_next.outputs.files_exists == 'true' || steps.check_files_astro.outputs.files_exists == 'true')
run: |
rm -f ${{ inputs.WORKING_DIRECTORY }}/.env.production
shell: bash
- name: Write local-testing config file
if: inputs.LOCAL_TESTING_SECRET != 'false'
uses: DamianReeves/write-file-action@master # TODO replace
with:
path: ${{ inputs.WORKING_DIRECTORY }}/config/local.yaml
contents: |
${{ inputs.LOCAL_TESTING_SECRET }}
- name: Set up docker buildx for tester
if: inputs.ACTION_ENABLE_TESTING == 'true'
id: tester
uses: docker/build-push-action@v6
with:
context: ${{ inputs.DOCKER_CONTEXT }}
file: ${{ inputs.DOCKERFILE }}
push: true
target: tester
platforms: linux/amd64
network: host # ?
tags: ghcr.io/entrecode/${{ inputs.NAMESPACE }}.${{ inputs.NAME}}:tester
#cache-from: type=gha
#cache-to: type=gha,mode=max
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
build-args: |
PAT=${{ inputs.PAT }}
- name: Test
if: inputs.ACTION_ENABLE_TESTING == 'true'
id: testrunner
run: |
docker run --pull always --network="host" --rm -e DBPORT=${{ inputs.DBPORT }} ghcr.io/entrecode/${{ inputs.NAMESPACE }}.${{ inputs.NAME}}:tester
shell: bash
- name: Fail if test failed
if: inputs.ACTION_ENABLE_TESTING == 'true' && steps.testrunner.outcome != 'success'
run: core.setFailed('Tests failed') && exit 1
shell: bash
- name: Remove local testing file
if: inputs.LOCAL_TESTING_SECRET != 'false'
run: |
rm -f ${{ inputs.WORKING_DIRECTORY }}/config/local.yaml
shell: bash
- name: Build and push
if: inputs.ACTIVATE_DEV_RUNNER == 'true' && github.ref == 'refs/heads/develop'
uses: docker/build-push-action@v6
with:
context: ${{ inputs.DOCKER_CONTEXT }}
file: ${{ inputs.DOCKERFILE }}
push: true
target: devRunner
platforms: linux/amd64
tags: ghcr.io/entrecode/${{ inputs.NAMESPACE }}.${{ inputs.NAME}}:${{ env.IMAGE_TAG }}
#cache-from: type=gha
#cache-to: type=gha,mode=max
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
build-args: |
PAT=${{ inputs.PAT }}
- name: Build and push
if: inputs.ACTIVATE_DEV_RUNNER == 'false' || inputs.ACTIVATE_DEV_RUNNER == '' || github.ref != 'refs/heads/develop'
uses: docker/build-push-action@v6
with:
context: ${{ inputs.DOCKER_CONTEXT }}
file: ${{ inputs.DOCKERFILE }}
push: true
target: runner
platforms: linux/amd64
tags: ghcr.io/entrecode/${{ inputs.NAMESPACE }}.${{ inputs.NAME}}:${{ env.IMAGE_TAG }}
#cache-from: type=gha
#cache-to: type=gha,mode=max
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
build-args: |
PAT=${{ inputs.PAT }}
- name: Set output
id: set-output
run: |
echo "version=${{ env.IMAGE_TAG }}" >> $GITHUB_OUTPUT
echo "env=${{ env.ENV }}" >> $GITHUB_OUTPUT
shell: bash