Package: cdk-gitlab-runner
Welcome tocdk-gitlab-runnerUse AWS CDK to create gitlab runner, and usegitlab runnerto help you execute your Gitlab Pipeline Job.GitLab Runner is the open source project that is used to run your CI/CD jobs and send the results back to GitLab.(source repo)WhyGitlab provides400 minutes per month for each free user, hosted Gitlab Runner to execute your gitlab pipeline job.That's pretty good and users don't need to manage gitlab runner. If it is just a simple ci job for test 400, it may be enough.
But what if you want to deploy to your AWS production environment through pipeline job?
Is there any security consideration for using the hosted gitlab runner?!But creating Gitlab Runner is not that simple, so I created this OSS so that you can quickly create Gitlab Runner and delete your Gitlab Runner via AWS CDK.
It will be used with AWS IAM Role, so you don't need to put AKSK in Gitlab environment variables.NoteDefault will help you generate below services:VPCPublic Subnet (2)EC2 (1 T3.micro)Before start you need gitlab runner token in yourgitlab projectorgitlab groupIn Group before Gitlab 15.10This registration process is only supported in GitLab Runner 15.10 or later
This registration process is not supported in GitLab Runner 15.9 or earlier and only available as an experimental feature in GitLab Runner 15.10 and 15.11. You should upgrade to GitLab Runner 16.0 or later to use a stable version of this registration process.Check this issueGroup > Settings > CI/CDIn Group after Gitlab 15.10This registration process is only supported in GitLab Runner 15.10 or later
This registration process is not supported in GitLab Runner 15.9 or earlier and only available as an experimental feature in GitLab Runner 15.10 and 15.11. You should upgrade to GitLab Runner 16.0 or later to use a stable version of this registration process.Check this issueGroup > Build > RunnersIn Project before Gitlab 15.10This registration process is only supported in GitLab Runner 15.10 or later
This registration process is not supported in GitLab Runner 15.9 or earlier and only available as an experimental feature in GitLab Runner 15.10 and 15.11. You should upgrade to GitLab Runner 16.0 or later to use a stable version of this registration process.Check this issueProject > Settings > CI/CD > RunnersIn Project after Gitlab 15.10This registration process is only supported in GitLab Runner 15.10 or later
This registration process is not supported in GitLab Runner 15.9 or earlier and only available as an experimental feature in GitLab Runner 15.10 and 15.11. You should upgrade to GitLab Runner 16.0 or later to use a stable version of this registration process.Check this issueProject > Settings > CI/CD > RunnersUsageReplace your gitlab runner token in$GITLABTOKENInstallUse the npm dist tag to opt in CDKv1 or CDKv2://forCDKv2
npminstallcdk-gitlab-runner
or
npminstallcdk-gitlab-runner@latest

//forCDKv1
npminstallcdk-gitlab-runner@cdkv1💡💡💡 please clickhere, if you are using aws-cdk v1.x.x version.💡💡💡Instance Typeimport{GitlabContainerRunner}from'cdk-gitlab-runner';//Ifwantchangeinstancetypetot3.large.newGitlabContainerRunner(this,'runner-instance',{gitlabtoken:'glrt-GITLABTOKEN',ec2type:'t3.large',gitlabRunnerVersion:'15.10'});//OR//Justcreateagitlabrunner,bydefaultinstancetypeist3.micro.import{GitlabContainerRunner}from'cdk-gitlab-runner';newGitlabContainerRunner(this,'runner-instance',{gitlabtoken:'glrt-GITLABTOKEN',gitlabRunnerVersion:'15.10'});Gitlab Server Customize Url .If you want change what you want tag name .//IfyouwantchangewhatyourselfGitlabServerUrl.import{GitlabContainerRunner}from'cdk-gitlab-runner';newGitlabContainerRunner(this,'runner-instance-change-tag',{gitlabtoken:'glrt-GITLABTOKEN',gitlaburl:'https://gitlab.my.com/',gitlabRunnerVersion:'15.10'});TagsIf you want change what you want tag name .
!!! Not support Gitlab Runner 15.10 and later !!!//Ifyouwantchangewhatyouwanttagname.import{GitlabContainerRunner}from'cdk-gitlab-runner';newGitlabContainerRunner(this,'runner-instance-change-tag',{gitlabtoken:'glrt-GITLABTOKEN',gitlabRunnerVersion:'15.10',tags:['aa','bb','cc'],});IAM PolicyIf you want add runner other IAM Policy like s3-readonly-access.//IfyouwantaddrunnerotherIAMPolicylikes3-readonly-access.import{GitlabContainerRunner}from'cdk-gitlab-runner';import{ManagedPolicy}from'aws-cdk-lib/aws-iam';construnner=newGitlabContainerRunner(this,'runner-instance-add-policy',{gitlabtoken:'glrt-GITLABTOKEN',gitlabRunnerVersion:'15.10',tags:['aa','bb','cc'],});runner.runnerRole.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName('AmazonS3ReadOnlyAccess'),);Security GroupIf you want add runner other SG Ingress .//IfyouwantaddrunnerotherSGIngress.import{GitlabContainerRunner}from'cdk-gitlab-runner';import{Port,Peer}from'aws-cdk-lib/aws-ec2';construnner=newGitlabContainerRunner(this,'runner-add-SG-ingress',{gitlabtoken:'glrt-GITLABTOKEN',gitlabRunnerVersion:'15.10',tags:['aa','bb','cc'],});//youcanaddingressinyourrunnerSG.runner.defaultRunnerSG.connections.allowFrom(Peer.ipv4('0.0.0.0/0'),Port.tcp(80),);Use self VPC2020/06/27 , you can use your self exist VPC or new VPC , but please check yourvpc public SubnetAuto-assign public IPv4 address must be Yes ,orvpc private Subnetroute table associatednat gateway.import{GitlabContainerRunner}from'cdk-gitlab-runner';import{Port,Peer,Vpc,SubnetType}from'aws-cdk-lib/aws-ec2';import{ManagedPolicy}from'aws-cdk-lib/aws-iam';constnewvpc=newVpc(stack,'VPC',{ipAddresses:IpAddresses.cidr('10.0.0.0/16'),maxAzs:2,subnetConfiguration:[{cidrMask:26,name:'RunnerVPC',subnetType:SubnetType.PUBLIC,},],natGateways:0,});construnner=newGitlabContainerRunner(this,'testing',{gitlabtoken:'glrt-GITLABTOKEN',gitlabRunnerVersion:'15.10',ec2type:'t3.small',selfvpc:newvpc,});Use your self exist role2020/06/27 , you can use your self exist role assign to runnerimport{GitlabContainerRunner}from'cdk-gitlab-runner';import{Port,Peer}from'aws-cdk-lib/aws-ec2';import{ManagedPolicy,Role,ServicePrincipal}from'aws-cdk-lib/aws-iam';constrole=newRole(this,'runner-role',{assumedBy:newServicePrincipal('ec2.amazonaws.com'),description:'For Gitlab EC2 Runner Test Role',roleName:'TestRole',});construnner=newGitlabContainerRunner(stack,'testing',{gitlabtoken:'glrt-GITLABTOKEN',gitlabRunnerVersion:'15.10',ec2iamrole:role,});runner.runnerRole.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName('AmazonS3ReadOnlyAccess'),);Custom Gitlab Runner EBS szie2020/08/22 , you can change you want ebs size.import{GitlabContainerRunner}from'cdk-gitlab-runner';newGitlabContainerRunner(stack,'testing',{gitlabtoken:'glrt-GITLABTOKEN',gitlabRunnerVersion:'15.10',ebsSize:50,});Control the number of runners with AutoScalingGroup2020/11/25 , you can set the number of runners.import{GitlabRunnerAutoscaling}from'cdk-gitlab-runner';newGitlabRunnerAutoscaling(stack,'testing',{gitlabToken:'glrt-GITLABTOKEN',gitlabRunnerVersion:'15.10',minCapacity:2,maxCapacity:2,});Support Spotfleet Gitlab Runner2020/08/27 , you can use spotfleet instance be your gitlab runner,
after create spotfleet instance will auto output instance id.import{GitlabContainerRunner,BlockDuration}from'cdk-gitlab-runner';construnner=newGitlabContainerRunner(stack,'testing',{gitlabToken:'glrt-GITLABTOKEN',gitlabRunnerVersion:'15.10',ec2type:'t3.large',spotFleet:true,});//configuretheexpirationafter1hoursrunner.expireAfter(Duration.hours(1));2020/11/19, you setting job runtime bind host volumes.
see morehttps://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-runnersdocker-sectionimport{GitlabContainerRunner,BlockDuration}from'cdk-gitlab-runner';construnner=newGitlabContainerRunner(stack,'testing',{gitlabToken:'glrt-GITLABTOKEN',gitlabRunnerVersion:'15.10',ec2type:'t3.large',dockerVolumes:[{hostPath:'/tmp/cache',containerPath:'/tmp/cache',},],});Wait about 6 mins , If success you will see your runner in that page .you can use taggitlab,runner,awscdk,!!!!! Not Support Gitlab Runner after 15.10 and laterExamplegitlab-ci.yamlgitlab docs see more ...dockerjob:image:docker:18.09-dindvariables:tags:-runner-awscdk-gitlabvariables:DOCKER_TLS_CERTDIR:""before_script:-docker infoscript:-docker info;-echo 'test 123';-echo 'hello world 1228'If your want to debug you can go to aws consoleIn your runner region !!!AWS Systems Manager > Session Manager > Start a sessionclick yourrunnerand clickstart sessionin the brower console in putbash# become to rootsudo-i# list runner container .root#dockerps-a# modify gitlab-runner/config.tomlroot#cd/home/ec2-user/.gitlab-runner/&&ls
config.toml:clap:  Supporters