DEV Community

Sachin Kumar Singh
Sachin Kumar Singh

Posted on

Using environment variables in melange

It is so simple to use environment variable when building a apko package using melange yet I couldn't find an examples to make a developers life easy so here is my attempt for that -

Create the env file with required variables

Example below shows a way to add some predefined gitlab CI variables allowing you to clone the repository -

    - echo CI_JOB_TOKEN=$CI_JOB_TOKEN >> .env
    - echo CI_COMMIT_REF_NAME=$CI_COMMIT_REF_NAME >> .env
    - echo CI_SERVER_FQDN=$CI_SERVER_FQDN >> .env

Enter fullscreen mode Exit fullscreen mode

Update melange yaml to use the variables

...
package:
  name: sample-app
...
...
vars:
  CI_JOB_TOKEN: ${CI_JOB_TOKEN}

pipeline:
  - working-directory: /home/user
    pipeline:
      - runs: |
          git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@${CI_SERVER_FQDN}/some-group/some-project -b $CI_COMMIT_REF_NAME
          APP_HOME="${{targets.destdir}}/opt/app"
          mkdir -p ${APP_HOME}
          pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Run melange build

melange build --signing-key melange.rsa --env-file .env --runner bubblewrap --rm --namespace xyz --out-dir some-dir --cache-dir another-dir
Enter fullscreen mode Exit fullscreen mode

So easy!

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay