summaryrefslogtreecommitdiff
path: root/.github/workflows/preview_build.yml
blob: 9aea890bcecb14cca2f9c623e44a5eaf85b4fc69 (plain)
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
name: Preview build

on:
  repository_dispatch:
    types:
    - Preview*

jobs:
  preview:
    name: Build preview

    runs-on: ubuntu-20.04
    container:
      # If you change this version, change the number in the cache step too.
      image: emscripten/emsdk:2.0.10
      # uid=1001(runner) gid=121(docker)
      options: -u 1001:121

    steps:
    - name: Update deployment status to in progress
      uses: octokit/request-action@v2.x
      with:
        route: POST /repos/{owner}/{repo}/deployments/{deployment_id}/statuses
        mediaType: |
          previews:
          - ant-man
          - flash
        owner: ${{ github.event.repository.owner.login }}
        repo: ${{ github.event.repository.name }}
        deployment_id: ${{ github.event.client_payload.deployment_id }}
        state: in_progress
      env:
        GITHUB_TOKEN: ${{ secrets.PREVIEW_GITHUB_TOKEN }}

    - name: Checkout
      uses: actions/checkout@v2
      with:
        ref: ${{ github.event.client_payload.sha }}

    - name: Name branch
      run: |
        name=$(echo "${{ github.event.client_payload.folder }}")
        git checkout -b ${name}

    - name: Setup cache
      uses: actions/cache@v2
      with:
        path: /emsdk/upstream/emscripten/cache
        key: 2.0.10-${{ runner.os }}

    - name: Build (host tools)
      run: |
        mkdir build-host
        cd build-host

        echo "::group::CMake"
        cmake .. -DOPTION_TOOLS_ONLY=ON
        echo "::endgroup::"

        echo "::group::Build"
        echo "Running on $(nproc) cores"
        make -j$(nproc) tools
        echo "::endgroup::"

    - name: Install GCC problem matcher
      uses: ammaraskar/gcc-problem-matcher@master

    - name: Build
      run: |
        mkdir build
        cd build

        echo "::group::CMake"
        emcmake cmake .. \
          -DHOST_BINARY_DIR=../build-host \
          -DCMAKE_BUILD_TYPE=RelWithDebInfo \
          # EOF
        echo "::endgroup::"

        echo "::group::Build"
        echo "Running on $(nproc) cores"
        emmake make -j$(nproc)
        echo "::endgroup::"

    - name: Publish preview
      run: |
        # setuptools is missing in this Docker image, which breaks installing
        # awscli. So we need to do this in two steps to recover sanity.
        pip3 install setuptools
        pip3 install awscli

        ~/.local/bin/aws s3 cp --only-show-errors build/openttd.data s3://${{ secrets.PREVIEW_S3_BUCKET }}/${{ github.event.client_payload.folder }}/
        ~/.local/bin/aws s3 cp --only-show-errors build/openttd.html s3://${{ secrets.PREVIEW_S3_BUCKET }}/${{ github.event.client_payload.folder }}/
        ~/.local/bin/aws s3 cp --only-show-errors build/openttd.js s3://${{ secrets.PREVIEW_S3_BUCKET }}/${{ github.event.client_payload.folder }}/
        ~/.local/bin/aws s3 cp --only-show-errors build/openttd.wasm s3://${{ secrets.PREVIEW_S3_BUCKET }}/${{ github.event.client_payload.folder }}/

        # Invalidate the cache of the CloudFront distribution
        ~/.local/bin/aws cloudfront create-invalidation --distribution-id ${{ secrets.PREVIEW_CF_DISTRIBUTION_ID }} --paths "/${{ github.event.client_payload.folder }}/*"
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

    - name: Update deployment status to success
      uses: octokit/request-action@v2.x
      with:
        route: POST /repos/{owner}/{repo}/deployments/{deployment_id}/statuses
        mediaType: |
          previews:
          - ant-man
          - flash
        owner: ${{ github.event.repository.owner.login }}
        repo: ${{ github.event.repository.name }}
        deployment_id: ${{ github.event.client_payload.deployment_id }}
        state: success
        environment_url: https://preview.openttd.org/${{ github.event.client_payload.folder }}/
      env:
        GITHUB_TOKEN: ${{ secrets.PREVIEW_GITHUB_TOKEN }}

    - if: failure()
      name: Update deployment status to failure
      uses: octokit/request-action@v2.x
      with:
        route: POST /repos/{owner}/{repo}/deployments/{deployment_id}/statuses
        mediaType: |
          previews:
          - ant-man
          - flash
        owner: ${{ github.event.repository.owner.login }}
        repo: ${{ github.event.repository.name }}
        deployment_id: ${{ github.event.client_payload.deployment_id }}
        state: failure
      env:
        GITHUB_TOKEN: ${{ secrets.PREVIEW_GITHUB_TOKEN }}