image: node:20.12.2

clone:
    depth: 5000       # include the last five hundred commits

pipelines:
    # Start pipeline with tag to build examples zips and copy them to the Bitbucket download folder
    tags:
        'v3.*.*':
            -   step:
                    name: build examples zip
                    caches:
                        - node
                    artifacts:
                        - dist/**
                    script:
                        - export NODE_OPTIONS=--openssl-legacy-provider
                        - npm install
                        - npm run buildExamples
                        - apt-get update
            -   step:
                    name: deploy examples zip
                    caches:
                        - node
                    script:
                        - apt-get install -y curl
                        # find all zip folders in dist folder, loop through the results and post the zips to the BB download folder
                        - for zipFilePath in $(find dist -name \*.zip); do
                            curl -X POST --user "$bbapi_name:$bbapi_key" "https://api.bitbucket.org/2.0/repositories/geowerkstatt-hamburg/masterportal/downloads" --form files=@"$zipFilePath" --verbose;
                            done
            -   step:
                    name: create mkdocs
                    image: python:3.8
                    caches:
                        - pip
                    script:
                        - apt-get update && apt-get install zip && apt-get install -y curl wput jq  && apt-get install zip
                        - pip install -r devtools/docsValidation/python-dependencies.txt
                        - mike deploy $BITBUCKET_TAG
                        - mv $BITBUCKET_CLONE_DIR/site $BITBUCKET_CLONE_DIR/$BITBUCKET_TAG
                        -   pipe: atlassian/ftp-deploy:0.7.0
                            variables:
                                USER: $MKDOCS_FTP_NAME
                                PASSWORD: $MKDOCS_FTP_PW
                                SERVER: $MKDOCS_FTP_SERVER
                                REMOTE_PATH: '/doc/'
                                LOCAL_PATH: $BITBUCKET_CLONE_DIR/$BITBUCKET_TAG
                                DEBUG: 'true'
                        # TODO: enable, if version 3 shall be Latest
                        # - mv $BITBUCKET_CLONE_DIR/$BITBUCKET_TAG $BITBUCKET_CLONE_DIR/Latest
                        # -   pipe: atlassian/ftp-deploy:0.7.0
                        #     variables:
                        #         USER: $MKDOCS_FTP_NAME
                        #         PASSWORD: $MKDOCS_FTP_PW
                        #         SERVER: $MKDOCS_FTP_SERVER
                        #         REMOTE_PATH: '/doc/'
                        #         LOCAL_PATH: '$BITBUCKET_CLONE_DIR/Latest'
                        #         DEBUG: 'true'
            -   step:
                    name: deploy on openCode
                    script:
                        - echo 'deply to opencode:'$BITBUCKET_TAG
                        - git fetch --all --tags
                        - git checkout tags/$BITBUCKET_TAG
                        - git push https://$OpenCode_User:$OpenCode_Password@gitlab.opencode.de/geowerkstatt-hamburg/masterportal.git $BITBUCKET_TAG
                        - git clone --single-branch --branch dev_vue https://geowerkstatt@bitbucket.org/geowerkstatt-hamburg/masterportal.git
                        - cd masterportal
                        - git push https://$OpenCode_User:$OpenCode_Password@gitlab.opencode.de/geowerkstatt-hamburg/masterportal.git -f
    custom: # Pipelines that are triggered manually
        npm audit:
            -   step:
                    name: npm audit (security)
                    caches:
                        - node
                    script:
                        - npm install
                        - npm audit --json
        deploy last tag on openCode:
            - variables:
                - name: BITBUCKET_TAG
                  description: tag to publish docs for, e.g. v3.1.0
            -   step:
                    name: deploy last tag on openCode
                    script:
                        - apt-get update && apt-get install -y curl wput jq
                        - git fetch --all --tags
                        - echo 'deply to opencode:'$BITBUCKET_TAG
                        - git checkout tags/$BITBUCKET_TAG
                        - git push https://$OpenCode_User:$OpenCode_Password@gitlab.opencode.de/geowerkstatt-hamburg/masterportal.git $BITBUCKET_TAG
                        - git clone --single-branch --branch dev_vue https://geowerkstatt@bitbucket.org/geowerkstatt-hamburg/masterportal.git
                        - cd masterportal
                        - git push https://$OpenCode_User:$OpenCode_Password@gitlab.opencode.de/geowerkstatt-hamburg/masterportal.git -f
        mkdocs to masterportal.org:
            - variables:
                - name: BITBUCKET_TAG
                  description: tag to publish docs for, e.g. v3.1.0
            -   step:
                    name: create mkdocs
                    image: python:3.8
                    caches:
                        - pip
                    script:
                        - apt-get update && apt-get install zip && apt-get install -y curl wput jq  && apt-get install zip
                        - pip install -r devtools/docsValidation/python-dependencies.txt
                        - mike deploy $BITBUCKET_TAG
                        - mv $BITBUCKET_CLONE_DIR/site $BITBUCKET_CLONE_DIR/$BITBUCKET_TAG
                        -   pipe: atlassian/ftp-deploy:0.7.0
                            variables:
                                USER: $MKDOCS_FTP_NAME
                                PASSWORD: $MKDOCS_FTP_PW
                                SERVER: $MKDOCS_FTP_SERVER
                                REMOTE_PATH: '/doc/'
                                LOCAL_PATH: $BITBUCKET_CLONE_DIR/$BITBUCKET_TAG
                                DEBUG: 'true'
                        # TODO: enable, if version 3 shall be Latest
                        # - mv $BITBUCKET_CLONE_DIR/$BITBUCKET_TAG $BITBUCKET_CLONE_DIR/Latest
                        # -   pipe: atlassian/ftp-deploy:0.7.0
                        #     variables:
                        #         USER: $MKDOCS_FTP_NAME
                        #         PASSWORD: $MKDOCS_FTP_PW
                        #         SERVER: $MKDOCS_FTP_SERVER
                        #         REMOTE_PATH: '/doc/'
                        #         LOCAL_PATH: '$BITBUCKET_CLONE_DIR/Latest'
                        #         DEBUG: 'true'
        validate mkdocs:
            -   step:
                    name: validate mkdocs
                    image: python:3.8
                    caches:
                        - pip
                    script:
                        - python -m venv .venv
                        - source .venv/bin/activate
                        - pip install -r devtools/docsValidation/python-dependencies.txt
                        - devtools/docsValidation/docsValidation.sh
