Skip to content

Commit 1fc8369

Browse files
committed
Integrate Uffizzi
1 parent 0bccc27 commit 1fc8369

File tree

3 files changed

+191
-0
lines changed

3 files changed

+191
-0
lines changed

.github/workflows/uffizzi-build.yml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
name: Build PR Image
2+
on:
3+
pull_request:
4+
types: [opened,synchronize,reopened,closed]
5+
6+
jobs:
7+
8+
build-application:
9+
name: Build and Push `JSONHero`
10+
runs-on: ubuntu-latest
11+
if: ${{ github.event_name != 'pull_request' || github.event.action != 'closed' }}
12+
outputs:
13+
tags: ${{ steps.meta.outputs.tags }}
14+
steps:
15+
- name: Checkout git repo
16+
uses: actions/checkout@v3
17+
- name: Set up Docker Buildx
18+
uses: docker/setup-buildx-action@v2
19+
- name: Generate UUID image name
20+
id: uuid
21+
run: echo "UUID_TAG_APP=$(uuidgen)" >> $GITHUB_ENV
22+
- name: Docker metadata
23+
id: meta
24+
uses: docker/metadata-action@v3
25+
with:
26+
images: registry.uffizzi.com/${{ env.UUID_TAG_APP }}
27+
tags: type=raw,value=60d
28+
- name: Build and Push Image to registry.uffizzi.com ephemeral registry
29+
uses: docker/build-push-action@v2
30+
with:
31+
push: true
32+
context: .
33+
tags: ${{ steps.meta.outputs.tags }}
34+
labels: ${{ steps.meta.outputs.labels }}
35+
file: ./Dockerfile
36+
cache-from: type=gha
37+
cache-to: type=gha,mode=max
38+
39+
40+
render-compose-file:
41+
name: Render Docker Compose File
42+
# Pass output of this workflow to another triggered by `workflow_run` event.
43+
runs-on: ubuntu-latest
44+
needs:
45+
- build-application
46+
outputs:
47+
compose-file-cache-key: ${{ steps.hash.outputs.hash }}
48+
steps:
49+
- name: Checkout git repo
50+
uses: actions/checkout@v3
51+
- name: Render Compose File
52+
run: |
53+
APP_IMAGE=$(echo ${{ needs.build-application.outputs.tags }})
54+
export APP_IMAGE
55+
# Render simple template from environment variables.
56+
envsubst < docker-compose.uffizzi.yaml > docker-compose.rendered.yml
57+
cat docker-compose.rendered.yml
58+
- name: Upload Rendered Compose File as Artifact
59+
uses: actions/upload-artifact@v3
60+
with:
61+
name: preview-spec
62+
path: docker-compose.rendered.yml
63+
retention-days: 2
64+
- name: Serialize PR Event to File
65+
run: |
66+
cat << EOF > event.json
67+
${{ toJSON(github.event) }}
68+
69+
EOF
70+
- name: Upload PR Event as Artifact
71+
uses: actions/upload-artifact@v3
72+
with:
73+
name: preview-spec
74+
path: event.json
75+
retention-days: 2
76+
77+
delete-preview:
78+
name: Call for Preview Deletion
79+
runs-on: ubuntu-latest
80+
if: ${{ github.event.action == 'closed' }}
81+
steps:
82+
# If this PR is closing, we will not render a compose file nor pass it to the next workflow.
83+
- name: Serialize PR Event to File
84+
run: echo '${{ toJSON(github.event) }}' > event.json
85+
- name: Upload PR Event as Artifact
86+
uses: actions/upload-artifact@v3
87+
with:
88+
name: preview-spec
89+
path: event.json
90+
retention-days: 2

.github/workflows/uffizzi-preview.yml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
name: Deploy Uffizzi Preview
2+
3+
on:
4+
workflow_run:
5+
workflows:
6+
- "Build PR Image"
7+
types:
8+
- completed
9+
10+
11+
jobs:
12+
cache-compose-file:
13+
name: Cache Compose File
14+
runs-on: ubuntu-latest
15+
outputs:
16+
compose-file-cache-key: ${{ env.COMPOSE_FILE_HASH }}
17+
pr-number: ${{ env.PR_NUMBER }}
18+
steps:
19+
- name: 'Download artifacts'
20+
# Fetch output (zip archive) from the workflow run that triggered this workflow.
21+
uses: actions/github-script@v6
22+
with:
23+
script: |
24+
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
25+
owner: context.repo.owner,
26+
repo: context.repo.repo,
27+
run_id: context.payload.workflow_run.id,
28+
});
29+
let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
30+
return artifact.name == "preview-spec"
31+
})[0];
32+
let download = await github.rest.actions.downloadArtifact({
33+
owner: context.repo.owner,
34+
repo: context.repo.repo,
35+
artifact_id: matchArtifact.id,
36+
archive_format: 'zip',
37+
});
38+
let fs = require('fs');
39+
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/preview-spec.zip`, Buffer.from(download.data));
40+
- name: 'Unzip artifact'
41+
run: unzip preview-spec.zip
42+
- name: Read Event into ENV
43+
run: |
44+
echo 'EVENT_JSON<<EOF' >> $GITHUB_ENV
45+
cat event.json >> $GITHUB_ENV
46+
echo 'EOF' >> $GITHUB_ENV
47+
- name: Hash Rendered Compose File
48+
id: hash
49+
# If the previous workflow was triggered by a PR close event, we will not have a compose file artifact.
50+
if: ${{ fromJSON(env.EVENT_JSON).action != 'closed' }}
51+
run: echo "COMPOSE_FILE_HASH=$(md5sum docker-compose.rendered.yml | awk '{ print $1 }')" >> $GITHUB_ENV
52+
- name: Cache Rendered Compose File
53+
if: ${{ fromJSON(env.EVENT_JSON).action != 'closed' }}
54+
uses: actions/cache@v3
55+
with:
56+
path: docker-compose.rendered.yml
57+
key: ${{ env.COMPOSE_FILE_HASH }}
58+
59+
- name: Read PR Number From Event Object
60+
id: pr
61+
run: echo "PR_NUMBER=${{ fromJSON(env.EVENT_JSON).number }}" >> $GITHUB_ENV
62+
63+
- name: DEBUG - Print Job Outputs
64+
if: ${{ runner.debug }}
65+
run: |
66+
echo "PR number: ${{ env.PR_NUMBER }}"
67+
echo "Compose file hash: ${{ env.COMPOSE_FILE_HASH }}"
68+
cat event.json
69+
deploy-uffizzi-preview:
70+
name: Use Remote Workflow to Preview on Uffizzi
71+
needs:
72+
- cache-compose-file
73+
uses: UffizziCloud/preview-action/.github/workflows/[email protected]
74+
with:
75+
# If this workflow was triggered by a PR close event, cache-key will be an empty string
76+
# and this reusable workflow will delete the preview deployment.
77+
compose-file-cache-key: ${{ needs.cache-compose-file.outputs.compose-file-cache-key }}
78+
compose-file-cache-path: docker-compose.rendered.yml
79+
server: https://app.uffizzi.com
80+
pr-number: ${{ needs.cache-compose-file.outputs.pr-number }}
81+
permissions:
82+
contents: read
83+
pull-requests: write
84+
id-token: write

docker-compose.uffizzi.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: '3'
2+
x-uffizzi:
3+
ingress:
4+
service: jsonhero
5+
port: 8787
6+
services:
7+
jsonhero:
8+
image: "${APP_IMAGE}"
9+
ports:
10+
- 8787:8787
11+
environment:
12+
SESSION_SECRET: abc123
13+
deploy:
14+
resources:
15+
limits:
16+
memory: 2000M
17+

0 commit comments

Comments
 (0)