Skip to content

Commit b6d0354

Browse files
authored
feat: uffizzi integration (#878)
Co-authored-by: Aramayis <>
1 parent 0e0c82e commit b6d0354

File tree

3 files changed

+191
-0
lines changed

3 files changed

+191
-0
lines changed

.github/workflows/uffizzi-build.yml

+90
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 `f-ui-kit`
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+
render-compose-file:
40+
name: Render Docker Compose File
41+
# Pass output of this workflow to another triggered by `workflow_run` event.
42+
runs-on: ubuntu-latest
43+
needs:
44+
- build-application
45+
outputs:
46+
compose-file-cache-key: ${{ steps.hash.outputs.hash }}
47+
steps:
48+
- name: Checkout git repo
49+
uses: actions/checkout@v3
50+
- name: Render Compose File
51+
run: |
52+
APP_IMAGE=$(echo ${{ needs.build-application.outputs.tags }})
53+
export APP_IMAGE
54+
# Render simple template from environment variables.
55+
envsubst < ./docker-compose.uffizzi.yml > docker-compose.rendered.yml
56+
cat docker-compose.rendered.yml
57+
- name: Upload Rendered Compose File as Artifact
58+
uses: actions/upload-artifact@v3
59+
with:
60+
name: preview-spec
61+
path: docker-compose.rendered.yml
62+
retention-days: 2
63+
- name: Serialize PR Event to File
64+
run: |
65+
cat << EOF > event.json
66+
${{ toJSON(github.event) }}
67+
68+
EOF
69+
- name: Upload PR Event as Artifact
70+
uses: actions/upload-artifact@v3
71+
with:
72+
name: preview-spec
73+
path: event.json
74+
retention-days: 2
75+
76+
delete-preview:
77+
name: Call for Preview Deletion
78+
runs-on: ubuntu-latest
79+
if: ${{ github.event.action == 'closed' }}
80+
steps:
81+
# If this PR is closing, we will not render a compose file nor pass it to the next workflow.
82+
- name: Serialize PR Event to File
83+
run: echo '${{ toJSON(github.event) }}' > event.json
84+
- name: Upload PR Event as Artifact
85+
uses: actions/upload-artifact@v3
86+
with:
87+
name: preview-spec
88+
path: event.json
89+
retention-days: 2
90+

.github/workflows/uffizzi-preview.yml

+84
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.yml

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

0 commit comments

Comments
 (0)