Gitlab CI copy file with variables

时间:2016-08-31 18:23:20

标签: gitlab gitlab-ci

I have a django repository in gitlab service. And I use gitlab CI runner in my VDS. I want a copy some file during deploy process, for example local_settings.py and fill variables only on deploy, for example like this:

# $CI_PROJECT_NAME/$CI_PROJECT_NAME/deploy_templates/local_settings.py.tpl
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'dbname',
        'USERNAME': '$DB_USERNAME',
        'PASSWORD': '$DB_PASSWORD',
    },
}

# .gitlab-ci.yml
configure_project:
    script:
        - git clone $CI_BUILD_REPO
        - cp $CI_PROJECT_NAME/$CI_PROJECT_NAME/deploy_templates/local_settings.py.tpl $CI_PROJECT_NAME/$CI_PROJECT_NAME/local_settings.py

In my project I define DB_USERNAME and DB_PASSWORD secure variables. How I can to get file with values? I need something like template directive from ansible

1 个答案:

答案 0 :(得分:0)

这可以使用envsubst命令来实现。在您的.gitlab-ci.yml文件中,您可以执行以下操作:

configure_project:
    script:
        - git clone $CI_BUILD_REPO
        - envsubst < "$CI_PROJECT_NAME/$CI_PROJECT_NAME/deploy_templates/local_settings.py.tpl" > "local_settings.py.tpl.exported"
        - cp local_settings.py.tpl.exported $CI_PROJECT_NAME/$CI_PROJECT_NAME/local_settings.py

envsubst将替换环境变量的值,并且输出可以重定向到另一个名为local_settings.py.tpl.exported的文件,您可以复制该文件。