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!

Top comments (0)