Using Loft in GitLab CI/CD
When using Loft with GitLab you can use the official loft-ci image as either a base image or directly. See Using the Container Image for details on the available tools. If additional tooling is needed for your CI/CD process, a custom image can be created.
Spaces for Merge Requests
This example shows how to create and delete a Space for running end-to-end tests for the default branch and merge requests. It assumes you have configured CI/CD variables LOFT_URL and LOFT_ACCESS_KEY.
image: loftsh/loft-ci
stages:
- test
e2e:
rules:
- if: $CI_MERGE_REQUEST_IID
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
stage: test
before_script:
- loft login $LOFT_URL --access-key $LOFT_ACCESS_KEY
- loft create space "${CI_PROJECT_NAME}-${CI_COMMIT_SHORT_SHA}-${CI_PIPELINE_ID}"
- apk add go make
script:
- kubectl apply -Rf ./kubernetes
- kubectl rollout status deployments/my-app
- make e2e
after_script:
- loft delete space "${CI_PROJECT_NAME}-${CI_COMMIT_SHORT_SHA}-${CI_PIPELINE_ID}"
Explanation:
- The
loftsh/loft-ciimage is used for all pipeline jobs and provides theloftCLI, thedevspaceCLI, andkubectl. - The
before_scriptfirst logs in to loft using the$LOFT_URLand$LOFT_ACCESS_KEYvariables that you defined in GitLab. See the GitLab docs for more information - The
before_scriptthen creates a space using predefined GitLab variables to create a unique name for the space. - Next
before_scriptinstalls some additional tooling needed to run the end-to-end tests. For more complex scenarios creating a custom image is recommended. - Then the
scriptsection useskubectlto deploy the application to the space and waits for themy-appdeployment to become ready.makeis then used to run an end-to-end test suite. - Finally
after_scriptdeletes the space. By usingafter_scriptwe can ensure the space is deleted even if the tests fail.
Virtual Clusters for Merge Requests
This example shows how to create and delete a Virtual Cluster for running end-to-end tests for the default branch and merge requests. It assumes you have configured CI/CD variables LOFT_URL and LOFT_ACCESS_KEY.
image: loftsh/loft-ci
stages:
- test
e2e:
rules:
- if: $CI_MERGE_REQUEST_IID
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
stage: test
before_script:
- loft login $LOFT_URL --access-key $LOFT_ACCESS_KEY
- loft create vcluster "${CI_PROJECT_NAME}-${CI_COMMIT_SHORT_SHA}-${CI_PIPELINE_ID}"
- apk add go make
script:
- kubectl apply -Rf ./kubernetes
- kubectl rollout status deployments/my-app
- make e2e
after_script:
- loft delete vcluster "${CI_PROJECT_NAME}-${CI_COMMIT_SHORT_SHA}-${CI_PIPELINE_ID}" --delete-space
Explanation:
- The
loftsh/loft-ciimage is used for all pipeline jobs and provides theloftCLI, thedevspaceCLI, andkubectl. - The
before_scriptfirst logs in to loft using the$LOFT_URLand$LOFT_ACCESS_KEYvariables that you defined in GitLab. See the GitLab docs for more information - The
before_scriptthen creates a virtual cluster using predefined GitLab variables to create a unique name. - Next
before_scriptinstalls some additional tooling needed to run the end-to-end tests. For more complex scenarios creating a custom image is recommended. - Then the
scriptsection useskubectlto deploy the application to the space and waits for themy-appdeployment to become ready.makeis then used to run an end-to-end test suite. - Finally
after_scriptdeletes the virtual cluster, and passes--delete-spaceto ensure the corresponding space for the cluster is deleted. By usingafter_scriptwe can ensure the space is deleted even if the tests fail.