aboutsummaryrefslogtreecommitdiffstats
path: root/.gitlab-ci.yml
blob: cdea2ca17ba227dadc7525cba6243f81d58376e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
stages:
    - build
    - lint
    - test
    - publish
    - deploy
variables:
  BASE_IMAGE_URL: registry.gitlab.com/python-discord/projects/site/django-ci


.test-template: &test-template
    stage: test
    services:
        - postgres:11-alpine
    script:
        - python manage.py test
    tags:
        - docker
    variables:
        DATABASE_URL: postgres://django:supersecret@postgres/pysite
        POSTGRES_DB: pysite
        POSTGRES_PASSWORD: supersecret
        POSTGRES_USER: django

build-alpine-3.7:
    stage: build
    image: docker:dind
    before_script:
        - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    script:
        - >
            docker build
            --build-arg EXTRAS=test,lint
            -t $BASE_IMAGE_URL:alpine-3.7-$CI_COMMIT_REF_SLUG
            -f docker/app/alpine/3.7/Dockerfile
            .
        - docker push $BASE_IMAGE_URL:alpine-3.7-$CI_COMMIT_REF_SLUG

build-alpine-3.6:
    stage: build
    image: docker:dind
    before_script:
        - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    script:
        - >
            docker build
            --build-arg EXTRAS=test,lint
            -t $BASE_IMAGE_URL:alpine-3.6-$CI_COMMIT_REF_SLUG
            -f docker/app/alpine/3.6/Dockerfile
            .
        - docker push $BASE_IMAGE_URL:alpine-3.6-$CI_COMMIT_REF_SLUG

build-stretch-3.7:
    stage: build
    image: docker:dind
    before_script:
        - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    script:
        - >
            docker build
            --build-arg EXTRAS=test,lint
            -t $BASE_IMAGE_URL:stretch-3.7-$CI_COMMIT_REF_SLUG
            -f docker/app/stretch/3.7/Dockerfile
            .
        - docker push $BASE_IMAGE_URL:stretch-3.7-$CI_COMMIT_REF_SLUg

build-stretch-3.6:
    stage: build
    image: docker:dind
    before_script:
        - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    script:
        - >
            docker build
            --build-arg EXTRAS=test,lint
            -t $BASE_IMAGE_URL:stretch-3.6-$CI_COMMIT_REF_SLUG
            -f docker/app/stretch/3.6/Dockerfile
            .
        - docker push $BASE_IMAGE_URL:stretch-3.6-$CI_COMMIT_REF_SLUg

lint:
    stage: lint
    dependencies:
        - build-alpine-3.7
    image: docker:dind
    before_script:
        - docker load < django_alpine_py37.tar.gz
    script:
        - docker run django:alpine-3.7 flake8
    tags:
        - docker

test-3.7-alpine:
    <<: *test-template
    image: docker:dind
    dependencies:
        - build-alpine-3.7
    before_script:
        - docker load < djano_alpine_py37.tar.gz
        - docker run --env SECRET_KEY=no --add-host postgres:$(dig +short postgres) --env DATABASE_URL=$DATABASE_URL django:alpine-3.7 python manage.py migrate
    script:
        - docker run --env SECRET_KEY=no --add-host postgres:$(dig +short postgres) --env DATABASE_URL=$DATABASE_URL -v ./:/app django:alpine-3.7 coverage run --source=api,home,pysite,wiki --branch manage.py test
    after_script:
        - docker run -v ./:/app coverage report
    artifacts:
        paths:
            - .coverage

test-3.6-alpine:
    <<: *test-template
    image: docker:dind
    dependencies:
        - build-alpine-3.6
    before_script:
        - docker load < django_alpine_py36.tar.gz
        - docker run --env SECRET_KEY=no --env DATABASE_URL=$DATABASE_URL django:alpine-3.6 python manage.py migrate
    script:
        - docker run --env SECRET_KEY=no --env DATABASE_URL=$DATABASE_URL django:alpine-3.6 python manage.py test

test-3.7-stretch:
    <<: *test-template
    image: docker:dind
    dependencies:
        - build-stretch-3.7
    before_script:
        - docker load < django_stretch_py37.tar.gz
        - docker run --env SECRET_KEY=no --env DATABASE_URL=$DATABASE_URL django:stretch-3.7 python manage.py migrate
    script:
        - docker run --env SECRET_KEY=no --env DATABASE_URL=$DATABASE_URL django:stretch-3.7 python manage.py test

test-3.6-stretch:
    <<: *test-template
    image: docker:dind
    dependencies:
        - build-stretch-3.6
    before_script:
        - docker load < django_stretch_py36.tar.gz
        - docker run --env SECRET_KEY=no --env DATABASE_URL=$DATABASE_URL django:stretch-3.6 python manage.py migrate
    script:
        - docker run --env SECRET_KEY=no --env DATABASE_URL=$DATABASE_URL django:stretch-3.6 python manage.py test

pages:
    stage: publish
    dependencies:
        - test-3.7-alpine
    before_script:
        - pip install coverage
    script:
        - coverage html --directory=public
    artifacts:
        paths:
            - public
        expire_in: 30 days

push-django:
    image: docker:stable-git
    stage: publish
    script:
        - echo "$DOCKER_PASSWORD" | docker login --username "$DOCKER_USERNAME" --password-stdin
        - docker build -t pythondiscord/django:latest .
        - docker push pythondiscord/django:latest
    only:
        - master
        - django
    tags:
        - docker

deploy:
    stage: deploy
    image: alpine:latest
    before_script:
        - apk add --no-cache openssh-client
        - echo "$DJANGO_DEPLOY_SSH_PRIVATE_KEY" > id_ed25519
        - chmod 400 id_ed25519
    script:
        - ssh -i id_ed25519 -p 583 -o "StrictHostKeyChecking=no" [email protected]
    environment:
        name: Django staging
        url: https://pysite.jchri.st
    tags:
        - docker
    only:
        - master
        - django