]> git.lizzy.rs Git - rust.git/blob - src/ci/shared.sh
Merge commit 'cd4810de42c57b64b74dae09c530a4c3a41f87b9' into libgccjit-codegen
[rust.git] / src / ci / shared.sh
1 #!/bin/false
2 # shellcheck shell=bash
3
4 # This file is intended to be sourced with `. shared.sh` or
5 # `source shared.sh`, hence the invalid shebang and not being
6 # marked as an executable file in git.
7
8 export MIRRORS_BASE="https://ci-mirrors.rust-lang.org/rustc"
9
10 # See https://unix.stackexchange.com/questions/82598
11 # Duplicated in docker/dist-various-2/shared.sh
12 function retry {
13   echo "Attempting with retry:" "$@"
14   local n=1
15   local max=5
16   while true; do
17     "$@" && break || {
18       if [[ $n -lt $max ]]; then
19         sleep $n  # don't retry immediately
20         ((n++))
21         echo "Command failed. Attempt $n/$max:"
22       else
23         echo "The command has failed after $n attempts."
24         return 1
25       fi
26     }
27   done
28 }
29
30 function isCI {
31     [[ "${CI-false}" = "true" ]] || isAzurePipelines || isGitHubActions
32 }
33
34 function isAzurePipelines {
35     [[ "${TF_BUILD-False}" = "True" ]]
36 }
37
38 function isGitHubActions {
39     [[ "${GITHUB_ACTIONS-false}" = "true" ]]
40 }
41
42
43 function isSelfHostedGitHubActions {
44     [[ "${RUST_GHA_SELF_HOSTED-false}" = "true" ]]
45 }
46
47 function isMacOS {
48     [[ "${OSTYPE}" = "darwin"* ]]
49 }
50
51 function isWindows {
52     [[ "${OSTYPE}" = "cygwin" ]] || [[ "${OSTYPE}" = "msys" ]]
53 }
54
55 function isLinux {
56     [[ "${OSTYPE}" = "linux-gnu" ]]
57 }
58
59 function isCiBranch {
60     if [[ $# -ne 1 ]]; then
61         echo "usage: $0 <branch-name>"
62         exit 1
63     fi
64     name="$1"
65
66     if isAzurePipelines; then
67         [[ "${BUILD_SOURCEBRANCHNAME}" = "${name}" ]]
68     elif isGitHubActions; then
69         [[ "${GITHUB_REF}" = "refs/heads/${name}" ]]
70     else
71         echo "isCiBranch only works inside CI!"
72         exit 1
73     fi
74 }
75
76 function ciBaseBranch {
77     if isAzurePipelines; then
78         echo "unsupported on Azure Pipelines"
79         exit 1
80     elif isGitHubActions; then
81         echo "${GITHUB_BASE_REF#refs/heads/}"
82     else
83         echo "ciBaseBranch only works inside CI!"
84         exit 1
85     fi
86 }
87
88 function ciCommit {
89     if isAzurePipelines; then
90         echo "${BUILD_SOURCEVERSION}"
91     elif isGitHubActions; then
92         echo "${GITHUB_SHA}"
93     else
94         echo "ciCommit only works inside CI!"
95         exit 1
96     fi
97 }
98
99 function ciCheckoutPath {
100     if isAzurePipelines; then
101         echo "${BUILD_SOURCESDIRECTORY}"
102     elif isGitHubActions; then
103         echo "${GITHUB_WORKSPACE}"
104     else
105         echo "ciCheckoutPath only works inside CI!"
106         exit 1
107     fi
108 }
109
110 function ciCommandAddPath {
111     if [[ $# -ne 1 ]]; then
112         echo "usage: $0 <path>"
113         exit 1
114     fi
115     path="$1"
116
117     if isAzurePipelines; then
118         echo "##vso[task.prependpath]${path}"
119     elif isGitHubActions; then
120         echo "${path}" >> "${GITHUB_PATH}"
121     else
122         echo "ciCommandAddPath only works inside CI!"
123         exit 1
124     fi
125 }
126
127 function ciCommandSetEnv {
128     if [[ $# -ne 2 ]]; then
129         echo "usage: $0 <name> <value>"
130         exit 1
131     fi
132     name="$1"
133     value="$2"
134
135     if isAzurePipelines; then
136         echo "##vso[task.setvariable variable=${name}]${value}"
137     elif isGitHubActions; then
138         echo "${name}=${value}" >> "${GITHUB_ENV}"
139     else
140         echo "ciCommandSetEnv only works inside CI!"
141         exit 1
142     fi
143 }
144
145 function releaseChannel {
146     if [[ -z "${RUST_CI_OVERRIDE_RELEASE_CHANNEL+x}" ]]; then
147         cat "${ci_dir}/channel"
148     else
149         echo $RUST_CI_OVERRIDE_RELEASE_CHANNEL
150     fi
151 }