How to create a cross platform zip archive using GitHub Actions

Photo by Tomas Sobek on Unsplash

How to create a cross platform zip archive using GitHub Actions

ยท

1 min read

Let me set some context to understand the problem at hand, zip command is available by default on all *nix OS flavours which includes both Linux and Mac OS. On latest Windows server, there is no zip as command readily available but GH Actions provide 7z in Windows runners. So the first problem is that we have use different commands on nix and Windows.

Secondly, you would have to run in different shells i.e. use bash for *nix and cmd or powershell on Windows hence will result in having 2 different tasks using different shells.

Fortunately, GH Actions have PowerShell installed on both *nix and Windows, this allows us to use a single task running PowerShell as a shell.

To choose which command to run, use an if condition to check the RUNNER_OS GH environment variable to use 7z for Windows and zip for *nix. This way, we can run a cross platform matrix build running on Linux, Mac OS and Windows to create a zip archive.

PowerShell Compress-Archive can be used for zip in Windows.

An example YAML is as shown below for your reference.

- name: Zip files
  shell: pwsh
    run: |
      if ($env:RUNNER_OS -eq "Windows") {
        7z a test.zip my_folder 
      }
      else {
        zip -r test.zip my_folder
      }
ย