-
Notifications
You must be signed in to change notification settings - Fork 1
/
entrypoint.sh
executable file
·86 lines (70 loc) · 3.01 KB
/
entrypoint.sh
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
#!/bin/sh -l
set -e # if a command fails it stops the execution
set -u # script fails if trying to access to an undefined variable
echo "[+] Action start"
KUSTOMIZE_VERSION="${1}"
KUSTOMIZE_IMAGES="${2}"
USER_EMAIL="${3}"
USER_NAME="${4}"
GITHUB_SERVER="${5}"
REPOSITORY_USERNAME="${6}"
REPOSITORY_NAME="${7}"
TARGET_BRANCH="${8}"
TARGET_DIRECTORY="${9}"
COMMIT_MESSAGE="${10}"
ORIGIN_COMMIT="https://$GITHUB_SERVER/$GITHUB_REPOSITORY/commit/$GITHUB_SHA"
COMMIT_MESSAGE="${COMMIT_MESSAGE/ORIGIN_COMMIT/$ORIGIN_COMMIT}"
COMMIT_MESSAGE="${COMMIT_MESSAGE/\$GITHUB_REF/$GITHUB_REF}"
COMMIT_MESSAGE="${COMMIT_MESSAGE/KUSTOMIZE_IMAGES/$KUSTOMIZE_IMAGES}"
# Make sure we have a version:
if [ -z $KUSTOMIZE_VERSION ]; then
echo "[+] Downloding Kustomize latest version"
curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash
else
echo "[+] Downloding Kustomize $KUSTOMIZE_VERSION version"
curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh $KUSTOMIZE_VERSION" | bash
fi
if [ -z "$USER_NAME" ]; then
USER_NAME="$REPOSITORY_USERNAME"
fi
BASE_DIR=$(pwd)
CLONE_DIR=$(mktemp -d)
echo "[+] Cloning destination git repository $REPOSITORY_NAME"
# Setup git
git config --global user.email "$USER_EMAIL"
git config --global user.name "$USER_NAME"
{
git clone --single-branch --branch "$TARGET_BRANCH" "https://$USER_NAME:$API_TOKEN_GITHUB@$GITHUB_SERVER/$REPOSITORY_USERNAME/$REPOSITORY_NAME.git" "$CLONE_DIR"
} || {
echo "::error::Could not clone the destination repository. Command:"
echo "::error::git clone --single-branch --branch $TARGET_BRANCH https://$USER_NAME:the_api_token@$GITHUB_SERVER/$REPOSITORY_USERNAME/$REPOSITORY_NAME.git $CLONE_DIR"
echo "::error::(Note that the USER_NAME and API_TOKEN is redacted by GitHub)"
echo "::error::Please verify that the target repository exist AND that it contains the destination branch name, and is accesible by the API_TOKEN_GITHUB"
exit 1
}
if [ ! -d "$CLONE_DIR/$TARGET_DIRECTORY" ]; then
ls -a $CLONE_DIR/
echo "::error::Requested directory doesn't exist: $CLONE_DIR/$TARGET_DIRECTORY"
exit 1
fi
if [ ! -f "$CLONE_DIR/$TARGET_DIRECTORY/kustomization.yaml" ]; then
echo "::error::kustomization.yaml doesn't exist in $CLONE_DIR/$TARGET_DIRECTORY"
exit 1
fi
echo "[+] cd into $CLONE_DIR/$TARGET_DIRECTORY"
cd $CLONE_DIR/$TARGET_DIRECTORY
echo "[+] Running Kustomize"
$BASE_DIR/kustomize edit set image $KUSTOMIZE_IMAGES || {
echo "::error::Kustomize failed"
exit 1
}
echo "[+] Adding git commit"
git add .
echo "[+] git status:"
git status
echo "[+] git diff-index:"
# git diff-index : to avoid doing the git commit failing if there are no changes to be commit
git diff-index --quiet HEAD || git commit --message "$COMMIT_MESSAGE"
echo "[+] Pushing git commit"
# --set-upstream: sets de branch when pushing to a branch that does not exist
git push "https://$USER_NAME:$API_TOKEN_GITHUB@$GITHUB_SERVER/$REPOSITORY_USERNAME/$REPOSITORY_NAME.git" --set-upstream "$TARGET_BRANCH"