.gitlab-ci.yml 7.33 KB
Newer Older
Axel Dürkop's avatar
Axel Dürkop committed
1
image: ruby:2.6
Andreas Böttger's avatar
Andreas Böttger committed
2

3 4 5
variables:
    JEKYLL_ENV: production
  
Andreas Böttger's avatar
Andreas Böttger committed
6
stages:
7 8
    - setup
    - test
Andreas Böttger's avatar
Andreas Böttger committed
9 10
    - review
    - deploy
11 12 13 14 15 16
    - clean

before_script:
    # https://docs.gitlab.com/ee/ci/ssh_keys/README.html
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
Andreas Böttger's avatar
Andreas Böttger committed
17
    
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    - export LC_ALL="C.UTF-8"; export LANG="en_US.UTF-8"; export LANGUAGE="en_US.UTF-8"
    - export BUNDLE_PATH="vendor/bundle"

prepare:
    stage: setup
    cache:
        key: gems
        paths:
            - vendor/bundle
    script:
        - bundle install

test:
    stage: test
    script:
        - 'perl -i -pe ''s!.*baseurl:\s.*!baseurl: "/$ENV{''CI_PROJECT_PATH''}/~$ENV{''CI_COMMIT_REF_NAME''}/"\n!g'' _config.yml'
        - cat _config.yml
        - bundle exec jekyll build -d test
    cache:
        key: gems
        paths:
            - vendor/bundle
        policy: pull
    except:
        - master

Andreas Böttger's avatar
Andreas Böttger committed
44 45 46
review:start:
    stage: review
    script:
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
        - echo "$REVIEW_SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
        - chmod 644 ~/.ssh/known_hosts
        - 'perl -i -pe ''s!.*baseurl:\s.*!baseurl: "/$ENV{''CI_PROJECT_PATH''}/~$ENV{''CI_COMMIT_REF_NAME''}/"\n!g'' _config.yml'
        - cat _config.yml
        - bundle install
        - bundle exec jekyll build -d public
        - eval $(ssh-agent -s)
        - echo "$REVIEW_SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
        - ssh -p "${REVIEW_SSH_PORT}" "${REVIEW_SSH_USER}"@"${REVIEW_HOST_NAME}" "mkdir -p /usr/share/nginx/html/~${CI_COMMIT_REF_NAME}"
        - scp -P "${REVIEW_SSH_PORT}" -r public/* "${REVIEW_SSH_USER}"@"${REVIEW_HOST_NAME}":"/usr/share/nginx/html/~${CI_COMMIT_REF_NAME}"
    cache:
        key: gems
        paths:
            - vendor/bundle
        policy: pull
    artifacts:
        paths:
            - public
Andreas Böttger's avatar
Andreas Böttger committed
65 66
    except:
        - master
67 68 69 70
    environment:
        name: $CI_COMMIT_REF_NAME
        url: https://$REVIEW_PROXY_HOST_NAME/${CI_PROJECT_PATH}/~${CI_COMMIT_REF_NAME}/
        on_stop: review:stop
Andreas Böttger's avatar
Andreas Böttger committed
71

72
  
Andreas Böttger's avatar
Andreas Böttger committed
73 74 75
review:stop:
    stage: review
    script:
76 77 78 79 80
        - echo "$REVIEW_SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
        - chmod 644 ~/.ssh/known_hosts
        - eval $(ssh-agent -s)
        - echo "$REVIEW_SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
        - ssh -p "${REVIEW_SSH_PORT}" "${REVIEW_SSH_USER}"@"${REVIEW_HOST_NAME}" "rm -rf /usr/share/nginx/html/~${CI_COMMIT_REF_NAME}"
Andreas Böttger's avatar
Andreas Böttger committed
81
    when: manual
82 83
    except:
        - master
Andreas Böttger's avatar
Andreas Böttger committed
84
    environment:
85
        name: $CI_COMMIT_REF_NAME
Andreas Böttger's avatar
Andreas Böttger committed
86
        action: stop
87 88 89

deploy:
    stage: deploy
Andreas Böttger's avatar
Andreas Böttger committed
90
    only:
91
        - master
Andreas Böttger's avatar
Andreas Böttger committed
92
    except:
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
        - schedules
    script:
        - echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
        - chmod 644 ~/.ssh/known_hosts
        #- 'perl -i -pe ''s!.*baseurl:\s.*!baseurl: "/$ENV{''CI_PROJECT_PATH''}"\n!g'' _config.yml'
        #- cat _config.yml
        - find backlog -regextype egrep -regex "backlog/[0-9]{8,8}_.*" | sort | awk -F'/|_' -v date=$(date +%Y%m%d) '{if ($2 <= date) {print("cp backlog/"$2"_home.html _layouts/home.html"); system("cp backlog/"$2"_home.html _layouts/home.html")}}'
        - bundle install
        - bundle exec jekyll build -d public
        - eval $(ssh-agent -s)
        - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
        - ssh -p "${SSH_PORT}" "${SSH_USER}"@"${HOST_NAME}" "mkdir -p /usr/share/nginx/html"
        - scp -P "${SSH_PORT}" -r public/* "${SSH_USER}"@"${HOST_NAME}":"/usr/share/nginx/html"
    cache:
        key: gems
        paths:
            - vendor/bundle
        policy: pull        
    environment:
        name: _$CI_COMMIT_REF_NAME
        #url: http://$HOST_NAME$COLON$HTTP_PORT/
        #url: https://$PROXY_HOST_NAME/$BASE_PATH/
Andreas Böttger's avatar
Andreas Böttger committed
115 116
        #url: https://$PROXY_HOST_NAME/$CI_PROJECT_PATH/
        url: https://$PROXY_HOST_NAME/
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

deploy:on-schedule:
    stage: deploy
    only:
        - schedules
    script:
        - echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
        - chmod 644 ~/.ssh/known_hosts
        #- 'perl -i -pe ''s!.*baseurl:\s.*!baseurl: "/$ENV{''CI_PROJECT_PATH''}"\n!g'' _config.yml'
        #- cat _config.yml
        - find backlog -regextype egrep -regex "backlog/[0-9]{8,8}_.*" | sort | awk -F'/|_' -v date=$(date +%Y%m%d) '{if ($2 <= date) {print("cp backlog/"$2"_home.html _layouts/home.html"); system("cp backlog/"$2"_home.html _layouts/home.html")}}'
        - bundle install
        - bundle exec jekyll build -d public
        - eval $(ssh-agent -s)
        - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
        - ssh -p "${SSH_PORT}" "${SSH_USER}"@"${HOST_NAME}" "mkdir -p /usr/share/nginx/html"
        - scp -P "${SSH_PORT}" -r public/* "${SSH_USER}"@"${HOST_NAME}":"/usr/share/nginx/html"
    cache:
        key: gems
        paths:
            - vendor/bundle
        policy: pull        
    environment:
        name: _$CI_COMMIT_REF_NAME
        #url: http://$HOST_NAME$COLON$HTTP_PORT/
        #url: https://$PROXY_HOST_NAME/$BASE_PATH/
Andreas Böttger's avatar
Andreas Böttger committed
143 144 145
        #url: https://$PROXY_HOST_NAME/$CI_PROJECT_PATH/
        url: https://$PROXY_HOST_NAME/

146 147 148 149 150 151 152 153 154 155
clean:
    stage: clean
    script:
        - echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
        - chmod 644 ~/.ssh/known_hosts
        - eval $(ssh-agent -s)
        - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
        - ssh -p "${SSH_PORT}" "${SSH_USER}"@"${HOST_NAME}" "rm -rf /usr/share/nginx/html/*"
    when: manual
    only:
Andreas Böttger's avatar
Andreas Böttger committed
156
        - master
157 158 159
    environment:
        name: $CI_COMMIT_REF_NAME
        action: stop
Andreas Böttger's avatar
Andreas Böttger committed
160

161 162

.deploy:
Andreas Böttger's avatar
Andreas Böttger committed
163 164
    stage: deploy
    only:
Andreas Böttger's avatar
Andreas Böttger committed
165
        - master
Andreas Böttger's avatar
Andreas Böttger committed
166
    except:
Andreas Böttger's avatar
Andreas Böttger committed
167
        - schedules
Andreas Böttger's avatar
Andreas Böttger committed
168
    tags:
Andreas Böttger's avatar
Andreas Böttger committed
169
        - ssg
Andreas Böttger's avatar
Andreas Böttger committed
170 171 172 173
    script:
        - pwd
        # `_layouts/home.html` kann datumsabhängig mit einer Datei aus `backlogs` überschrieben werden
        - find backlog -regextype egrep -regex "backlog/[0-9]{8,8}_.*" | sort | awk -F'/|_' -v date=$(date +%Y%m%d) '{if ($2 <= date) {print("cp backlog/"$2"_home.html _layouts/home.html"); system("cp backlog/"$2"_home.html _layouts/home.html")}}'
Andreas Böttger's avatar
Andreas Böttger committed
174 175
        - sudo docker run --rm -v $(pwd):/source ${BUILD_IMAGE} install
        - sudo docker run --rm -v $(pwd):/source ${BUILD_IMAGE} build
Andreas Böttger's avatar
Andreas Böttger committed
176
        # Da `StrictHostKeyChecking no` verwendet wird, ist das Folgende ok :\
Andreas Böttger's avatar
Andreas Böttger committed
177
        - ssh-keygen -f "$HOME/.ssh/known_hosts" -R [@$HOST_NAME]:$SSH_PORT
Andreas Böttger's avatar
Andreas Böttger committed
178 179 180 181 182 183
        - sshpass -p "$SSH_PASSWD" ssh -p "$SSH_PORT" $SSH_USER@$HOST_NAME "mkdir -p /usr/share/nginx/html/production"
        - sshpass -p "$SSH_PASSWD" scp -P "$SSH_PORT" -r _site/* $SSH_USER@$HOST_NAME:/usr/share/nginx/html/production
    environment:
        name: production
        url: https://$PROXY_HOST_NAME/
        
184
.deploy:on-schedule:
Andreas Böttger's avatar
Andreas Böttger committed
185 186 187 188
    stage: deploy
    only:
        - schedules
    tags:
Andreas Böttger's avatar
Andreas Böttger committed
189
        - ssg
Andreas Böttger's avatar
Andreas Böttger committed
190
    script:
Andreas Böttger's avatar
Andreas Böttger committed
191
        - pwd
Andreas Böttger's avatar
Andreas Böttger committed
192
        # `_layouts/home.html` kann datumsabhängig mit einer Datei aus `backlogs` überschrieben werden
Andreas Böttger's avatar
Andreas Böttger committed
193
        - find backlog -regextype egrep -regex "backlog/[0-9]{8,8}_.*" | sort | awk -F'/|_' -v date=$(date +%Y%m%d) '{if ($2 <= date) {print("cp backlog/"$2"_home.html _layouts/home.html"); system("cp backlog/"$2"_home.html _layouts/home.html")}}'
Andreas Böttger's avatar
Andreas Böttger committed
194 195
        - sudo docker run --rm -v $(pwd):/source ${BUILD_IMAGE} install
        - sudo docker run --rm -v $(pwd):/source ${BUILD_IMAGE} build
Andreas Böttger's avatar
Andreas Böttger committed
196 197 198 199
        - sshpass -p "$SSH_PASSWD" ssh -p "$SSH_PORT" $SSH_USER@$HOST_NAME "mkdir -p /usr/share/nginx/html/production"
        - sshpass -p "$SSH_PASSWD" scp -P "$SSH_PORT" -r _site/* $SSH_USER@$HOST_NAME:/usr/share/nginx/html/production
    environment:
        name: production
Andreas Böttger's avatar
Andreas Böttger committed
200
        url: https://$PROXY_HOST_NAME/