Skip to content

Commit 3dac353

Browse files
akshatsinha007Akshat Sinha
and
Akshat Sinha
authored
feat: Integration of Cranecopy plugin (#5131)
* Added cranecopy plugin * Update 260_cranecopy.down.sql * Update and rename 247_cranecopy.up.sql to 260_cranecopy.up.sql * Rename 247_cranecopy.down.sql to 260_cranecopy.down.sql * Rename 260_cranecopy.down.sql to 261_cranecopy.down.sql * Rename 260_cranecopy.up.sql to 261_cranecopy.up.sql * added image * Rename 261_cranecopy.down.sql to 265_cranecopy.down.sql * Rename 261_cranecopy.up.sql to 265_cranecopy.up.sql --------- Co-authored-by: Akshat Sinha <akshatsinha@Akshats-MacBook-Pro.local>
1 parent 48bfbae commit 3dac353

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed

assets/cranecopy.png

34.9 KB
Loading

scripts/sql/265_cranecopy.down.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
DELETE FROM pipeline_stage_step_variable where pipeline_stage_step_id in (select id from pipeline_stage_step where name ='CraneCopy');
2+
DELETE FROM plugin_step_variable where plugin_step_id=(SELECT ps.id FROM plugin_metadata p inner JOIN plugin_step ps on ps.plugin_id=p.id WHERE p.name='CraneCopy' and ps."index"=1 and ps.deleted=false);
3+
DELETE FROM plugin_stage_mapping where plugin_id=(SELECT id from plugin_metadata where name='CraneCopy');
4+
DELETE FROM plugin_step where plugin_id=(SELECT id FROM plugin_metadata WHERE name='CraneCopy');
5+
DELETE FROM plugin_tag_relation WHERE plugin_id=(SELECT id FROM plugin_metadata WHERE name='CraneCopy');
6+
DELETE FROM pipeline_stage_step WHERE name ='CraneCopy';
7+
DELETE FROM plugin_metadata where name='CraneCopy';

scripts/sql/265_cranecopy.up.sql

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
INSERT INTO plugin_metadata (id,name,description,type,icon,deleted,created_on,created_by,updated_on,updated_by)
2+
VALUES (nextval('id_seq_plugin_metadata'),'CraneCopy','The Crane Copy plugin can be used to copy container images from one registry to another.The Plugin can only be used in Post build Stage.','PRESET','https://raw.githubusercontent.com/devtron-labs/devtron/main/assets/cranecopy.png',false,'now()',1,'now()',1);
3+
4+
INSERT INTO plugin_stage_mapping (id,plugin_id,stage_type,created_on,created_by,updated_on,updated_by)
5+
VALUES (nextval('id_seq_plugin_stage_mapping'),(SELECT id from plugin_metadata where name='CraneCopy'), 0,'now()',1,'now()',1);
6+
7+
INSERT INTO "plugin_pipeline_script" ("id", "script","type","deleted","created_on", "created_by", "updated_on", "updated_by")
8+
VALUES (
9+
nextval('id_seq_plugin_pipeline_script'),
10+
$$#!/bin/sh
11+
set -eo pipefail
12+
13+
type=$(echo $CI_CD_EVENT | jq -r '.type')
14+
if [[ "$type" == "CD" ]]; then
15+
echo "You are in Deployment stage,the plugin can only be used in Post Build Stage"
16+
exit 1
17+
fi
18+
19+
echo "##################################################
20+
# #
21+
# CRANE COPY PLUGIN IS RUNNING... #
22+
# #
23+
###################################################
24+
"
25+
targetRegistry="$TargetRegistry"
26+
targetRepo="${targetRegistry#*/}"
27+
username="$RegistryUsername"
28+
password="$RegistryPassword"
29+
sourcerepo=$(echo "$CI_CD_EVENT" | jq -r '.commonWorkflowRequest.dockerRepository')
30+
sourceregistry=$(echo "$CI_CD_EVENT" | jq -r '.commonWorkflowRequest.dockerRegistryURL')
31+
export sourcekey="$(echo "$CI_CD_EVENT" | jq -r '.commonWorkflowRequest.accessKey')"
32+
export sourcepassd="$(echo "$CI_CD_EVENT" | jq -r '.commonWorkflowRequest.secretKey')"
33+
export sourceregion="$(echo "$CI_CD_EVENT" | jq -r '.commonWorkflowRequest.awsRegion')"
34+
Tag=$(echo "$CI_CD_EVENT" | jq -r '.commonWorkflowRequest.dockerImageTag')
35+
sourcepass=$(echo "$CI_CD_EVENT" | jq -r '.commonWorkflowRequest.dockerPassword')
36+
sourceuser=$(echo "$CI_CD_EVENT" | jq -r '.commonWorkflowRequest.dockerUsername')
37+
source_is_ecr=false
38+
target_is_ecr=false
39+
if [[ "$sourceregistry" == *"amazonaws.com"* ]]; then
40+
source_is_ecr=true
41+
fi
42+
43+
if [[ "$targetRegistry" == *"amazonaws.com"* ]]; then
44+
target_is_ecr=true
45+
fi
46+
47+
if [[ "$targetRegistry" == *"pkg.dev"* ]]; then
48+
echo $RegistryPassword > output.txt
49+
cat output.txt| base64 -d > key.json
50+
auth=$(docker run --rm --name gcloud-config -v "$(pwd)/key.json":/key.json gcr.io/google.com/cloudsdktool/google-cloud-cli:alpine /bin/bash -c 'gcloud auth login --no-launch-browser --cred-file="/key.json" && gcloud auth print-access-token' | tail -n1)
51+
username=oauth2accesstoken
52+
password=$auth
53+
fi
54+
55+
if $source_is_ecr && $target_is_ecr; then
56+
region="${targetRegistry##*.dkr.ecr.}"
57+
region="${region%%.*}"
58+
export region
59+
export AWS_ACCESS_KEY_ID="$username"
60+
export AWS_SECRET_ACCESS_KEY="$password"
61+
aws_auth=$(docker run --rm -e AWS_ACCESS_KEY_ID="$AWS_ACCESS_KEY_ID" -e AWS_SECRET_ACCESS_KEY="$AWS_SECRET_ACCESS_KEY" -e AWS_DEFAULT_REGION="$region" amazon/aws-cli ecr get-login-password --region "$region")
62+
aws_pass=$(docker run --rm -e AWS_ACCESS_KEY_ID="$sourcekey" -e AWS_SECRET_ACCESS_KEY="$sourcepassd" -e AWS_DEFAULT_REGION="$sourceregion" amazon/aws-cli ecr get-login-password --region "$sourceregion")
63+
docker run --rm --entrypoint /busybox/sh gcr.io/go-containerregistry/crane:debug -c " \
64+
mkdir ytr && \
65+
crane auth login -u AWS -p '$aws_pass' '$sourceregistry' && \
66+
crane pull $sourceregistry/$sourcerepo:$Tag /ytr --platform=all --format=oci && \
67+
crane auth login -u AWS -p '$aws_auth' '${targetRegistry%%/*}' && \
68+
crane push /ytr '$targetRegistry':$Tag && \
69+
echo -e '\nSuccessfully copied image from $sourceregistry/$sourcerepo:$Tag to $targetRegistry:$Tag' && \
70+
echo -e '\nImage Details:' && \
71+
echo -e 'Repository: $targetRepo' && \
72+
echo -e 'Tag: $Tag' && \
73+
echo -e 'Image Digest:' && \
74+
crane digest '$targetRegistry:$Tag'"
75+
docker login -u AWS -p $aws_pass $sourceregistry
76+
elif $target_is_ecr; then
77+
region="${targetRegistry##*.dkr.ecr.}"
78+
region="${region%%.*}"
79+
export region
80+
export AWS_ACCESS_KEY_ID="$username"
81+
export AWS_SECRET_ACCESS_KEY="$password"
82+
aws_auth=$(docker run --rm -e AWS_ACCESS_KEY_ID="$AWS_ACCESS_KEY_ID" -e AWS_SECRET_ACCESS_KEY="$AWS_SECRET_ACCESS_KEY" -e AWS_DEFAULT_REGION="$region" amazon/aws-cli ecr get-login-password --region "$region")
83+
docker run --rm --entrypoint /busybox/sh -v /root/.docker:/root/.docker gcr.io/go-containerregistry/crane:debug -c " \
84+
mkdir ytr && \
85+
crane pull $sourceregistry/$sourcerepo:$Tag /ytr --platform=all --format=oci && \
86+
echo "${targetRegistry%%/*}" && \
87+
crane auth login -u AWS -p '$aws_auth' '${targetRegistry%%/*}' && \
88+
crane push /ytr '$targetRegistry':$Tag && \
89+
echo -e '\nSuccessfully copied image from $sourceregistry/$sourcerepo:$Tag to $targetRegistry:$Tag' && \
90+
echo -e '\nImage Details:' && \
91+
echo -e 'Repository: $targetRepo' && \
92+
echo -e 'Tag: $Tag' && \
93+
echo -e 'Image Digest:' && \
94+
crane digest '$targetRegistry:$Tag'"
95+
if [[ "$sourceregistry" == *"pkg.dev"* ]]; then
96+
echo "$CI_CD_EVENT" | jq -r .commonWorkflowRequest.dockerPassword | tr -d "'" > gcld.json
97+
wauth=$(docker run --rm --name gcloud-config -v "$(pwd)/gcld.json":/gcld.json gcr.io/google.com/cloudsdktool/google-cloud-cli:alpine /bin/bash -c 'gcloud auth login --no-launch-browser --cred-file="/gcld.json" && gcloud auth print-access-token' | tail -n1)
98+
docker login -u oauth2accesstoken -p $wauth $sourceregistry
99+
else
100+
docker login -u $sourceuser -p $sourcepass $sourceregistry
101+
fi
102+
103+
elif $source_is_ecr; then
104+
export sourcekey="$(echo "$CI_CD_EVENT" | jq -r '.commonWorkflowRequest.accessKey')"
105+
export sourcepassd="$(echo "$CI_CD_EVENT" | jq -r '.commonWorkflowRequest.secretKey')"
106+
export sourceregion="$(echo "$CI_CD_EVENT" | jq -r '.commonWorkflowRequest.awsRegion')"
107+
aws_pass=$(docker run --rm -e AWS_ACCESS_KEY_ID="$sourcekey" -e AWS_SECRET_ACCESS_KEY="$sourcepassd" -e AWS_DEFAULT_REGION="$sourceregion" amazon/aws-cli ecr get-login-password --region "$sourceregion" )
108+
docker run --rm --entrypoint /busybox/sh gcr.io/go-containerregistry/crane:debug -c " \
109+
mkdir ytr && \
110+
crane auth login -u AWS -p '$aws_pass' '$sourceregistry' && \
111+
crane pull $sourceregistry/$sourcerepo:$Tag /ytr --platform=all --format=oci && \
112+
crane auth login -u "$username" -p "$password" "${targetRegistry%%/*}" && \
113+
crane push /ytr \"$targetRegistry\":$Tag && \
114+
echo -e '\nSuccessfully copied image from $sourceregistry/$sourcerepo:$Tag to $targetRegistry:$Tag' && \
115+
echo -e '\nImage Details:' && \
116+
echo -e 'Repository: $targetRepo' && \
117+
echo -e 'Tag: $Tag' && \
118+
echo -e 'Image Digest:' && \
119+
crane digest \"$targetRegistry:$Tag\""
120+
docker login -u AWS -p $aws_pass $sourceregistry
121+
else
122+
docker run --rm --entrypoint /busybox/sh -v /root/.docker:/root/.docker gcr.io/go-containerregistry/crane:debug -c " \
123+
mkdir ytr && \
124+
crane pull $sourceregistry/$sourcerepo:$Tag /ytr --platform=all --format=oci && \
125+
crane auth login -u "$username" -p "$password" "${targetRegistry%%/*}" && \
126+
crane push /ytr \"$targetRegistry\":$Tag && \
127+
echo -e '\nSuccessfully copied image from $sourceregistry/$sourcerepo:$Tag to $targetRegistry:$Tag' && \
128+
echo -e '\nImage Details:' && \
129+
echo -e 'Repository: $targetRepo' && \
130+
echo -e 'Tag: $Tag' && \
131+
echo -e 'Image Digest:' && \
132+
crane digest \"$targetRegistry:$Tag\""
133+
if [[ "$sourceregistry" == *"pkg.dev"* ]]; then
134+
echo "$CI_CD_EVENT" | jq -r .commonWorkflowRequest.dockerPassword | tr -d "'" > gcld.json
135+
wauth=$(docker run --rm --name gcloud-config -v "$(pwd)/gcld.json":/gcld.json gcr.io/google.com/cloudsdktool/google-cloud-cli:alpine /bin/bash -c 'gcloud auth login --no-launch-browser --cred-file="/gcld.json" && gcloud auth print-access-token' | tail -n1)
136+
docker login -u oauth2accesstoken -p $wauth $sourceregistry
137+
else
138+
docker login -u $sourceuser -p $sourcepass $sourceregistry
139+
fi
140+
fi
141+
142+
$$,
143+
'SHELL',
144+
'f',
145+
'now()',
146+
1,
147+
'now()',
148+
1
149+
);
150+
151+
152+
153+
INSERT INTO "plugin_step" ("id", "plugin_id","name","description","index","step_type","script_id","deleted", "created_on", "created_by", "updated_on", "updated_by")
154+
VALUES (nextval('id_seq_plugin_step'), (SELECT id FROM plugin_metadata WHERE name='CraneCopy'),'Step 1','Step 1 - CraneCopy','1','INLINE',(SELECT last_value FROM id_seq_plugin_pipeline_script),'f','now()', 1, 'now()', 1);
155+
156+
INSERT INTO plugin_step_variable (id,plugin_step_id,name,format,description,is_exposed,allow_empty_value,default_value,value,variable_type,value_type,previous_step_index,variable_step_index,variable_step_index_in_plugin,reference_variable_name,deleted,created_on,created_by,updated_on,updated_by)
157+
VALUES (nextval('id_seq_plugin_step_variable'),(SELECT ps.id FROM plugin_metadata p inner JOIN plugin_step ps on ps.plugin_id=p.id WHERE p.name='CraneCopy' and ps."index"=1 and ps.deleted=false),'TargetRegistry','STRING','The target registry to push the image.In the format taregtregistry.com/repo','t','f',null,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1);
158+
159+
INSERT INTO plugin_step_variable (id,plugin_step_id,name,format,description,is_exposed,allow_empty_value,default_value,value,variable_type,value_type,previous_step_index,variable_step_index,variable_step_index_in_plugin,reference_variable_name,deleted,created_on,created_by,updated_on,updated_by)
160+
VALUES (nextval('id_seq_plugin_step_variable'),(SELECT ps.id FROM plugin_metadata p inner JOIN plugin_step ps on ps.plugin_id=p.id WHERE p.name='CraneCopy' and ps."index"=1 and ps.deleted=false),'RegistryUsername','STRING','The username for authentication.(Provide AWS Access key ID in case of ECR)','t','f',null,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1);
161+
162+
INSERT INTO plugin_step_variable (id,plugin_step_id,name,format,description,is_exposed,allow_empty_value,default_value,value,variable_type,value_type,previous_step_index,variable_step_index,variable_step_index_in_plugin,reference_variable_name,deleted,created_on,created_by,updated_on,updated_by)
163+
VALUES (nextval('id_seq_plugin_step_variable'),(SELECT ps.id FROM plugin_metadata p inner JOIN plugin_step ps on ps.plugin_id=p.id WHERE p.name='CraneCopy' and ps."index"=1 and ps.deleted=false),'RegistryPassword','STRING','The password to the registry for authentication.(Provide AWS Secret Access key in case of ECR).','t','f',null,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1);

0 commit comments

Comments
 (0)