Gitlab CI皮棉:作业配置应为包含字符串和字符串数组的数组

时间:2020-04-25 11:06:36

标签: gitlab gitlab-ci

我正在尝试编写gitlab CI文件,如下所示:

image: ubuntu:latest

variables:
    GIT_SUBMODULE_STRATEGY: recursive
    AWS_DEFAULT_REGION: eu-central-1
    S3_BUCKET: $BUCKET_TRIAL

stages:
    - deploy

.before_script_template: &before_script_definition
    stage: deploy
    before_script:
        - apt-get -y update
        - apt-get -y install python3-pip python3.7 zip
        - python3.7 -m pip install --upgrade pip
        - python3.7 -V
        - pip3.7 install virtualenv

.after_script_template: &after_script_definition
    after_script:
        # Upload package to S3
        # Install AWS CLI
        - pip install awscli --upgrade # --user
        - export PATH=$PATH:~/.local/bin  # Add to PATH

        # Configure AWS connection
        - aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
        - aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
        - aws configure set default.region $AWS_DEFAULT_REGION
        - aws sts get-caller-identity --output text --query 'Account'  # current account
        - aws s3 cp ~/forlambda/archive.zip $BUCKET_TRIAL/${LAMBDA_NAME}-deployment.zip

monatliche_strom:
    variables:
        LAMBDA_NAME: monthly_strom
    before_script: *before_script_definition
    script:
        - mv some.py ~
        - mv requirements.txt ~

        # Move submodules
        - mv submodule1/submodule1 ~
        - mv submodule1/submodule2/submodule2 ~

        # Setup virtual environment
        - mkdir ~/forlambda
        - cd ~/forlambda
        - virtualenv -p python3 venv
        - source venv/bin/activate

        - pip3.7 install -r ~/requirements.txt -t ~/forlambda/venv/lib/python3.7/site-packages/

        # Package environment and dependencies
        - cd ~/forlambda/venv/lib/python3.7/site-packages/
        - zip -r9 ~/forlambda/archive.zip .
        - cd ~
        - zip -g ~/forlambda/archive.zip some.py
        - zip -r ~/forlambda/archive.zip submodule1/*
        - zip -r ~/forlambda/archive.zip submodule2/*
    after_script: *after_script_definition

当我在gitlab CI皮棉中运行它时,它给我以下错误:

jobs:monatliche_strom:before_script配置应为数组 包含字符串和字符串数组

jobs:monatliche_strom:after_script配置应为数组 包含字符串和字符串数组

我对gitlab CI还是很陌生,所以有人可以告诉我我在做什么错吗?

1 个答案:

答案 0 :(得分:1)

尝试一下:

image: ubuntu:latest

variables:
    GIT_SUBMODULE_STRATEGY: recursive
    AWS_DEFAULT_REGION: eu-central-1
    S3_BUCKET: $BUCKET_TRIAL

stages:
    - deploy

.before_script_template: &before_script_definition
    stage: deploy
    before_script:
        - apt-get -y update
        - apt-get -y install python3-pip python3.7 zip
        - python3.7 -m pip install --upgrade pip
        - python3.7 -V
        - pip3.7 install virtualenv

.after_script_template: &after_script_definition
    after_script:
        # Upload package to S3
        # Install AWS CLI
        - pip install awscli --upgrade # --user
        - export PATH=$PATH:~/.local/bin  # Add to PATH
        # Configure AWS connection
        - aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
        - aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
        - aws configure set default.region $AWS_DEFAULT_REGION
        - aws sts get-caller-identity --output text --query 'Account'  # current account
        - aws s3 cp ~/forlambda/archive.zip $BUCKET_TRIAL/${LAMBDA_NAME}-deployment.zip

monatliche_strom:
    variables:
        LAMBDA_NAME: monthly_strom
    <<: *before_script_definition
    script:
        - mv some.py ~
        - mv requirements.txt ~
        # Move submodules
        - mv submodule1/submodule1 ~
        - mv submodule1/submodule2/submodule2 ~
        # Setup virtual environment
        - mkdir ~/forlambda
        - cd ~/forlambda
        - virtualenv -p python3 venv
        - source venv/bin/activate
        - pip3.7 install -r ~/requirements.txt -t ~/forlambda/venv/lib/python3.7/site-packages/
        # Package environment and dependencies
        - cd ~/forlambda/venv/lib/python3.7/site-packages/
        - zip -r9 ~/forlambda/archive.zip .
        - cd ~
        - zip -g ~/forlambda/archive.zip some.py
        - zip -r ~/forlambda/archive.zip submodule1/*
        - zip -r ~/forlambda/archive.zip submodule2/*
    <<: *after_script_definition

由于您已经在锚点中描述了before_scriptafter_script,因此必须使用<<将给定的哈希值合并到当前哈希值中