fork/wezterm

git clone https://git.julia.blue/fork/wezterm

Wez Furlongci: trigger builds when .cargo/config.toml changes7d0bff069

termiz-0.23.3
36.2 KiB1160 linesraw
1#!/usr/bin/env python3
2import os
3import sys
4import glob
5from copy import deepcopy
6
7TRIGGER_PATHS = [
8    "**/*.rs",
9    "**/Cargo.lock",
10    "**/Cargo.toml",
11    ".cargo/config.toml",
12    "assets/fonts/**/*",
13    "assets/icon/*",
14    "ci/deploy.sh",
15]
16
17TRIGGER_PATHS_APPIMAGE = [
18    "ci/appimage.sh",
19    "ci/appstreamcli",
20    "ci/source-archive.sh",
21]
22
23TRIGGER_PATHS_UNIX = [
24    "assets/open-wezterm-here",
25    "assets/shell-completion/**/*",
26    "assets/shell-integration/**/*",
27    "assets/wezterm-nautilus.py",
28    "assets/wezterm.appdata.xml",
29    "assets/wezterm.desktop",
30    "get-deps",
31    "ci/tag-name.sh",
32    "termwiz/data/wezterm.terminfo",
33]
34
35TRIGGER_PATHS_MAC = [
36    "assets/macos/**/*",
37    "ci/macos-entitlement.plist",
38    "get-deps",
39    "ci/tag-name.sh",
40]
41
42TRIGGER_PATHS_WIN = [
43    "assets/windows/**/*",
44    "ci/windows-installer.iss",
45]
46
47
48def yv(v, depth=0):
49    if v is True:
50        return "true"
51    if v is False:
52        return "false"
53    if v is None:
54        return "nil"
55
56    if isinstance(v, str):
57        if "\n" in v:
58            indent = "  " * depth
59            result = ""
60            for l in v.splitlines():
61                result = result + "\n" + (f"{indent}{l}" if l else "")
62            return "|" + result
63        # This is hideous
64        if '"' in v:
65            return "'" + v + "'"
66        return '"' + v + '"'
67
68    return v
69
70
71class Step(object):
72    def render(self, f, depth=0):
73        raise NotImplementedError(repr(self))
74
75
76class RunStep(Step):
77    def __init__(self, name, run, shell="bash", env=None, condition=None):
78        self.name = name
79        self.run = run
80        self.shell = shell
81        self.env = env
82        self.condition = condition
83
84    def render(self, f, depth=0):
85        indent = "  " * depth
86        f.write(f"{indent}- name: {yv(self.name)}\n")
87        if self.condition:
88            f.write(f"{indent}  if: {self.condition}\n")
89        if self.env:
90            f.write(f"{indent}  env:\n")
91            keys = list(self.env.keys())
92            keys.sort()
93            for k in keys:
94                v = self.env[k]
95                f.write(f"{indent}    {k}: {v}\n")
96        if self.shell:
97            f.write(f"{indent}  shell: {self.shell}\n")
98
99        run = self.run
100
101        f.write(f"{indent}  run: {yv(run, depth + 2)}\n")
102
103
104class ActionStep(Step):
105    def __init__(self, name, action, params=None, env=None, condition=None, id=None):
106        self.name = name
107        self.action = action
108        self.params = params
109        self.env = env
110        self.condition = condition
111        self.id = id
112
113    def render(self, f, depth=0):
114        indent = "  " * depth
115        f.write(f"{indent}- name: {yv(self.name)}\n")
116        f.write(f"{indent}  uses: {self.action}\n")
117        if self.id:
118            f.write(f"{indent}  id: {self.id}\n")
119        if self.condition:
120            f.write(f"{indent}  if: {self.condition}\n")
121        if self.params:
122            f.write(f"{indent}  with:\n")
123            for k, v in self.params.items():
124                f.write(f"{indent}    {k}: {yv(v, depth + 3)}\n")
125        if self.env:
126            f.write(f"{indent}  env:\n")
127            for k, v in self.env.items():
128                f.write(f"{indent}    {k}: {yv(v, depth + 3)}\n")
129
130
131class CacheStep(ActionStep):
132    def __init__(self, name, path, key, id=None):
133        super().__init__(
134            name, action="actions/cache@v4", params={"path": path, "key": key}, id=id
135        )
136
137
138class SccacheStep(ActionStep):
139    def __init__(self, name):
140        super().__init__(name, action="mozilla-actions/sccache-action@v0.0.7")
141
142
143class CheckoutStep(ActionStep):
144    def __init__(self, name="checkout repo", submodules=True, container=None):
145        params = {}
146        if submodules:
147            params["submodules"] = "recursive"
148        # Newer versions of the checkout action use a binary-incompatible node
149        # binary, so we are pinned back on v3
150        # https://github.com/actions/checkout/issues/1442
151        version = "v3" if container is not None and "centos7" in container else "v4"
152        super().__init__(name, action=f"actions/checkout@{version}", params=params)
153
154
155class InstallCrateStep(ActionStep):
156    def __init__(self, crate: str, key: str, version=None):
157        params = {"crate": crate, "cache-key": key}
158        if version is not None:
159            params["version"] = version
160        super().__init__(
161            f"Install {crate} from Cargo",
162            action="baptiste0928/cargo-install@v3",
163            params=params,
164        )
165
166
167class Job(object):
168    def __init__(self, runs_on, container=None, steps=None, env=None):
169        self.runs_on = runs_on
170        self.container = container
171        self.steps = steps
172        self.env = env
173
174    def render(self, f, depth=0):
175        f.write("\n    steps:\n")
176        for s in self.steps:
177            s.render(f, depth)
178
179
180class Target(object):
181    def __init__(
182        self,
183        name=None,
184        os="ubuntu-latest",
185        container=None,
186        bootstrap_git=False,
187        rust_target=None,
188        continuous_only=False,
189        app_image=False,
190        is_tag=False,
191    ):
192        if not name:
193            if container:
194                name = container
195            else:
196                name = os
197        self.name = name.replace(":", "")
198        self.os = os
199        self.container = container
200        self.bootstrap_git = bootstrap_git
201        self.rust_target = rust_target
202        self.continuous_only = continuous_only
203        self.app_image = app_image
204        self.env = {}
205        self.is_tag = is_tag
206
207    def render_env(self, f, depth=0):
208        self.global_env()
209        if self.env:
210            indent = "    "
211            f.write(f"{indent}env:\n")
212            for k, v in self.env.items():
213                f.write(f"{indent}  {k}: {yv(v, depth + 3)}\n")
214
215    def uses_yum(self):
216        if "fedora" in self.name:
217            return True
218        if "centos" in self.name:
219            return True
220        return False
221
222    def uses_apt(self):
223        if "ubuntu" in self.name:
224            return True
225        if "debian" in self.name:
226            return True
227        return False
228
229    def uses_apk(self):
230        if "alpine" in self.name:
231            return True
232        return False
233
234    def uses_zypper(self):
235        if "suse" in self.name:
236            return True
237        return False
238
239    def needs_sudo(self):
240        if not self.container and self.uses_apt():
241            return True
242        return False
243
244    def install_system_package(self, name):
245        installer = None
246        if self.uses_yum():
247            installer = "yum"
248        elif self.uses_apt():
249            installer = "apt-get"
250        elif self.uses_apk():
251            installer = "apk"
252        elif self.uses_zypper():
253            installer = "zypper"
254        else:
255            return []
256        if self.needs_sudo():
257            installer = f"sudo -n {installer}"
258        if self.uses_apk():
259            return [RunStep(f"Install {name}", f"{installer} add {name}")]
260        else:
261            return [RunStep(f"Install {name}", f"{installer} install -y {name}")]
262
263    def install_curl(self):
264        if (
265            self.uses_yum()
266            or self.uses_apk()
267            or self.uses_zypper()
268            or (self.uses_apt() and self.container)
269        ):
270            if "centos:stream9" in self.container:
271                return self.install_system_package("curl-minimal")
272            else:
273                return self.install_system_package("curl")
274        return []
275
276    def install_openssh_server(self):
277        steps = []
278        if (
279            self.uses_yum()
280            or self.uses_zypper()
281            or (self.uses_apt() and self.container)
282        ):
283            steps += [
284                RunStep("Ensure /run/sshd exists", "mkdir -p /run/sshd")
285            ] + self.install_system_package("openssh-server")
286        if self.uses_apk():
287            steps += self.install_system_package("openssh")
288        return steps
289
290    def install_newer_compiler(self):
291        steps = []
292        if self.name == "centos7":
293            steps.append(
294                RunStep(
295                    "Install SCL",
296                    "yum install -y centos-release-scl-rh",
297                )
298            )
299            steps.append(
300                RunStep(
301                    "Update compiler",
302                    "yum install -y devtoolset-9-gcc devtoolset-9-gcc-c++",
303                )
304            )
305        return steps
306
307    def install_git(self):
308        steps = []
309        if self.bootstrap_git:
310            GIT_VERS = "2.26.2"
311            steps.append(
312                CacheStep(
313                    "Cache Git installation",
314                    path="/usr/local/git",
315                    key=f"{self.name}-git-{GIT_VERS}",
316                )
317            )
318
319            pre_reqs = ""
320            if self.uses_yum():
321                pre_reqs = "yum install -y wget curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker make"
322            elif self.uses_apt():
323                pre_reqs = "apt-get install -y wget libcurl4-openssl-dev libexpat-dev gettext libssl-dev libz-dev gcc libextutils-autoinstall-perl make"
324            elif self.uses_zypper():
325                pre_reqs = "zypper install -y wget libcurl-devel libexpat-devel gettext-tools libopenssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker make"
326
327            steps.append(
328                RunStep(
329                    name="Install Git from source",
330                    shell="bash",
331                    run=f"""{pre_reqs}
332if test ! -x /usr/local/git/bin/git ; then
333    cd /tmp
334    wget https://github.com/git/git/archive/v{GIT_VERS}.tar.gz
335    tar xzf v{GIT_VERS}.tar.gz
336    cd git-{GIT_VERS}
337    make prefix=/usr/local/git install
338fi
339ln -s /usr/local/git/bin/git /usr/local/bin/git""",
340                )
341            )
342
343        else:
344            if "tumbleweed" in self.name:
345                # git-core requires /usr/bin/which and that gets satisfied
346                # by busybox-which by default, which blocks installing
347                # rpmbuild, which depends on the which rpm directly,
348                # but that is blocked by the conflicting busybox-which rpm.
349                # So we explicitly install which here now
350                steps += self.install_system_package("which")
351
352            steps += self.install_system_package("git")
353
354        return steps
355
356    def install_rust(self, cache=True, toolchain="stable"):
357        salt = "2"
358        key_prefix = f"{self.name}-{self.rust_target}-{salt}-${{{{ runner.os }}}}"
359        params = dict()
360        if self.rust_target:
361            params["target"] = self.rust_target
362        steps = []
363        # Manually setup rust toolchain in CentOS7 curl is too old for the action
364        if "centos7" in self.name:
365            steps += [
366                RunStep(
367                    name="Install Rustup",
368                    run="""
369if ! command -v rustup &>/dev/null; then
370  curl --proto '=https' --tlsv1.2 --retry 10 -fsSL "https://sh.rustup.rs" | sh -s -- --default-toolchain none -y
371  echo "${CARGO_HOME:-$HOME/.cargo}/bin" >> $GITHUB_PATH
372fi
373""",
374                ),
375                RunStep(
376                    name="Setup Toolchain",
377                    run=f"""
378rustup toolchain install {toolchain} --profile minimal --no-self-update
379rustup default {toolchain}
380""",
381                ),
382            ]
383        elif "macos" in self.name:
384            steps += [
385                RunStep(
386                    name="Install Rust (ARM)",
387                    run="rustup target add aarch64-apple-darwin",
388                ),
389                RunStep(
390                    name="Install Rust (Intel)",
391                    run="rustup target add x86_64-apple-darwin",
392                )
393            ]
394        else:
395            steps += [
396                ActionStep(
397                    name="Install Rust",
398                    action=f"dtolnay/rust-toolchain@{toolchain}",
399                    params=params,
400                ),
401            ]
402        if cache:
403            steps += [
404                SccacheStep(name="Compile with sccache"),
405                # Cache vendored dependecies
406                CacheStep(
407                    name="Cache Rust Dependencies",
408                    path="vendor\n.cargo/config",
409                    key="cargo-deps-${{ hashFiles('**/Cargo.lock') }}",
410                    id="cache-cargo-vendor",
411                ),
412                # Vendor dependencies
413                RunStep(
414                    name="Vendor dependecies",
415                    condition="steps.cache-cargo-vendor.outputs.cache-hit != 'true'",
416                    run="cargo vendor --locked --versioned-dirs >> .cargo/config",
417                ),
418            ]
419        return steps
420
421    def install_system_deps(self):
422        if "win" in self.name:
423            return []
424        sudo = "sudo -n " if self.needs_sudo() else ""
425        return [
426            RunStep(
427                name="Install System Deps",
428                run=f"{sudo}env CI=yes PATH=$PATH ./get-deps",
429            )
430        ]
431
432    def fixup_windows_path(self, cmd):
433        if "win" in self.name:
434            return "PATH C:\\Strawberry\\perl\\bin;%PATH%\n" + cmd
435        return cmd
436
437    def build_all_release(self):
438        bin_crates = [
439            "wezterm",
440            "wezterm-gui",
441            "wezterm-mux-server",
442            "strip-ansi-escapes",
443        ]
444        steps = []
445        for bin in bin_crates:
446            if "win" in self.name:
447                steps += [
448                    RunStep(
449                        name=f"Build {bin} (Release mode)",
450                        shell="cmd",
451                        run=self.fixup_windows_path(f"cargo build -p {bin} --release"),
452                    )
453                ]
454            elif "macos" in self.name:
455                steps += [
456                    RunStep(
457                        name=f"Build {bin} (Release mode Intel)",
458                        run=f"cargo build --target x86_64-apple-darwin -p {bin} --release",
459                    ),
460                    RunStep(
461                        name=f"Build {bin} (Release mode ARM)",
462                        run=f"cargo build --target aarch64-apple-darwin -p {bin} --release",
463                    ),
464                ]
465            else:
466                if self.name == "centos7":
467                    enable = "source /opt/rh/devtoolset-9/enable && "
468                else:
469                    enable = ""
470                steps += [
471                    RunStep(
472                        name=f"Build {bin} (Release mode)",
473                        run=enable + f"cargo build -p {bin} --release",
474                    )
475                ]
476        return steps
477
478    def test_all(self):
479        run = "cargo nextest run --all --no-fail-fast"
480        if "macos" in self.name:
481            run += " --target=x86_64-apple-darwin"
482        if self.name == "centos7":
483            run = "source /opt/rh/devtoolset-9/enable\n" + run
484        return [
485            # Install cargo-nextest
486            InstallCrateStep("cargo-nextest", key=self.name),
487            # Run tests
488            RunStep(name="Test", run=self.fixup_windows_path(run), shell="cmd")
489            if "win" in self.name
490            else RunStep(name="Test", run=run),
491        ]
492
493    def package(self, trusted=False):
494        steps = []
495        deploy_env = None
496        if trusted and ("mac" in self.name):
497            deploy_env = {
498                "MACOS_CERT": "${{ secrets.MACOS_CERT }}",
499                "MACOS_CERT_PW": "${{ secrets.MACOS_CERT_PW }}",
500                "MACOS_TEAM_ID": "${{ secrets.MACOS_TEAM_ID }}",
501                "MACOS_APPLEID": "${{ secrets.MACOS_APPLEID }}",
502                "MACOS_APP_PW": "${{ secrets.MACOS_APP_PW }}",
503            }
504        steps = [RunStep("Package", "bash ci/deploy.sh", env=deploy_env)]
505        if self.app_image:
506            # AppImage needs fuse
507            steps += self.install_system_package("libfuse2")
508            steps.append(RunStep("Source Tarball", "bash ci/source-archive.sh"))
509            steps.append(RunStep("Build AppImage", "bash ci/appimage.sh"))
510        return steps
511
512    def upload_artifact(self):
513        steps = []
514
515        if self.uses_yum():
516            steps.append(
517                RunStep(
518                    "Move RPM",
519                    f"mv ~/rpmbuild/RPMS/*/*.rpm .",
520                )
521            )
522        elif self.uses_apk():
523            steps += [
524                # Add the distro name/version into the filename
525                RunStep(
526                    "Rename APKs",
527                    f"mv ~/packages/wezterm/x86_64/*.apk $(echo ~/packages/wezterm/x86_64/*.apk | sed -e 's/wezterm-/wezterm-{self.name}-/')",
528                ),
529                # Move it to the repo dir
530                RunStep(
531                    "Move APKs",
532                    f"mv ~/packages/wezterm/x86_64/*.apk .",
533                ),
534                # Move and rename the keys
535                RunStep(
536                    "Move APK keys",
537                    f"mv ~/.abuild/*.pub wezterm-{self.name}.pub",
538                ),
539            ]
540        elif self.uses_zypper():
541            steps.append(
542                RunStep(
543                    "Move RPM",
544                    f"mv /usr/src/packages/RPMS/*/*.rpm .",
545                )
546            )
547
548        patterns = self.asset_patterns()
549        glob = " ".join(patterns)
550        paths = "\n".join(patterns)
551
552        return steps + [
553            ActionStep(
554                "Upload artifact",
555                action="actions/upload-artifact@v4",
556                params={"name": self.name, "path": paths},
557            ),
558        ]
559
560    def asset_patterns(self):
561        patterns = []
562        if self.uses_yum() or self.uses_zypper():
563            patterns += ["wezterm-*.rpm"]
564        elif "win" in self.name:
565            patterns += ["WezTerm-*.zip", "WezTerm-*.exe"]
566        elif "mac" in self.name:
567            patterns += ["WezTerm-*.zip"]
568        elif ("ubuntu" in self.name) or ("debian" in self.name):
569            patterns += ["wezterm-*.deb", "wezterm-*.xz"]
570        elif "alpine" in self.name:
571            patterns += ["wezterm-*.apk"]
572            if self.is_tag:
573                patterns.append("*.pub")
574
575        if self.app_image:
576            patterns.append("*src.tar.gz")
577            patterns.append("*.AppImage")
578            #patterns.append("*.zsync") broken upstream: <https://github.com/linuxdeploy/linuxdeploy/issues/309>
579        return patterns
580
581    def upload_artifact_nightly(self):
582        steps = []
583
584        if self.uses_yum():
585            steps.append(
586                RunStep(
587                    "Move RPM",
588                    f"mv ~/rpmbuild/RPMS/*/*.rpm wezterm-nightly-{self.name}.rpm",
589                )
590            )
591        elif self.uses_apk():
592            steps.append(
593                RunStep(
594                    "Move APKs",
595                    f"mv ~/packages/wezterm/x86_64/*.apk wezterm-nightly-{self.name}.apk",
596                )
597            )
598        elif self.uses_zypper():
599            steps.append(
600                RunStep(
601                    "Move RPM",
602                    f"mv /usr/src/packages/RPMS/*/*.rpm wezterm-nightly-{self.name}.rpm",
603                )
604            )
605
606        patterns = self.asset_patterns()
607        glob = " ".join(patterns)
608        paths = "\n".join(patterns)
609
610        return steps + [
611            ActionStep(
612                "Upload artifact",
613                action="actions/upload-artifact@v4",
614                params={"name": self.name, "path": paths, "retention-days": 5},
615            ),
616        ]
617
618    def upload_asset_nightly(self):
619        steps = []
620
621        patterns = self.asset_patterns()
622        checksum = RunStep(
623            "Checksum",
624            f"for f in {' '.join(patterns)} ; do sha256sum $f > $f.sha256 ; done",
625        )
626
627        patterns.append("*.sha256")
628        glob = " ".join(patterns)
629
630        if self.container == "ubuntu:22.04":
631            steps += [
632                RunStep(
633                    "Upload to gemfury",
634                    f"for f in wezterm*.deb ; do curl -i -F package=@$f https://$FURY_TOKEN@push.fury.io/wez/ ; done",
635                    env={"FURY_TOKEN": "${{ secrets.FURY_TOKEN }}"},
636                ),
637            ]
638
639        return [
640            ActionStep(
641                "Download artifact",
642                action="actions/download-artifact@v4",
643                params={"name": self.name},
644            ),
645            checksum,
646            RunStep(
647                "Upload to Nightly Release",
648                f"bash ci/retry.sh gh release upload --clobber nightly {glob}",
649                env={"GITHUB_TOKEN": "${{ secrets.GITHUB_TOKEN }}"},
650            ),
651        ] + steps
652
653    def upload_asset_tag(self):
654        steps = []
655
656        patterns = self.asset_patterns()
657        checksum = RunStep(
658            "Checksum",
659            f"for f in {' '.join(patterns)} ; do sha256sum $f > $f.sha256 ; done",
660        )
661
662        patterns.append("*.sha256")
663        glob = " ".join(patterns)
664
665        if self.container == "ubuntu:22.04":
666            steps += [
667                RunStep(
668                    "Upload to gemfury",
669                    f"for f in wezterm*.deb ; do curl -i -F package=@$f https://$FURY_TOKEN@push.fury.io/wez/ ; done",
670                    env={"FURY_TOKEN": "${{ secrets.FURY_TOKEN }}"},
671                ),
672            ]
673
674        return steps + [
675            ActionStep(
676                "Download artifact",
677                action="actions/download-artifact@v4",
678                params={"name": self.name},
679            ),
680            checksum,
681            RunStep(
682                "Create pre-release",
683                "bash ci/retry.sh bash ci/create-release.sh $(ci/tag-name.sh)",
684                env={
685                    "GITHUB_TOKEN": "${{ secrets.GITHUB_TOKEN }}",
686                },
687            ),
688            RunStep(
689                "Upload to Tagged Release",
690                f"bash ci/retry.sh gh release upload --clobber $(ci/tag-name.sh) {glob}",
691                env={
692                    "GITHUB_TOKEN": "${{ secrets.GITHUB_TOKEN }}",
693                },
694            ),
695        ]
696
697    def create_flathub_pr(self):
698        if not self.app_image:
699            return []
700        return [
701            ActionStep(
702                "Checkout flathub/org.wezfurlong.wezterm",
703                action="actions/checkout@v4",
704                params={
705                    "repository": "flathub/org.wezfurlong.wezterm",
706                    "path": "flathub",
707                    "token": "${{ secrets.GH_PAT }}",
708                },
709            ),
710            RunStep(
711                "Create flathub commit and push",
712                "bash ci/make-flathub-pr.sh",
713            ),
714            RunStep(
715                "Submit PR",
716                'cd flathub && gh pr create --fill --body "PR automatically created by release automation in the wezterm repo"',
717                env={
718                    "GITHUB_TOKEN": "${{ secrets.GH_PAT }}",
719                },
720            ),
721        ]
722
723    def create_winget_pr(self):
724        steps = []
725        if "windows" in self.name:
726            steps += [
727                ActionStep(
728                    "Checkout winget-pkgs",
729                    action="actions/checkout@v4",
730                    params={
731                        "repository": "wez/winget-pkgs",
732                        "path": "winget-pkgs",
733                        "token": "${{ secrets.GH_PAT }}",
734                    },
735                ),
736                RunStep(
737                    "Setup email for winget repo",
738                    "cd winget-pkgs && git config user.email wez@wezfurlong.org",
739                ),
740                RunStep(
741                    "Setup name for winget repo",
742                    "cd winget-pkgs && git config user.name 'Wez Furlong'",
743                ),
744                RunStep(
745                    "Create winget manifest and push to fork",
746                    "bash ci/make-winget-pr.sh winget-pkgs WezTerm-*.exe",
747                ),
748                RunStep(
749                    "Submit PR",
750                    'cd winget-pkgs && gh pr create --fill --body "PR automatically created by release automation in the wezterm repo"',
751                    env={
752                        "GITHUB_TOKEN": "${{ secrets.GH_PAT }}",
753                    },
754                ),
755            ]
756
757        return steps
758
759    def update_homebrew_tap(self):
760        steps = []
761        if "macos" in self.name:
762            steps += [
763                ActionStep(
764                    "Checkout homebrew tap",
765                    action="actions/checkout@v4",
766                    params={
767                        "repository": "wez/homebrew-wezterm",
768                        "path": "homebrew-wezterm",
769                        "token": "${{ secrets.GH_PAT }}",
770                    },
771                ),
772                RunStep(
773                    "Update homebrew tap formula",
774                    "cp wezterm.rb homebrew-wezterm/Casks/wezterm.rb",
775                ),
776                ActionStep(
777                    "Commit homebrew tap changes",
778                    action="stefanzweifel/git-auto-commit-action@v5",
779                    params={
780                        "commit_message": "Automated update to match latest tag",
781                        "repository": "homebrew-wezterm",
782                    },
783                ),
784            ]
785        elif self.app_image:
786            steps += [
787                ActionStep(
788                    "Checkout linuxbrew tap",
789                    action="actions/checkout@v4",
790                    params={
791                        "repository": "wez/homebrew-wezterm-linuxbrew",
792                        "path": "linuxbrew-wezterm",
793                        "token": "${{ secrets.GH_PAT }}",
794                    },
795                ),
796                RunStep(
797                    "Update linuxbrew tap formula",
798                    "cp wezterm-linuxbrew.rb linuxbrew-wezterm/Formula/wezterm.rb",
799                ),
800                ActionStep(
801                    "Commit linuxbrew tap changes",
802                    action="stefanzweifel/git-auto-commit-action@v5",
803                    params={
804                        "commit_message": "Automated update to match latest tag",
805                        "repository": "linuxbrew-wezterm",
806                    },
807                ),
808            ]
809
810        return steps
811
812    def global_env(self):
813        self.env["CARGO_INCREMENTAL"] = "0"
814        self.env["SCCACHE_GHA_ENABLED"] = "true"
815        self.env["RUSTC_WRAPPER"] = "sccache"
816        if "macos" in self.name:
817            self.env["MACOSX_DEPLOYMENT_TARGET"] = "10.12"
818        if "alpine" in self.name:
819            self.env["RUSTFLAGS"] = "-C target-feature=-crt-static"
820        if "win" in self.name:
821            self.env["RUSTUP_WINDOWS_PATH_ADD_BIN"] = "1"
822        return
823
824    def prep_environment(self, cache=True):
825        steps = []
826        sudo = "sudo -n " if self.needs_sudo() else ""
827        if self.uses_apt():
828            if self.container:
829                steps += [
830                    RunStep(
831                        "set APT to non-interactive",
832                        "echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections",
833                    ),
834                ]
835            steps += [
836                RunStep("Update APT", f"{sudo}apt update"),
837            ]
838
839        if self.uses_zypper():
840            if self.container:
841                steps += [
842                    RunStep(
843                        "Seed GITHUB_PATH to work around possible @action/core bug",
844                        f'echo "$PATH:/bin:/usr/bin" >> $GITHUB_PATH',
845                    ),
846                    RunStep(
847                        "Install util-linux",
848                        "zypper install -y util-linux",
849                    ),
850                ]
851        if self.container:
852            if ("fedora" in self.container) or (
853                ("centos" in self.container) and ("centos7" not in self.container)
854            ):
855                steps += [
856                    RunStep(
857                        "Install config manager",
858                        "dnf install -y 'dnf-command(config-manager)'",
859                    ),
860                ]
861            if "centos:stream8" in self.container:
862                steps += [
863                    RunStep(
864                        "Enable PowerTools",
865                        "dnf config-manager --set-enabled powertools",
866                    ),
867                ]
868            if "centos:stream9" in self.container:
869                steps += [
870                    # This holds the xcb bits
871                    RunStep(
872                        "Enable CRB repo for X bits",
873                        "dnf config-manager --set-enabled crb",
874                    ),
875                ]
876            if "alpine" in self.container:
877                steps += [
878                    RunStep(
879                        "Upgrade system",
880                        "apk upgrade --update-cache",
881                        shell="sh",
882                    ),
883                    RunStep(
884                        "Install CI dependencies",
885                        "apk add nodejs zstd wget bash coreutils tar findutils",
886                        shell="sh",
887                    ),
888                    RunStep(
889                        "Allow root login",
890                        "sed 's/root:!/root:*/g' -i /etc/shadow",
891                    ),
892                ]
893            if "opensuse" in self.container:
894                steps += [
895                    # This holds the xcb bits
896                    RunStep(
897                        "Install tar",
898                        "zypper install -yl tar gzip",
899                    ),
900                ]
901
902        steps += self.install_newer_compiler()
903        steps += self.install_git()
904        steps += self.install_curl()
905
906        if self.uses_apt():
907            if self.container:
908                steps += [
909                    RunStep("Update APT", f"{sudo}apt update"),
910                ]
911
912        steps += self.install_openssh_server()
913        steps += self.checkout()
914        # We should be able to cache mac builds now?
915        steps += self.install_rust()  # cache="mac" not in self.name)
916        steps += self.install_system_deps()
917        return steps
918
919    def pull_request(self):
920        steps = self.prep_environment()
921        steps += self.build_all_release()
922        steps += self.test_all()
923        steps += self.package()
924        steps += self.upload_artifact()
925
926        return (
927            Job(
928                runs_on=self.os,
929                container=self.container,
930                steps=steps,
931                env=self.env,
932            ),
933            None,
934        )
935
936    def checkout(self, submodules=True):
937        steps = []
938        if self.container:
939            steps += [
940                RunStep(
941                    "Workaround git permissions issue",
942                    "git config --global --add safe.directory /__w/wezterm/wezterm",
943                )
944            ]
945        steps += [CheckoutStep(submodules=submodules, container=self.container)]
946        return steps
947
948    def continuous(self):
949        steps = self.prep_environment()
950        steps += self.build_all_release()
951        steps += self.test_all()
952        steps += self.package(trusted=True)
953        steps += self.upload_artifact_nightly()
954
955        self.env["BUILD_REASON"] = "Schedule"
956
957        uploader = Job(
958            runs_on="ubuntu-latest",
959            steps=self.checkout(submodules=False) + self.upload_asset_nightly(),
960        )
961
962        return (
963            Job(
964                runs_on=self.os,
965                container=self.container,
966                steps=steps,
967                env=self.env,
968            ),
969            uploader,
970        )
971
972    def tag(self):
973        steps = self.prep_environment()
974        steps += self.build_all_release()
975        steps += self.test_all()
976        steps += self.package(trusted=True)
977        steps += self.upload_artifact()
978
979        uploader = Job(
980            runs_on="ubuntu-latest",
981            steps=self.checkout(submodules=False)
982            + self.update_homebrew_tap()
983            + self.upload_asset_tag()
984            + self.create_winget_pr()
985            + self.create_flathub_pr(),
986        )
987
988        return (
989            Job(
990                runs_on=self.os,
991                container=self.container,
992                steps=steps,
993                env=self.env,
994            ),
995            uploader,
996        )
997
998
999TARGETS = [
1000    Target(container="ubuntu:20.04", continuous_only=True, app_image=True),
1001    Target(container="ubuntu:22.04", continuous_only=True),
1002    Target(container="ubuntu:24.04", continuous_only=True),
1003    # debian 8's wayland libraries are too old for wayland-client
1004    # Target(container="debian:8.11", continuous_only=True, bootstrap_git=True),
1005    # harfbuzz's C++ is too new for debian 9's toolchain
1006    # Target(container="debian:9.12", continuous_only=True, bootstrap_git=True),
1007    Target(container="debian:10.3", continuous_only=True),
1008    Target(container="debian:11", continuous_only=True),
1009    Target(container="debian:12", continuous_only=True),
1010    Target(name="centos9", container="quay.io/centos/centos:stream9"),
1011    Target(name="macos", os="macos-latest"),
1012    # https://fedoraproject.org/wiki/End_of_life?rd=LifeCycle/EOL
1013    Target(container="fedora:39"),
1014    Target(container="fedora:40"),
1015    Target(container="fedora:41"),
1016    # Target(container="alpine:3.15"),
1017    Target(name="windows", os="windows-latest", rust_target="x86_64-pc-windows-msvc"),
1018]
1019
1020
1021def generate_actions(namer, jobber, trigger, is_continuous, is_tag=False):
1022    for t in TARGETS:
1023        # Clone the definition, as some Target methods called
1024        # in the body below have side effects that we don't
1025        # want to bleed across into different schedule types
1026        t = deepcopy(t)
1027
1028        t.is_tag = is_tag
1029        # if t.continuous_only and not is_continuous:
1030        #    continue
1031        name = namer(t).replace(":", "")
1032        print(name)
1033        job, uploader = jobber(t)
1034
1035        file_name = f".github/workflows/gen_{name}.yml"
1036        if job.container:
1037            if t.app_image:
1038                container = f"container:\n      image: {yv(job.container)}\n      options: --privileged"
1039            else:
1040                container = f"container: {yv(job.container)}"
1041
1042        else:
1043            container = ""
1044
1045        trigger_paths = [file_name]
1046        trigger_paths += TRIGGER_PATHS
1047        if "win" in name:
1048            trigger_paths += TRIGGER_PATHS_WIN
1049        elif "macos" in name:
1050            trigger_paths += TRIGGER_PATHS_MAC
1051        else:
1052            trigger_paths += TRIGGER_PATHS_UNIX
1053        if t.app_image:
1054            trigger_paths += TRIGGER_PATHS_APPIMAGE
1055
1056        trigger_paths = "- " + "\n      - ".join(yv(p) for p in sorted(trigger_paths))
1057        trigger_with_paths = trigger.replace("@PATHS@", trigger_paths)
1058
1059        with open(file_name, "w") as f:
1060            f.write(
1061                f"""name: {name}
1062{trigger_with_paths}
1063jobs:
1064  build:
1065    runs-on: {yv(job.runs_on)}
1066    {container}
1067"""
1068            )
1069
1070            t.render_env(f)
1071
1072            job.render(f, 3)
1073
1074            # We upload using a native runner as github API access
1075            # inside a container is really unreliable and can result
1076            # in broken releases that can't automatically be repaired
1077            # <https://github.com/cli/cli/issues/4863>
1078            if uploader:
1079                f.write(
1080                    """
1081  upload:
1082    runs-on: ubuntu-latest
1083    needs: build
1084    if: github.repository == 'wezterm/wezterm'
1085    permissions:
1086      contents: write
1087      pages: write
1088      id-token: write
1089"""
1090                )
1091                uploader.render(f, 3)
1092
1093        # Sanity check the yaml, if pyyaml is available
1094        try:
1095            import yaml
1096
1097            with open(file_name) as f:
1098                yaml.safe_load(f)
1099        except ImportError:
1100            pass
1101
1102
1103def generate_pr_actions():
1104    generate_actions(
1105        lambda t: f"{t.name}",
1106        lambda t: t.pull_request(),
1107        trigger="""
1108on:
1109  pull_request:
1110    branches:
1111      - main
1112    paths:
1113      @PATHS@
1114""",
1115        is_continuous=False,
1116    )
1117
1118
1119def continuous_actions():
1120    generate_actions(
1121        lambda t: f"{t.name}_continuous",
1122        lambda t: t.continuous(),
1123        trigger="""
1124on:
1125  schedule:
1126    - cron: "10 3 * * *"
1127  push:
1128    branches:
1129      - main
1130    paths:
1131      @PATHS@
1132""",
1133        is_continuous=True,
1134    )
1135
1136
1137def tag_actions():
1138    generate_actions(
1139        lambda t: f"{t.name}_tag",
1140        lambda t: t.tag(),
1141        trigger="""
1142on:
1143  push:
1144    tags:
1145      - "20*"
1146""",
1147        is_continuous=True,
1148        is_tag=True,
1149    )
1150
1151
1152def remove_gen_actions():
1153    for name in glob.glob(".github/workflows/gen_*.yml"):
1154        os.remove(name)
1155
1156
1157remove_gen_actions()
1158generate_pr_actions()
1159continuous_actions()
1160tag_actions()