diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1affdae94ef8f9ef5eeac0cddcdbde84fff5f66e..0f538378dbb4545c0f3dffcf6e3fa399d2737cff 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,6 +1,11 @@ variables: CI_TEMPLATE_REGISTRY_HOST: registry.gitlab.inria.fr - TF_STATE_NAME: default + TF_STATE_NAME: pipeline-$CI_PIPELINE_ID + TF_VAR_CI_PIPELINE_ID: $CI_PIPELINE_ID + +workflow: + rules: + - if: $CLOUDSTACK_API_KEY include: - template: Terraform/Base.gitlab-ci.yml # https://gitlab.com/gitlab-org/gitlab/blob/master/lib/gitlab/ci/templates/Terraform/Base.gitlab-ci.yml @@ -32,9 +37,7 @@ deploy: - small script: - cp $SSH_PRIVATE_KEY id_rsa - - gitlab-terraform plan -var runner_count=0 - - gitlab-terraform apply - - gitlab-terraform plan -var runner_count=3 + - gitlab-terraform plan - gitlab-terraform apply execute: @@ -43,9 +46,12 @@ execute: parallel: matrix: - index: [0, 1, 2] + variables: + PIPELINE_ID: $CI_PIPELINE_ID tags: - terraform - docker + - pipeline-$PIPELINE_ID - runner-$index script: - echo Greetings from runner $index! @@ -58,6 +64,6 @@ cleanup: script: - cd "${TF_ROOT}" - cp $SSH_PRIVATE_KEY id_rsa - - gitlab-terraform plan -var runner_count=0 - - gitlab-terraform apply + - gitlab-terraform destroy + - 'curl --user "gitlab-ci-token:$CI_JOB_TOKEN" --request DELETE "$CI_API_V4_URL/projects/$CI_PROJECT_ID/terraform/state/$TF_STATE_NAME"' when: always diff --git a/cloud-init.sh.tftpl b/cloud-init.sh.tftpl index 2b4d6de68d30eee4e159f0a6d028bdf6fed2e973..33f0670d809500b8ca12cba3b7c0b3a604795fc0 100644 --- a/cloud-init.sh.tftpl +++ b/cloud-init.sh.tftpl @@ -16,7 +16,7 @@ # We install docker.io to be able to register a docker executor apt-get install --yes gitlab-runner docker.io gitlab-runner register --non-interactive \ - --tag-list terraform,docker,runner-${index} \ + --tag-list terraform,docker,pipeline-${CI_PIPELINE_ID},runner-${index} \ --executor docker --docker-image alpine --url https://gitlab.inria.fr \ --registration-token ${REGISTRATION_TOKEN} ) >>/root/log.txt 2>&1 diff --git a/main.tf b/main.tf index 7683dcfb4c5448845fb9659ca0863208fca2b191..2a5fa55f37193f8788880834c8173683410f2e42 100644 --- a/main.tf +++ b/main.tf @@ -24,12 +24,12 @@ variable "SSH_PUBLIC_KEY" { type = string } -variable "runner_count" { +variable "CI_PIPELINE_ID" { type = number } resource "cloudstack_instance" "runner" { - count = var.runner_count + count = 3 name = "gitlabcigallery-terraform-runner-${count.index}" service_offering = "Custom" template = "ubuntu-20.04-lts" @@ -43,6 +43,7 @@ resource "cloudstack_instance" "runner" { index = count.index REGISTRATION_TOKEN = var.REGISTRATION_TOKEN SSH_PUBLIC_KEY = var.SSH_PUBLIC_KEY + CI_PIPELINE_ID = var.CI_PIPELINE_ID }) connection { type = "ssh"