-
Notifications
You must be signed in to change notification settings - Fork 231
77 lines (67 loc) · 2.98 KB
/
update-dl-url.yml
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
---
# This workflow allows on-call operators to safely switch the value of the `dl`
# key in `config.json` during an outage, to ensure people can continue building
# projects with `cargo`.
#
# The workflow can be invoked through GitHub's UI.
#
# To add new options change `on.workflow_dispatch.inputs.url.options` to
# include your new option, and then add the related environment variable in
# the `env` section.
name: Update download URL
on:
workflow_dispatch:
inputs:
url:
description: Switch downloads to
required: true
type: choice
options:
# The format of this is "${id}:${description}". Each option
# corresponds to the environment variable `URL_${id}`.
- "api: API on Heroku (default)"
- "cdn: CloudFront CDN"
- "s3_primary: S3 bucket in us-west-1"
- "s3_fallback: S3 bucket in eu-west-1"
env:
URL_api: "https://crates.io/api/v1/crates"
URL_cdn: "https://static.crates.io/crates/{crate}/{crate}-{version}.crate"
URL_s3_primary: "https://crates-io.s3-us-west-1.amazonaws.com/crates/{crate}/{crate}-{version}.crate"
URL_s3_fallback: "https://crates-io-fallback.s3-eu-west-1.amazonaws.com/crates/{crate}/{crate}-{version}.crate"
permissions:
contents: write
jobs:
switch:
# The option the user selected is included in the job name to permanently
# log it, as those options are not otherwise logged and including it in the
# build log would result in it being deleted after 90 days.
name: Switch to ${{ github.event.inputs.url }}
runs-on: ubuntu-latest
steps:
- name: Clone the source code
uses: actions/checkout@v2
- name: Switch the download endpoint
run: |
set -euo pipefail
# Dynamically fetch the URL based on the choice the user made. The
# first line calculates the environment variable name by
# concatenating "URL_" and the part of the input before ":". Then the
# second line fetches the value of the variable with that name.
var_name="URL_$(echo "${{ github.event.inputs.url }}" | sed "s/:.*//")"
url="${!var_name}"
# Use `jq` to update the `dl` field of the configuration file while
# preserving the rest of the contents.
jq --arg url "${url}" '.dl = $url' config.json > updated-config.json
# The GitHub API is used instead of `git commit`/`git push` to sign
# the commit with GitHub's key. The sha of the previous `config.json`
# is provided to make sure we don't override concurrent changes.
curl \
--fail \
-X PUT \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-d "{
\"message\": \"Switching download endpoint\",
\"content\": \"$(base64 -w0 updated-config.json)\",
\"sha\": \"$(git hash-object config.json)\"
}" \
https://api.github.com/repos/${{ github.repository }}/contents/config.json