-
Notifications
You must be signed in to change notification settings - Fork 119
147 lines (145 loc) · 5.28 KB
/
build-microzig.yml
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
name: Build Microzig
on:
push:
branches: [main]
paths:
- .github/**
- build.zig*
- tools/**
- core/**
- port/**
- examples/**
pull_request:
branches: [main]
paths:
- build.zig*
- tools/**
- core/**
- port/**
- examples/**
jobs:
build-microzig:
if: |
${{
( github.event_name == 'pull_request' && github.base_ref == 'main' )
|| github.ref_name == 'main'
}}
name: Build
uses: ./.github/workflows/build-base.yml
with:
zig-version: ${{ vars.MZ_TARGET_ZIG_VERSION }}
get-submodules: true
is-packaged: true
zig-args: -Doptimize=ReleaseSmall
secrets:
downloads-url: ${{ secrets.DOWNLOADS_URL }}
# If this is a push to main we want to create an automatic test against the
# zig-master branch. To do this, we need to create a PR and while we could
# just go directly from main to zig-master, this would mean that if the tests
# with zig-master fail, the main branch will also display the failure (this
# is because github action results are saved based on the commit). To solve
# that issue, we create a new branch with a new commit in it, then create
# the PR based on that.
#
# Creates the branch that we will use for the PR to zig-master
create-branch:
if: ${{ github.event_name == 'push' && github.ref_name == 'main' }}
name: Create Patch Branch
needs:
- build-microzig
runs-on: ubuntu-latest
outputs:
branch: master-patch/${{ github.sha }}
permissions:
contents: write
pull-requests: write
steps:
# First we checkout the repository
- uses: actions/checkout@v4
# Check if the branch exsits. This will be used in future steps to
# conditionally execute them.
- id: cbe
name: Check branch exists
# The list remote returns nothing if the branch does not exist.
# So, if there is nothing in the command output, it does not exist
run: |
if [[ -z "$(git ls-remote \
--heads origin master-patch/${{ github.sha }})" ]]; then
echo "exists=false" >> "$GITHUB_OUTPUT"
else
echo "exists=true" >> "$GITHUB_OUTPUT"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# If the branch doesn't already exist, we create a new one. We use the
# username and email of the last commit.
- name: Create Branch
if: steps.cbe.outputs.exists == 'false'
run: |
git config user.name "$(git log -1 --pretty=format:'%an')"
git config user.email "$(git log -1 --pretty=format:'%ae')"
git checkout -b "master-patch/${{ github.sha }}"
git push -u origin "master-patch/${{ github.sha }}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Checkout to the new branch
- name: checkout
uses: actions/checkout@v4
with:
ref: master-patch/${{ github.sha }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Create an empty commit that will be used to save the ci results
# against (For more details see "create-branch" comment)
- name: add-commit
run: |
git config user.name "$(git log -1 --pretty=format:'%an')"
git config user.email "$(git log -1 --pretty=format:'%ae')"
git commit \
--author "ZEG Github Action <>" \
-m "chore: commit for zig master build ci" \
--allow-empty
git push
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Now that a branch exists, we can create our PR. To do this we will use the
# github command line tool.
create-pr:
if: ${{ github.event_name == 'push' && github.ref_name == 'main' }}
name: Create Pull Request
runs-on: ubuntu-latest
needs: create-branch
permissions:
pull-requests: read
contents: read
steps:
- uses: actions/checkout@v4
# Check if the PR already exists, this will be used to conditionally
# create the PR.
- id: cpe
name: check pr exists
# github actions pr only outputs when the query find something.
# So, if the output is empty, pr does not exist
run: |
gh pr list -B zig-master \
-H "${{ needs.create-branch.outputs.branch }}" 2> check-exists-out
if [[ -z "$(cat check-exists-out)" ]]; then
echo "exists=false" >> "$GITHUB_OUTPUT"
else
echo "exists=true" >> "$GITHUB_OUTPUT"
fi
cat check-exists-out
rm check-exists-out
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Create the actual PR if it doesn't already exist
- name: create pull request,
if: steps.cpe.outputs.exists == 'false'
run: |
gh pr create -B zig-master -H master-patch/${{ github.sha }} \
--title 'Testing commit ${{ github.sha }} with zig master' \
--body 'Created by Github action'
env:
# We use a custom TOKEN to enable triggering other workflows.
# See: https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/triggering-a-workflow#triggering-a-workflow-from-a-workflow
GITHUB_TOKEN: ${{ secrets.PR_CREATE_TOKEN }}