-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathentrypoint.sh
executable file
·143 lines (121 loc) · 3.96 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
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
#!/usr/bin/env bash
set -e
git version
git config --global --add safe.directory /github/workspace
echo "Creating artifact.."
COMMIT_MESSAGE=$(git log -1 --pretty=%B)
COMMIT_AUTHOR=$(git log -1 --pretty=format:'%an')
COMMIT_AUTHOR_EMAIL=$(git log -1 --pretty=format:'%ae')
COMMIT_COMITTER=$(git log -1 --pretty=format:'%cn')
COMMIT_COMITTER_EMAIL=$(git log -1 --pretty=format:'%ce')
COMMIT_CREATED=$(git log -1 --format=%cI)
BRANCH=${GITHUB_HEAD_REF} # For PRs this var has the branch, see https://docs.github.com/en/actions/reference/environment-variables
if [ -z "$BRANCH" ]; then BRANCH=${GITHUB_REF##refs/heads/}; fi
export GITHUB_BRANCH=$BRANCH
# TODO check if head sha is better suited for the workflows: https://github.community/t/github-sha-isnt-the-value-expected/17903/2
EVENT="push"
SHA=$GITHUB_SHA
URL="https://github.com/$GITHUB_REPOSITORY/commit/$GITHUB_SHA"
if [[ -n "$GITHUB_BASE_REF" ]];
then
EVENT="pr"
SHA=$INPUT_BRANCHHEAD
SOURCE_BRANCH=$GITHUB_BASE_REF
TARGET_BRANCH=$GITHUB_TARGET_REF
PR_NUMBER=$(echo "$GITHUB_REF" | awk -F / '{print $3}')
URL="https://github.com/$GITHUB_REPOSITORY/pull/$PR_NUMBER"
fi
if [[ $GITHUB_REF == refs/tags/* ]] # True if $GITHUB_REF starts with a "refs/tags/" (wildcard matching).
then
TAG=${GITHUB_REF##refs/tags/}
EVENT="tag"
fi
gimlet artifact create \
--repository "$GITHUB_REPOSITORY" \
--sha "$SHA" \
--created "$COMMIT_CREATED" \
--branch "$BRANCH" \
--event "$EVENT" \
--sourceBranch "$SOURCE_BRANCH" \
--targetBranch "$TARGET_BRANCH" \
--tag "$TAG" \
--authorName "$COMMIT_AUTHOR" \
--authorEmail "$COMMIT_AUTHOR_EMAIL" \
--committerName "$COMMIT_COMITTER" \
--committerEmail "$COMMIT_COMITTER_EMAIL" \
--message "$COMMIT_MESSAGE" \
--url "$URL" \
> artifact.json
echo "Attaching CI run URL.."
gimlet artifact add \
-f artifact.json \
--field "name=CI" \
--field "url=$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"
echo "Attaching custom fields.."
fields=$(echo $INPUT_FIELDS | tr ";" "\n")
for field in $fields
do
# Set the delimiter
OLDIFS=$IFS
IFS='='
#Read the split words into an array based on delimiter
read -a key_value <<< "$field"
IFS=$OLDIFS
gimlet artifact add \
-f artifact.json \
--field "name=${key_value[0]}" \
--field "url=${key_value[1]}"
done
echo "Attaching Gimlet manifests.."
vars_file=".gimlet/vars"
for file in .gimlet/*
do
if [[ "$file" == $vars_file ]]; then
continue
fi
if [[ -f $file ]]; then
gimlet artifact add -f artifact.json --envFile $file
fi
done
echo "Attaching environment variable context.."
VARS=$(printenv | grep GITHUB | grep -v '=$' | awk '$0="--var "$0')
gimlet artifact add -f artifact.json $VARS
echo "Attaching common Gimlet variables.."
gimlet artifact add \
-f artifact.json \
--var "REPO=$GITHUB_REPOSITORY" \
--var "OWNER=$GITHUB_REPOSITORY_OWNER" \
--var "BRANCH=$BRANCH" \
--var "TAG=$TAG" \
--var "SHA=$GITHUB_SHA" \
--var "ACTOR=$GITHUB_ACTOR" \
--var "EVENT=$GITHUB_EVENT_NAME" \
--var "JOB=$GITHUB_JOB"
echo "Attaching variables file.."
if [[ -f $vars_file ]]; then
gimlet artifact add -f artifact.json --varsFile $vars_file
fi
if [[ "$INPUT_DEBUG" == "true" ]]; then
cat artifact.json
exit 0
fi
echo "Shipping artifact.."
PUSH_RESULT=$(gimlet artifact push -f artifact.json --output json)
if [ $? -ne 0 ]; then
echo $PUSH_RESULT
exit 1
fi
ARTIFACT_ID=$(echo $PUSH_RESULT | jq -r '.id' )
echo "Shipped artifact ID is: $ARTIFACT_ID"
echo "::set-output name=artifact-id::$ARTIFACT_ID"
if [[ "$INPUT_WAIT" == "true" || "$INPUT_DEPLOY" == "true" ]]; then
gimlet artifact track --wait --timeout $INPUT_TIMEOUT $ARTIFACT_ID
else
gimlet artifact track $ARTIFACT_ID
fi
if [[ "$INPUT_DEPLOY" == "true" ]]; then
echo "Deploying.."
RELEASE_ID=$(gimlet release make --artifact $ARTIFACT_ID --env $INPUT_ENV --app $INPUT_APP --output json | jq -r '.id')
echo "Deployment ID is: $RELEASE_ID"
gimlet release track --wait --timeout $INPUT_TIMEOUT $RELEASE_ID
fi