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

jobs:
- job: test
  displayName: 'Lint'

  pool:
    vmImage: 'Ubuntu-16.04'

  steps:
  - task: UsePythonVersion@0
    displayName: 'Set Python version'
    inputs:
      versionSpec: '3.7.x'
      addToPath: true

  - script: pip3 install pipenv
    displayName: 'Install pipenv'

  - script: pipenv install --dev --deploy --system
    displayName: 'Install project using pipenv'

  - script: python3 -m flake8 --format junit-xml --output-file test-lint.xml
    displayName: 'Run linter'

  - task: PublishTestResults@2
    condition: succeededOrFailed()
    inputs:
      testResultsFiles: '**/test-*.xml'
      testRunTitle: 'Snekbox Flake8 Lint Results'

- job: build
  displayName: 'Build'
  dependsOn: test

  variables:
    BASE_CHANGED: true
    VENV_CHANGED: true
    PIPFILE_CHANGED: true

  steps:
  - task: Docker@1
    displayName: 'Login: Docker Hub'

    inputs:
      containerregistrytype: 'Container Registry'
      dockerRegistryEndpoint: 'DockerHub'
      command: 'login'

  - script: |
      REQUEST_URL="https://dev.azure.com/python-discord/${SYSTEM_TEAMPROJECTID}/_apis/build/builds?queryOrder=finishTimeDescending&resultFilter=succeeded&\$top=1&repositoryType=${BUILD_REPOSITORY_PROVIDER}&repositoryId=${BUILD_REPOSITORY_NAME}&branchName=${BUILD_SOURCEBRANCH}&api-version=5.0"
      echo "Retrieving previous build's commit using $REQUEST_URL"
      RESPONSE="$(curl -sSL "${REQUEST_URL}")"

      if [[ $BUILD_REASON = "PullRequest" ]]; then
        PREV_COMMIT="$(echo "${RESPONSE}" | grep -Po '"pr\.sourceSha"\s*:\s*"\K.*?[^\\](?="\s*[,}])')"
        if [[ -z $PREV_COMMIT ]]; then
          echo "Could not retrieve the previous build's commit. Falling back to the head of the target branch."
          PREV_COMMIT="origin/$SYSTEM_PULLREQUEST_TARGETBRANCH"
        fi
      else
        PREV_COMMIT="$(echo "${RESPONSE}" | grep -Po '"sourceVersion"\s*:\s*"\K.*?[^\\](?="\s*[,}])')"
      fi

      if [[ -n $PREV_COMMIT ]]; then
        echo "Using $PREV_COMMIT to compare diffs."

        if [[ -z "$(docker images -q pythondiscord/snekbox-base:latest)" ]]; then
          echo "The base image does not exist so it will be built."
        elif [[ -z "$(git diff $PREV_COMMIT -- docker/base.Dockerfile)" ]]; then
          echo "No changes detected in docker/base.Dockerfile. The base image will not be built."
          echo "##vso[task.setvariable variable=BASE_CHANGED]false"
        fi

        if [[ -z "$(docker images -q pythondiscord/snekbox-venv:latest)" ]]; then
            echo "The venv image does not exist so it will be built."
        else
          if [[ -z "$(git diff $PREV_COMMIT -- docker/venv.Dockerfile)" ]]; then
            echo "No changes detected in docker/venv.Dockerfile. The venv image will not be built."
            echo "##vso[task.setvariable variable=VENV_CHANGED]false"
          fi

          if [[ -z "$(git diff $PREV_COMMIT -- Pipfile*)" ]]; then
            echo "No changes detected in Pipfile or Pipfile.lock. The venv image will not be built, unless venv.Dockerfile changed or the image doesn't exist."
            echo "##vso[task.setvariable variable=PIPFILE_CHANGED]false"
          fi
        fi
      else
        echo "No previous commit was retrieved. Either the previous build is too old and was deleted or the branch was empty before this build. All images will be built."
      fi
    displayName: 'Check Changed Files'

  - script: docker build -t pythondiscord/snekbox-base:latest -f docker/base.Dockerfile .
    displayName: 'Build Base Image'
    condition: and(succeeded(), eq(variables.BASE_CHANGED, 'true'))

  - script: docker build -t pythondiscord/snekbox-venv:latest -f docker/venv.Dockerfile .
    displayName: 'Build Virtual Environment Image'
    condition: and(succeeded(), or(eq(variables.BASE_CHANGED, 'true'), eq(variables.VENV_CHANGED, 'true'), and(eq(variables.PIPFILE_CHANGED, 'true'), ne(variables['Build.Reason'], 'PullRequest'))))

  - script: docker build -t pythondiscord/snekbox:latest -f docker/Dockerfile .
    displayName: 'Build Final Image'

  - script: docker push pythondiscord/snekbox-base:latest
    displayName: 'Push Base Image to Dockerhub'
    condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'), eq(variables.BASE_CHANGED, 'true'))

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

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