Integrate 7-Zip in CI/CD Pipelines

This article explains how to integrate 7-Zip compression into Continuous Integration and Continuous Deployment (CI/CD) pipelines to package build artifacts, reduce storage consumption, and accelerate deployment transfer speeds. It covers CLI installation across different operating systems, essential command-line arguments optimized for automated environments, and practical pipeline configurations for GitHub Actions and GitLab CI.

Why Use 7-Zip in CI/CD

7-Zip provides higher compression ratios than standard ZIP utilities through its proprietary 7z format and LZMA/LZMA2 algorithms. In automated pipelines, smaller build artifacts reduce network bandwidth usage, speed up upload and download steps between pipeline stages, and decrease cloud storage costs for artifact repositories.

Installing 7-Zip on Build Runners

Most cloud-hosted runners require you to install 7-Zip during pipeline execution unless you use pre-configured container images.

  • Ubuntu / Debian Runners:
    sudo apt-get update && sudo apt-get install -y p7zip-full
  • macOS Runners:
    brew install sevenzip
  • Windows Runners: Windows runners often have 7-Zip pre-installed. If not, install it using Chocolatey:
    choco install 7zip -y

When scripting 7-Zip, specific flags ensure stability and efficiency within a non-interactive pipeline:

  • a: Add files to archive.
  • -t7z or -tzip: Specifies the archive format.
  • -mx[0-9]: Sets the compression level. Use -mx=1 (fastest) for quick intermediate stage artifacts, or -mx=9 (ultra) for final release packages.
  • -mmt=on: Enables multithreading to leverage all available runner CPU cores.
  • -bso0 -bsp0: Suppresses standard output and progress indicators to keep pipeline logs clean.
  • -y: Assumes "yes" to all interactive prompts to prevent pipeline hanging.

Example command for maximum compression:

7z a -t7z -mx=9 -mmt=on -y build-artifact.7z ./dist/*

GitHub Actions Implementation

The following workflow demonstrates checking out code, building an application, compressing the output using 7-Zip, and uploading the compressed archive as a GitHub artifact.

name: Build and Archive

on:
  push:
    branches: [ "main" ]

jobs:
  build-and-compress:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Install 7-Zip
        run: sudo apt-get update && sudo apt-get install -y p7zip-full

      - name: Build Project
        run: |
          mkdir dist
          echo "Sample build file content" > dist/app.bin

      - name: Compress Artifacts with 7-Zip
        run: |
          7z a -t7z -mx=7 -mmt=on release.7z ./dist

      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: packaged-release
          path: release.7z

GitLab CI Implementation

The following .gitlab-ci.yml configuration installs 7-Zip inside an Ubuntu-based Docker container, packages the build directory, and preserves the archive for subsequent stages.

stages:
  - build
  - package

package_artifacts:
  stage: package
  image: ubuntu:latest
  before_script:
    - apt-get update && apt-get install -y p7zip-full
  script:
    - mkdir -p output
    - echo "Build output" > output/data.txt
    - 7z a -t7z -mx=7 -mmt=on artifacts.7z ./output
  artifacts:
    paths:
      - artifacts.7z
    expire_in: 1 week

Best Practices for CI/CD Workflows

  1. Balance CPU Cost and Compression: Ultra compression (-mx=9) consumes significant CPU time. In environments where compute minutes are billed, standard compression (-mx=5) often provides the best balance between speed and file size reduction.
  2. Handle Exit Codes: 7-Zip returns exit code 0 on success, 1 on non-fatal warnings, and 2 or higher on fatal errors. Ensure your scripts evaluate exit codes properly to prevent silent deployment failures.
  3. Automate Decompression in Downstream Jobs: Downstream deployment jobs must include the extraction command before processing files:
    7z x artifacts.7z -o./destination -y