forked from apache/atlas
-
Notifications
You must be signed in to change notification settings - Fork 10
199 lines (178 loc) · 8.04 KB
/
main-ecr.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
name: Build and Push Atlas Metastore docker image to ECR(deprecated as of Jan 11, 2023)
on:
push:
branches:
- xoxo/deprecating-ecr/xoxo
permissions:
id-token: write
contents: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Get branch name
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
id: get_branch
- name: Get repository name
run: echo "REPOSITORY_NAME=`echo "$GITHUB_REPOSITORY" | awk -F / '{print $2}' | sed -e "s/:refs//"`" >> $GITHUB_ENV
shell: bash
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Cache Maven packages
uses: actions/cache@v2
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/build.sh') }}
restore-keys: ${{ runner.os }}-m2
- name: Build with Maven
run: |
branch_name=${{ steps.get_branch.outputs.branch }}
if [[ $branch_name == 'main' || $branch_name == 'master' || $branch_name == 'lineageondemand' ]]
then
echo "build without dashboard"
chmod +x ./build.sh && ./build.sh build_without_dashboard
else
echo "build with dashboard"
chmod +x ./build.sh && ./build.sh
fi
- name: Get IAM Role and Region to login into ECR
id: get_ecr-details
run: |
branch_name=${{ steps.get_branch.outputs.branch }}
if [[ $branch_name == 'main' || $branch_name == 'staging' || $branch_name == 'master' ]]
then
iamRole=${{ secrets.AWS_PROD_PLATFORM_ECR_ACCESS }}
region='us-east-1'
else
iamRole=${{ secrets.AWS_DEV_PLATFORM_ECR_ACCESS }}
region='ap-south-1'
fi
echo "##[set-output name=iamRole;]$(echo ${iamRole})"
echo "##[set-output name=region;]$(echo ${region})"
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: ${{ steps.get_ecr-details.outputs.iamRole }}
aws-region: ${{ steps.get_ecr-details.outputs.region }}
- name: Check and create ECR Repo if it does not exist
uses: nick-fields/retry@v2
id: check_and_create_repo
with:
timeout_minutes: 10
max_attempts: 5
command: |
aws ecr describe-repositories --repository-names atlanhq/${{ github.event.repository.name }} || aws ecr create-repository --repository-name atlanhq/${{ github.event.repository.name }} --image-tag-mutability IMMUTABLE
while ! $(aws ecr get-repository-policy --repository-name atlanhq/${{ github.event.repository.name }})
do
aws ecr set-repository-policy --repository-name atlanhq/${{ github.event.repository.name }} --policy-text file://ecrorgcrossaccountpolicy.json
registryIdfrompolicy=$(aws ecr get-repository-policy --repository-name atlanhq/${{ github.event.repository.name }} | jq -r '.registryId')
registryIdfromfromecrrepo=$(aws ecr describe-repositories --repository-names atlanhq/${{ github.event.repository.name }} | jq -r '.repositories[0].registryId')
echo "registryIdfrompolicy : $registryIdfrompolicy and registryIdfromfromecrrepo : $registryIdfromfromecrrepo"
if [ $registryIdfrompolicy == $registryIdfromfromecrrepo ]
then
break
fi
done
- name: Get Semantic Version tag
id: semver_tag
run: |
branch_name=${{ steps.get_branch.outputs.branch }}
tag=$(git tag --sort=v:refname | grep -E "^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)-$branch_name$" | tail -1)
if [[ -z $tag ]]
then
echo "Creating a new tag series"
updated_full_tag="1.0.0-$branch_name"
new_tag=1.0.0
else
# echo "Current tag is $tag"
patch_version=$(echo $tag | cut -d '.' -f 3 | cut -d '-' -f 1)
minor_version=$(echo $tag | cut -d '.' -f 2)
major_version=$(echo $tag | cut -d '.' -f 1)
if [[ $patch_version == '999' && $minor_version == '999' ]]
then
bump_type='M'
elif [[ $patch_version == '999' && $minor_version != '999' ]]
then
bump_type='m'
else
bump_type='p'
fi
plain_tag_value=$major_version.$minor_version.$patch_version
chmod +x ./sem-ver-bump.sh
new_tag=$(./sem-ver-bump.sh -$bump_type $plain_tag_value)
updated_full_tag=$new_tag-$branch_name
# echo "Last tag is $tag, Bumping up to $updated_full_tag"
fi
commit_tag=$(git describe --tags --exact-match $GITHUB_SHA 2>/dev/null || echo "no_tag_found")
if [[ $commit_tag == "no_tag_found" ]]
then
echo "No associated tag found, tagging $GITHUB_SHA with $updated_full_tag"
git tag $updated_full_tag $GITHUB_SHA
git push --tags
echo "##[set-output name=build;]$(echo true)"
echo "##[set-output name=new_tag;]$(echo $new_tag)"
echo "Image will be tagged with $new_tag"
else
echo "$GITHUB_SHA is already tagged with $commit_tag"
IMAGE_META=$(aws ecr describe-images --repository-name=atlanhq/${{ github.event.repository.name }} --image-ids=imageTag=$commit_tag 2>/dev/null || echo "no_tag_found")
if [[ $IMAGE_META == "no_tag_found" ]]; then
echo "Image with specified tag does not exist, proceeding to build and push"
existing_tag=$(echo $commit_tag | cut -d '-' -f 1)
echo "##[set-output name=new_tag;]$(echo $existing_tag)"
echo "##[set-output name=build;]$(echo true)"
echo "Image will be tagged with $existing_tag"
else
IMAGE_TAGS="$( echo ${IMAGE_META} | jq '.imageDetails[0].imageTags[0]' -r )"
echo "Image with the specified tag exists, Skipping build"
existing_tag=$(echo $commit_tag | cut -d '-' -f 1)
echo "##[set-output name=new_tag;]$(echo $existing_tag)"
echo "##[set-output name=build;]$(echo false)"
fi
fi
- name: Keep last 10 tags and delete rest of the old tags
id: cleanup-tags
run: |
branch_name=${{ steps.get_branch.outputs.branch }}
git tag --sort=v:refname | grep -E "^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)-$branch_name" > tagsfile
cat tagsfile
vars=($(awk -F= '{print $1}' tagsfile))
Npars=${#vars[@]}
echo "Npars : $Npars"
numberOfTagsToKeep=10
if [ $Npars -gt $numberOfTagsToKeep ]
then
for ((i=0; i<$Npars - $numberOfTagsToKeep; i++)); do
echo "var $i : ${vars[$i]}"
git push --delete origin ${vars[$i]}
done
fi
- name: Login to Amazon ECR
id: login-ecr
if: steps.semver_tag.outputs.build == 'true'
uses: aws-actions/amazon-ecr-login@v1
- name: Set up Buildx
id: buildx
if: steps.semver_tag.outputs.build == 'true'
uses: docker/setup-buildx-action@v2
with:
driver-opts: image=moby/buildkit:master
- name: Build and push docker image
id: docker_build
if: steps.semver_tag.outputs.build == 'true'
uses: docker/build-push-action@v3
with:
context: .
file: ./Dockerfile
push: true
tags: |
${{ steps.login-ecr.outputs.registry }}/atlanhq/${{ github.event.repository.name }}:${{ steps.semver_tag.outputs.new_tag }}-${{ steps.get_branch.outputs.branch }}
${{ steps.login-ecr.outputs.registry }}/atlanhq/${{ github.event.repository.name }}:${{ steps.get_branch.outputs.branch }}-${{ steps.semver_tag.outputs.new_tag }}
build-args: |
ACCESS_TOKEN_USR=$GITHUB_ACTOR
ACCESS_TOKEN_PWD=${{ secrets.ORG_PAT_GITHUB }}