Skip to content

Commit d3ddfc5

Browse files
committed
Integrate Uffizzi
Signed-off-by: Vibhav Bobade <[email protected]>
1 parent c7fd12b commit d3ddfc5

File tree

4 files changed

+212
-0
lines changed

4 files changed

+212
-0
lines changed

.github/uffizzi/Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM uffizzi/ttyd:golang1.18-alpine
2+
3+
COPY envd /usr/bin/envd
4+
5+
WORKDIR /
6+
7+
ENTRYPOINT ["tini", "--"]
8+
CMD ["ttyd", "/bin/zsh"]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: "3"
2+
3+
x-uffizzi:
4+
ingress:
5+
service: envd
6+
port: 7681
7+
8+
services:
9+
envd:
10+
image: "${APP_IMAGE}"
11+
restart: unless-stopped
12+
ports:
13+
- "7681:7681"
14+
deploy:
15+
resources:
16+
limits:
17+
memory: 500M
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 `Envd`
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: Setup Go
29+
uses: actions/setup-go@v3
30+
with:
31+
go-version: 1.18
32+
- name: Cache Go modules
33+
uses: actions/cache@preview
34+
with:
35+
path: ~/go/pkg/mod
36+
key: ${{ runner.os }}-build-${{ hashFiles('**/go.sum') }}
37+
restore-keys: |
38+
${{ runner.OS }}-
39+
- name: Build
40+
run: make
41+
- name: Build and Push Image to registry.uffizzi.com ephemeral registry
42+
uses: docker/build-push-action@v2
43+
with:
44+
push: true
45+
context: .
46+
tags: ${{ steps.meta.outputs.tags }}
47+
labels: ${{ steps.meta.outputs.labels }}
48+
file: ./.github/uffizzi/Dockerfile
49+
cache-from: type=gha
50+
cache-to: type=gha,mode=max
51+
52+
53+
render-compose-file:
54+
name: Render Docker Compose File
55+
# Pass output of this workflow to another triggered by `workflow_run` event.
56+
runs-on: ubuntu-latest
57+
needs:
58+
- build-application
59+
outputs:
60+
compose-file-cache-key: ${{ steps.hash.outputs.hash }}
61+
steps:
62+
- name: Checkout git repo
63+
uses: actions/checkout@v3
64+
- name: Render Compose File
65+
run: |
66+
APP_IMAGE=$(echo ${{ needs.build-application.outputs.tags }})
67+
export APP_IMAGE
68+
# Render simple template from environment variables.
69+
envsubst < .github/uffizzi/docker-compose.uffizzi.yml > docker-compose.rendered.yml
70+
cat docker-compose.rendered.yml
71+
- name: Upload Rendered Compose File as Artifact
72+
uses: actions/upload-artifact@v3
73+
with:
74+
name: preview-spec
75+
path: docker-compose.rendered.yml
76+
retention-days: 2
77+
- name: Serialize PR Event to File
78+
run: |
79+
cat << EOF > event.json
80+
${{ toJSON(github.event) }}
81+
82+
EOF
83+
- name: Upload PR Event as Artifact
84+
uses: actions/upload-artifact@v3
85+
with:
86+
name: preview-spec
87+
path: event.json
88+
retention-days: 2
89+
90+
delete-preview:
91+
name: Call for Preview Deletion
92+
runs-on: ubuntu-latest
93+
if: ${{ github.event.action == 'closed' }}
94+
steps:
95+
# If this PR is closing, we will not render a compose file nor pass it to the next workflow.
96+
- name: Serialize PR Event to File
97+
run: echo '${{ toJSON(github.event) }}' > event.json
98+
- name: Upload PR Event as Artifact
99+
uses: actions/upload-artifact@v3
100+
with:
101+
name: preview-spec
102+
path: event.json
103+
retention-days: 2
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

0 commit comments

Comments
 (0)