Skip to content

Commit 595aa63

Browse files
pottekkatbzp2010
andauthored
ci: add install script (#158)
Co-authored-by: Zeping Bai <[email protected]>
1 parent e8e162f commit 595aa63

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Update S3 and Cloudfront
2+
on:
3+
push:
4+
branches:
5+
- main
6+
paths:
7+
- .github/workflows/update-s3-and-cloudfront.yaml
8+
- s3/install
9+
10+
jobs:
11+
update-s3-and-cloudfront:
12+
runs-on: ubuntu-latest
13+
timeout-minutes: 30
14+
env:
15+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
16+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
17+
S3_BUCKET_REGION: ${{ secrets.S3_BUCKET_REGION }}
18+
steps:
19+
- uses: actions/checkout@v4
20+
21+
- name: Configure AWS credentials
22+
uses: aws-actions/configure-aws-credentials@v1
23+
with:
24+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
25+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
26+
aws-region: ${{ secrets.S3_BUCKET_REGION }}
27+
28+
- name: Deploy
29+
# The path to the install script will be /adc/install
30+
run: aws s3 sync ./s3 s3://${{ secrets.S3_BUCKET }}/adc
31+
32+
- name: Invalidate CloudFront
33+
uses: chetan/invalidate-cloudfront-action@v2
34+
env:
35+
DISTRIBUTION: ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }}
36+
PATHS: "/"
37+
AWS_REGION: "${{ secrets.S3_BUCKET_REGION }}"
38+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
39+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

s3/install

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env bash
2+
3+
ARCH="$(uname -m)"
4+
OS="$(uname)"
5+
6+
# convert to standard arch names used in files
7+
if [ "x${ARCH}" = "xx86_64" ]; then
8+
ARCH="amd64"
9+
fi
10+
11+
# convert to standard os names used in files
12+
if [ "x${OS}" = "xDarwin" ]; then
13+
OS="darwin"
14+
else
15+
OS="linux"
16+
fi
17+
18+
# either specify the version in an environment variable or get the latest from GitHub
19+
if [ "x${ADC_VERSION}" = "x" ]; then
20+
ADC_VERSION=$(curl -L -s https://github.com/api7/adc/releases/latest |
21+
grep "adc/releases/tag/" | head -1 | awk -F '"' '{print $4}' |
22+
awk -F '/' '{print $NF}')
23+
fi
24+
25+
if [ "x${ADC_VERSION}" = "x" ]; then
26+
printf "Unable to find the latest version of ADC. Please set the ADC_VERSION environment variable and try again. For example, export ADC_VERSION=0.5.0\n"
27+
exit 1
28+
fi
29+
30+
# if version has v in prefix, remove it
31+
ADC_VERSION=${ADC_VERSION#v}
32+
33+
FILENAME="adc_${ADC_VERSION}_${OS}_${ARCH}.tar.gz"
34+
35+
# example download URL format: https://github.com/api7/adc/releases/download/v0.5.0/adc_0.5.0_darwin_arm64.tar.gz
36+
URL="https://github.com/api7/adc/releases/download/v${ADC_VERSION}/${FILENAME}"
37+
38+
printf "Downloading ADC v${ADC_VERSION} for ${OS} ${ARCH}..."
39+
# printf "Download URL: %s\n" "$URL"
40+
41+
curl -L -s ${URL} -o ${PWD}/adc.tar.gz
42+
if [ $? -ne 0 ]; then
43+
echo "Error downloading ADC. Please check your internet connection and try again."
44+
exit 1
45+
fi
46+
47+
# temporary folder name to extract the downloaded file
48+
TEMP_FOLDER_NAME=$(tr -dc A-Za-z0-9 </dev/urandom 2>/dev/null | head -c 16)
49+
if [ -z "$TEMP_FOLDER_NAME" ]; then
50+
TEMP_FOLDER_NAME="TEMP_ADC_FOLDER"
51+
fi
52+
53+
mkdir $TEMP_FOLDER_NAME
54+
55+
printf "\nExtracting ADC to temporary folder %s...\n" "$TEMP_FOLDER_NAME"
56+
57+
tar -xzf "${PWD}/adc.tar.gz" -C "${PWD}/${TEMP_FOLDER_NAME}"
58+
if [ $? -ne 0 ]; then
59+
echo "Error extracting ADC. The downloaded file might be corrupted. Please try again and make sure that you are installing the correct version."
60+
exit 1
61+
fi
62+
63+
INSTALL_DIR=${ADC_DIR}
64+
if [ -z "$INSTALL_DIR" ]; then
65+
INSTALL_DIR="/usr/local/bin"
66+
fi
67+
68+
printf "Installing ADC in $INSTALL_DIR...\n"
69+
70+
WHOAMI=$(whoami)
71+
72+
# install adc binary, use sudo if user doesn't have permission to install in INSTALL_DIR
73+
if mv "${PWD}/$TEMP_FOLDER_NAME/adc" "$INSTALL_DIR/adc" >/dev/null 2>&1; then
74+
echo "ADC installed successfully!"
75+
else
76+
if sudo mv ${PWD}/$TEMP_FOLDER_NAME/adc $INSTALL_DIR/adc; then
77+
echo "ADC installed successfully with sudo permissions!"
78+
else
79+
echo "Unable to install ADC. Please check the permissions of the user $WHOAMI for the directory $INSTALL_DIR."
80+
exit 1
81+
fi
82+
fi
83+
84+
# clean up temporary files
85+
printf "Removing temporary files...\n"
86+
rm -rf adc.tar.gz ${PWD}/$TEMP_FOLDER_NAME/
87+
88+
printf "Done!\n"

0 commit comments

Comments
 (0)