summaryrefslogtreecommitdiff
path: root/.github/workflows/update-otp-patches.yaml
blob: 0a20080e88f80ba73496fb033c5e7854c07dbb51 (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
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
name: Update OTP Patch Versions for Bazel Based Workflows
on:
  schedule:
  - cron: '0 3 * * *'
  workflow_dispatch:
jobs:
  update-toolchains:
    name: Update OTP Versions
    runs-on: ubuntu-20.04
    strategy:
      max-parallel: 1
      fail-fast: false
      matrix:
        erlang_version:
        - "24.3"
        - "25.0"
        - "25.1"
        - "25.2"
        - "25.3"
        include:
        - erlang_version: "24.3"
          name: '24'
          branch: v3.10.x
          labels: |
            backport-v3.9.x
        - erlang_version: "25.0"
          name: '25_0'
          branch: main
          labels: |
            backport-v3.11.x
        - erlang_version: "25.1"
          name: '25_1'
          branch: main
          labels: |
            backport-v3.11.x
            backport-v3.10.x
        - erlang_version: "25.2"
          name: '25_2'
          branch: main
          labels: |
            backport-v3.11.x
            backport-v3.10.x
        - erlang_version: "25.3"
          name: '25_3'
          branch: main
          labels: |
            backport-v3.12.x
            backport-v3.11.x
            backport-v3.10.x
    timeout-minutes: 10
    env:
      branch: bump-otp-${{ matrix.erlang_version }}
    steps:
    - name: CHECKOUT REPOSITORY
      uses: actions/checkout@v3
      with:
        ref: ${{ matrix.branch }}
    - name: FAIL IF THE PR ALREADY EXISTS
      id: check-for-branch
      run: |
        set +e
        if git ls-remote --exit-code --heads origin ${{ env.branch }}; then
          echo "Branch ${{ env.branch }} already exits"
          exit 1
        fi
    - name: DETERMINE LATEST PATCH & SHA
      id: fetch-version
      run: |
        TAG_NAME=$(curl -s GET https://api.github.com/repos/erlang/otp/tags?per_page=100 \
          | jq -r 'map(select(.name | contains("OTP-${{ matrix.erlang_version }}"))) | first | .name')
        VERSION=${TAG_NAME#OTP-}

        if [[ -z "${VERSION}" ]]; then
          echo "Failed to determine latest VERSION for OTP-${{ matrix.erlang_version }}"
          exit 1
        fi

        ARCHIVE_RBE_URL="https://github.com/erlang/otp/releases/download/${TAG_NAME}/otp_src_${VERSION}.tar.gz"
        wget --continue --quiet --output-document="/tmp/otp_src_${VERSION}.tar.gz" "${ARCHIVE_RBE_URL}"
        SHA="$(shasum -a 256 "/tmp/otp_src_${VERSION}.tar.gz" | awk '{print $1}')"

        if [[ -z "${SHA}" ]]; then
          echo "Failed to determine SHA for ${TAG_NAME}"
          exit 1
        fi

        ARCHIVE_OCI_URL="https://github.com/erlang/otp/archive/OTP-${VERSION}.tar.gz"
        wget --continue --quiet --output-document="/tmp/OTP-${VERSION}.tar.gz" "${ARCHIVE_OCI_URL}"
        SHA2="$(shasum -a 256 "/tmp/OTP-${VERSION}.tar.gz" | awk '{print $1}')"

        if [[ -z "${SHA2}" ]]; then
          echo "Failed to determine SHA2 for ${TAG_NAME}"
          exit 1
        fi

        echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
        echo "SHA=${SHA}" >> $GITHUB_OUTPUT
        echo "SHA2=${SHA2}" >> $GITHUB_OUTPUT
    - name: MODIFY VERSION FILE
      run: |
        sudo npm install --global --silent @bazel/buildozer

        OLD_SHA="$(cat MODULE.bazel | buildozer 'print sha256' -:${{ matrix.name }})"
        OLD_VERSION="$(cat MODULE.bazel | buildozer 'print version' -:${{ matrix.name }})"

        echo "$(cat MODULE.bazel | buildozer 'set sha256 "${{ steps.fetch-version.outputs.SHA }}"' -:${{ matrix.name }})" > MODULE.bazel
        echo "$(cat MODULE.bazel | buildozer 'set version "${{ steps.fetch-version.outputs.VERSION }}"' -:${{ matrix.name }})" > MODULE.bazel

        echo "$(cat WORKSPACE | buildozer 'set downloaded_file_path "OTP-${{ steps.fetch-version.outputs.VERSION }}.tar.gz"' -:otp_src_${{ matrix.name }})" > WORKSPACE
        echo "$(cat WORKSPACE | buildozer 'set urls ["https://github.com/erlang/otp/archive/OTP-${{ steps.fetch-version.outputs.VERSION }}.tar.gz"]' -:otp_src_${{ matrix.name }})" > WORKSPACE
        echo "$(cat WORKSPACE | buildozer 'set sha256 "${{ steps.fetch-version.outputs.SHA2 }}"' -:otp_src_${{ matrix.name }})" > WORKSPACE

        cat << EOF > replacer.mjs
        import { readFile, writeFile } from 'node:fs/promises';
        let [_node, _script, s, p, r] = process.argv;
        let source = await readFile(s, 'utf-8');
        let pattern = await readFile(p, 'utf-8');
        let replacement = await readFile(r, 'utf-8');
        if (!source.includes(pattern)) process.exit(1);
        let updated = source.replace(pattern, replacement);
        await writeFile(s, updated);
        EOF

        cat << EOF > old.txt
                internal_erlang_from_github_release(
                    name = "${{ matrix.name }}",
                    sha256 = "${OLD_SHA}",
                    version = "${OLD_VERSION}",
                ),
        EOF

        cat << EOF > new.txt
                internal_erlang_from_github_release(
                    name = "${{ matrix.name }}",
                    sha256 = "${{ steps.fetch-version.outputs.SHA }}",
                    version = "${{ steps.fetch-version.outputs.VERSION }}",
                ),
        EOF

        node replacer.mjs WORKSPACE old.txt new.txt
        rm replacer.mjs old.txt new.txt

        set -x
        git diff
    - name: CREATE PULL REQUEST
      uses: peter-evans/create-pull-request@v5.0.0
      with:
        token: ${{ secrets.REPO_SCOPED_TOKEN }}
        committer: GitHub <noreply@github.com>
        author: GitHub <noreply@github.com>
        title: Adopt otp ${{ steps.fetch-version.outputs.VERSION }}
        body: >
           Automated changes created by
           ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
           using the [create-pull-request](https://github.com/peter-evans/create-pull-request)
           GitHub action in the ${{ github.workflow }} workflow.
        commit-message: |
          Adopt otp ${{ steps.fetch-version.outputs.VERSION }}
        labels: ${{ matrix.labels }}
        branch: ${{ env.branch }}
        delete-branch: true