Skip to main content

A Gitlab Runner JSII construct lib for AWS CDK

Project description

NPM version PyPI version Release

Downloads npm PyPI

Welcome to cdk-gitlab-runner

This repository template helps you create gitlab runner on your aws account via AWS CDK one line.

Note

Default will help you generate below services:

  • VPC

    • Public Subnet (2)
  • EC2 (1 T3.micro)

Before start you need gitlab runner token in your gitlab project or gitlab group

In Group

Group > Settings > CI/CD group

In Group

Project > Settings > CI/CD > Runners project

Usage

Replace your gitlab runner token in $GITLABTOKEN

Instance Type

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from cdk_gitlab_runner import GitlabContainerRunner

# If want change instance type to t3.large .
GitlabContainerRunner(self, "runner-instance", gitlabtoken="$GITLABTOKEN", ec2type="t3.large")
# OR
# Just create a gitlab runner , by default instance type is t3.micro .
from cdk_gitlab_runner import GitlabContainerRunner

GitlabContainerRunner(self, "runner-instance", gitlabtoken="$GITLABTOKEN")

Gitlab Server Customize Url .

If you want change what you want tag name .

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
# If you want change  what  your self Gitlab Server Url .
from cdk_gitlab_runner import GitlabContainerRunner

GitlabContainerRunner(self, "runner-instance-change-tag",
    gitlabtoken="$GITLABTOKEN",
    gitlaburl="https://gitlab.my.com/"
)

Tags

If you want change what you want tag name .

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
# If you want change  what  you want tag name .
from cdk_gitlab_runner import GitlabContainerRunner

GitlabContainerRunner(self, "runner-instance-change-tag",
    gitlabtoken="$GITLABTOKEN",
    tags=["aa", "bb", "cc"]
)

IAM Policy

If you want add runner other IAM Policy like s3-readonly-access.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
# If you want add runner other IAM Policy like s3-readonly-access.
from cdk_gitlab_runner import GitlabContainerRunner
from aws_cdk.aws_iam import ManagedPolicy

runner = GitlabContainerRunner(self, "runner-instance-add-policy",
    gitlabtoken="$GITLABTOKEN",
    tags=["aa", "bb", "cc"]
)
runner.runner_role.add_managed_policy(
    ManagedPolicy.from_aws_managed_policy_name("AmazonS3ReadOnlyAccess"))

Security Group

If you want add runner other SG Ingress .

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
# If you want add runner other SG Ingress .
from cdk_gitlab_runner import GitlabContainerRunner
from aws_cdk.aws_ec2 import Port, Peer

runner = GitlabContainerRunner(self, "runner-add-SG-ingress",
    gitlabtoken="GITLABTOKEN",
    tags=["aa", "bb", "cc"]
)

# you can add ingress in your runner SG .
runner.default_runner_sG.connections.allow_from(
    Peer.ipv4("0.0.0.0/0"),
    Port.tcp(80))

Use self VPC

2020/06/27 , you can use your self exist VPC or new VPC , but please check your vpc public Subnet Auto-assign public IPv4 address must be Yes ,or vpc private Subnet route table associated nat gateway .

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from cdk_gitlab_runner import GitlabContainerRunner
from aws_cdk.aws_ec2 import Port, Peer, Vpc, SubnetType
from aws_cdk.aws_iam import ManagedPolicy

newvpc = Vpc(stack, "VPC",
    cidr="10.1.0.0/16",
    max_azs=2,
    subnet_configuration=[SubnetConfiguration(
        cidr_mask=26,
        name="RunnerVPC",
        subnet_type=SubnetType.PUBLIC
    )
    ],
    nat_gateways=0
)

runner = GitlabContainerRunner(self, "testing",
    gitlabtoken="$GITLABTOKEN",
    ec2type="t3.small",
    selfvpc=newvpc
)

Use your self exist role

2020/06/27 , you can use your self exist role assign to runner

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from cdk_gitlab_runner import GitlabContainerRunner
from aws_cdk.aws_ec2 import Port, Peer
from aws_cdk.aws_iam import ManagedPolicy, Role, ServicePrincipal

role = Role(self, "runner-role",
    assumed_by=ServicePrincipal("ec2.amazonaws.com"),
    description="For Gitlab EC2 Runner Test Role",
    role_name="TestRole"
)

runner = GitlabContainerRunner(stack, "testing",
    gitlabtoken="$GITLAB_TOKEN",
    ec2iamrole=role
)
runner.runner_role.add_managed_policy(
    ManagedPolicy.from_aws_managed_policy_name("AmazonS3ReadOnlyAccess"))

Custom Gitlab Runner EBS szie

2020/08/22 , you can change you want ebs size.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from cdk_gitlab_runner import GitlabContainerRunner

GitlabContainerRunner(stack, "testing",
    gitlabtoken="$GITLAB_TOKEN",
    ebs_size=50
)

Control the number of runners with AutoScalingGroup

2020/11/25 , you can set the number of runners.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from cdk_gitlab_runner import GitlabRunnerAutoscaling

GitlabRunnerAutoscaling(stack, "testing",
    gitlab_token="$GITLAB_TOKEN",
    min_capacity=2,
    max_capacity=2
)

Support Spotfleet Gitlab Runner

2020/08/27 , you can use spotfleet instance be your gitlab runner, after create spotfleet instance will auto output instance id .thank @pahud again ~~~

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from cdk_gitlab_runner import GitlabContainerRunner, BlockDuration

runner = GitlabContainerRunner(stack, "testing",
    gitlabtoken="GITLAB_TOKEN",
    ec2type="t3.large",
    block_duration=BlockDuration.ONE_HOUR,
    spot_fleet=True
)
# configure the expiration after 1 hours
runner.expire_after(Duration.hours(1))

2020/11/19, you setting job runtime bind host volumes. see more https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-runnersdocker-section

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from cdk_gitlab_runner import GitlabContainerRunner, BlockDuration

runner = GitlabContainerRunner(stack, "testing",
    gitlabtoken="GITLAB_TOKEN",
    ec2type="t3.large",
    docker_volumes=[{
        "host_path": "/tmp/cahce",
        "container_path": "/tmp/cahce"
    }
    ]
)

2020/11/19, support runner auto unregister runner when cdk app destroy.

Note

vs

About change instance type

This is before ( included )

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from aws_cdk.aws_ec2 import InstanceType, InstanceClass, InstanceSize
from cdk_gitlab_runner import GitlabContainerRunner

# If want change instance type to t3.large .
GitlabContainerRunner(self, "runner-instance",
    gitlabtoken="$GITLABTOKEN",
    ec2type=InstanceType.of(InstanceClass.T3, InstanceSize.LARGE)
)

This is

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from cdk_gitlab_runner import GitlabContainerRunner

# If want change instance type to t3.large .
GitlabContainerRunner(self, "runner-instance",
    gitlabtoken="$GITLABTOKEN",
    ec2type="t3.large"
)

Wait about 6 mins , If success you will see your runner in that page .

runner

you can use tag gitlab , runner , awscdk ,

Example gitlab-ci.yaml

gitlab docs see more ...

dockerjob:
  image: docker:18.09-dind
  variables:
  tags:
    - runner
    - awscdk
    - gitlab
  variables:
    DOCKER_TLS_CERTDIR: ""
  before_script:
    - docker info
  script:
    - docker info;
    - echo 'test 123';
    - echo 'hello world 1228'

If your want to debug you can go to aws console

In your runner region !!!

AWS Systems Manager > Session Manager > Start a session

system manager

click your runner and click start session

in the brower console in put bash

# become to root
sudo -i

# list runner container .
root# docker ps -a

# modify gitlab-runner/config.toml

root# cd /home/ec2-user/.gitlab-runner/ && ls
config.toml

:clap: Supporters

Stargazers repo roster for @guan840912/cdk-gitlab-runner

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cdk-gitlab-runner-1.77.26.tar.gz (101.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cdk_gitlab_runner-1.77.26-py3-none-any.whl (98.8 kB view details)

Uploaded Python 3

File details

Details for the file cdk-gitlab-runner-1.77.26.tar.gz.

File metadata

  • Download URL: cdk-gitlab-runner-1.77.26.tar.gz
  • Upload date:
  • Size: 101.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for cdk-gitlab-runner-1.77.26.tar.gz
Algorithm Hash digest
SHA256 57a84ae7e68c66ca628051155dda0757c41160ff8ae433e3dbab7903c667c855
MD5 0ef4b4b1483cdfa8d6deebb0af13937b
BLAKE2b-256 861205bcd739eae8e7acc375d430ec308dc1a58e4c29b969c6ecdf7702201b38

See more details on using hashes here.

File details

Details for the file cdk_gitlab_runner-1.77.26-py3-none-any.whl.

File metadata

  • Download URL: cdk_gitlab_runner-1.77.26-py3-none-any.whl
  • Upload date:
  • Size: 98.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for cdk_gitlab_runner-1.77.26-py3-none-any.whl
Algorithm Hash digest
SHA256 27ba26c910ed016438947c97aabd59ec8fa17c5251b8597f0a16d9a3e7495840
MD5 4ffc9184c19ac10c3c33d9c29aaab388
BLAKE2b-256 01a98b3bd6ad3b8c4ae8c2a1c124db265a1bfc835f1a1db38120e69dd821e94d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page