aboutsummaryrefslogtreecommitdiffstats
path: root/azure-pipelines.yml
blob: 9b0c6482ec40ffd807fbae7bb9ce3f20fa1ee53f (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
# https://aka.ms/yaml

jobs:
  - job: test
    displayName: 'Lint & Test'

    pool:
      vmImage: 'ubuntu-18.04'

    steps:
      - template: ci/build.yml
      - template: ci/setup.yml
      - template: ci/lint-test.yml

  # When a pull request, only perform this job if images need to be built.
  # It's always performed for non-PRs because the final image will always need
  # to be built.
  - job: build
    displayName: 'Build'
    condition: >
      and(
        succeeded(),
        or(
          ne(variables['Build.Reason'], 'PullRequest'),
          eq(coalesce(dependencies.test.outputs['check.BASE_CHANGED'], 'True'), 'True'),
          eq(coalesce(dependencies.test.outputs['check.VENV_CHANGED'], 'True'), 'True')
        )
      )
    dependsOn: test

    # coalesce() gives variables default values if they are null (i.e. unset).
    variables:
      BASE_CHANGED: $[ coalesce(dependencies.test.outputs['check.BASE_CHANGED'], 'True') ]
      VENV_CHANGED: $[ coalesce(dependencies.test.outputs['check.VENV_CHANGED'], 'True') ]
      BASE_PULL: $[ coalesce(dependencies.test.outputs['check.BASE_PULL'], 'False') ]

    steps:
      - task: Docker@1
        displayName: 'Log into Docker Hub'
        inputs:
          command: login
          containerregistrytype: 'Container Registry'
          dockerRegistryEndpoint: 'DockerHub'

      # Because this is the base image for the venv image, if the venv needs to
      # be built, this base image must also be present. Build it if it has
      # changed or can't be pulled from Docker Hub.
      - script: |
          docker build \
            -f docker/base.Dockerfile \
            -t pythondiscord/snekbox-base:latest \
            .
        displayName: 'Build Base Image'
        condition: >
          and(
            succeeded(),
            ne(variables.BASE_PULL, 'True'),
            or(
              eq(variables.BASE_CHANGED, 'True'),
              eq(variables.VENV_CHANGED, 'True')
            )
          )

      # Also build this image if base has changed - even if this image hasn't.
      - script: |
          docker build \
            -f docker/venv.Dockerfile \
            -t pythondiscord/snekbox-venv:latest \
            .
        displayName: 'Build Virtual Environment Image'
        condition: >
          and(
            succeeded(),
            or(
              eq(variables.BASE_CHANGED, 'True'),
              eq(variables.VENV_CHANGED, 'True')
            )
          )

      # Always build this image unless it's for a pull request.
      - script: |
          docker build \
            -f docker/Dockerfile \
            -t pythondiscord/snekbox:latest \
            .
        displayName: 'Build Final Image'
        condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))

      # Push images only after they've all successfully been built.
      # These have the same conditions as the build tasks. However, for safety,
      # a condition for not being a pull request is added.
      - script: docker push pythondiscord/snekbox-base:latest
        displayName: 'Push Base Image'
        condition: >
          and(
            succeeded(),
            ne(variables['Build.Reason'], 'PullRequest'),
            ne(variables.BASE_PULL, 'True'),
            or(
              eq(variables.BASE_CHANGED, 'True'),
              eq(variables.VENV_CHANGED, 'True')
            )
          )

      - script: docker push pythondiscord/snekbox-venv:latest
        displayName: 'Push Virtual Environment Image'
        condition: >
          and(
            succeeded(),
            ne(variables['Build.Reason'], 'PullRequest'),
            or(
              eq(variables.BASE_CHANGED, 'True'),
              eq(variables.VENV_CHANGED, 'True')
            )
          )

      - script: docker push pythondiscord/snekbox:latest
        displayName: 'Push Final Image'
        condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))