Skip to main content
In this lesson we demonstrate how to use a custom transformer to add, map, or remove environment variables while converting Jenkins pipelines into GitHub Actions workflows. The transformer lets you:
  • Map Jenkins env vars to GitHub Actions env vars
  • Convert Jenkins variables into GitHub Actions secrets
  • Remove variables from the generated workflow
A presentation slide with the title "Custom Transformer — environment variables" centered on a blue-green gradient background. A small "© Copyright KodeKloud" appears in the bottom-left corner.
Overview
  • The custom transformer runs during the import/dry-run and modifies the resulting workflow’s env section.
  • You author rules in a small Ruby DSL file that the importer evaluates to translate or remove variables.
  • This process is especially useful for mapping credential references to GitHub Actions secrets.
Transformer syntax examples Below are common transformer directives used in the Ruby DSL. You can place these in a transformer file (e.g., ss-pipeline-transformer.rb).
You can match multiple variables using regular expressions:
Quick reference Jenkins pipeline example This is the Jenkinsfile we will convert — a simple two-stage pipeline triggered by poll SCM:
Dry-run conversion (initial) First, run a dry-run conversion without any custom transformer to inspect the base output and see which environment variables remain unresolved:
Example dry-run output (summarized):
Generated workflow (excerpt) The importer generates a workflow that often requires manual tuning. Here are representative excerpts to show where env vars appear and how some items may be left as commented placeholders when the importer doesn’t have a mapping. Example job (Trivy scan):
Workflow header, triggers and environment (note the converted cron and partially converted env vars):
Why some env vars were not transformed
  • The importer converts SCM polling to a GitHub Actions cron schedule automatically.
  • Credential references stored in Jenkins credential stores are typically not transformed by default. Those appear as commented placeholders with the message # This item has no matching transformer.
  • Use a custom transformer to explicitly map these variables to literals or secrets.
Creating a custom transformer to handle environment variables Create a transformer file (for example ss-pipeline-transformer.rb) and use env directives to add mappings, mark values as secrets, or remove variables. Example transformer (Ruby DSL):
  • secret("mongo_db_password") indicates the importer should reference the GitHub Actions secret named mongo_db_password.
  • Setting a variable to nil removes it from the generated workflow.
Run dry-run with the custom transformer Re-run the importer specifying the transformer file:
If you see a message such as:
it usually means the transformer path is incorrect relative to your current working directory. Verify the file exists (for example ls ss-pipeline-transformer.rb) and either run the command from that directory or supply an absolute path to --custom-transformers.
Resulting transformed workflow (excerpt) After applying the custom transformer, the env section reflects the mappings and secrets:
Best practices and tips
  • Keep transformer files in your project repository and reference them with a relative or absolute path to avoid “not found” errors.
  • Prefer mapping credentials to GitHub Actions secrets rather than hard-coding sensitive values.
  • Use regular expressions cautiously to avoid accidentally removing environment variables you need.
Summary
  • Use env "NAME", "value" to map literals, env "NAME", secret("KEY") to map to GitHub Actions secrets, and env "NAME", nil to remove variables from the generated workflow.
  • If the importer cannot find your custom transformer, verify the path and working directory or use an absolute path.
  • This article focuses on env var transformation; other Jenkins-specific constructs (e.g., complex credential bindings or specialized plugins) may require additional custom handling and are covered separately.
Links and references

Watch Video