Skip to content

Commit bf1466b

Browse files
committed
Integrate Uffizzi
1 parent 5c52df6 commit bf1466b

File tree

6 files changed

+437
-2
lines changed

6 files changed

+437
-2
lines changed

.github/workflows/uffizzi-build.yml

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

.github/workflows/uffizzi-preview.yml

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

docker-compose.uffizzi.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
version: "3"
2+
3+
x-uffizzi:
4+
ingress:
5+
service: opentitan
6+
port: 7681
7+
8+
services:
9+
10+
opentitan:
11+
image: "${OPENTITAN_IMAGE}"
12+
ports:
13+
- "7700:7700"
14+
- "7681:7681"
15+
# entrypoint: ["/bin/bash", "-c"]
16+
# command: ["cd /home/dev/src && ttyd bash"]
17+
entrypoint: ["/bin/bash"]
18+
command:
19+
- "-c"
20+
- "apt-get update && \
21+
apt-get install neovim -y && \
22+
apt-get install unzip -y && \
23+
apt-get install wget -y && \
24+
wget 'https://github.com/$GHA_ACTOR/$GHA_REPO/archive/refs/heads/$GHA_BRANCH.zip' && \
25+
unzip $GHA_BRANCH.zip -d . && \
26+
mv $GHA_REPO-$GHA_BRANCH /home/dev/src && \
27+
cd /home/dev/src && \
28+
ttyd bash
29+
"
30+
deploy:
31+
resources:
32+
limits:
33+
memory: 4000M

python-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
hjson==3.1.0
77
jsonschema==4.17.3; python_version >= "3.7"
88
libcst==0.4.1
9-
mako==1.1.6
9+
mako==1.2.0
1010
pluralizer==1.2.0
1111
pycryptodome==3.15.0
1212
pyelftools==0.29

0 commit comments

Comments
 (0)