Creación de pipeline de Pruebas o Staging
Laboratorio: Creación de pipeline de Pruebas o Staging
Descripción
La presente guía enseña al estudiante el proceso de creación de pipelines como código para Gitlab CI.
Objetivos
- Crear un pipeline que corra a través de Gitlab CI.
Antes de comenzar
- Contar con el acceso al ambiente del laboratorio
- Contar con las herramientas instaladas
Inicio de laboratorio
-
Acceda a la carpeta
angular-democd angular-demo -
Cree una carpeta llamada
ci-templatesmkdir ci-templates -
Cree un archivo llamado
.dev.yamlen la carpetaci-templatescon el contenido actual de.gitlab-ci.ymlcp .gitlab-ci.yml ci-templates/.dev.yaml -
Edite el archivo
ci-templates/.dev.yamlcambiando el nombre de los Jobs agregando-deval finalpackage-dev: sonar-dev: build-image-dev: apply-dev: -
Edite el archivo
ci-templates/.dev.yamlagregando a cada tarea unrulespara verificar la rama1 2 3
package-dev: rules: - if: $CI_COMMIT_BRANCH =~ /dev*/ -
Edite el archivo
.gitlab-ci.ymlborrando su contenido y reemplazando por lo siguiente1 2 3 4 5 6 7 8 9 10
include: - local: 'ci-templates/*.yaml' stages: - package - analyze - build - promote - deploy - test -
Edite el archivo
ci/.dev.yamlagregando el Job depolicy-test1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
policy-test-dev: rules: - if: $CI_COMMIT_BRANCH =~ /dev*/ stage: analyze image: name: openpolicyagent/conftest:v0.32.0 entrypoint: - "" tags: - dev script: - conftest test -p manifests/policy.rego manifests/k8s-template.yaml --output=junit > policy_results.xml artifacts: reports: junit: policy_results.xml -
Agregue el bloque
artifactsal Jobbuild-image1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
build-image-dev: stage: build image: name: gcr.io/kaniko-project/executor:debug entrypoint: [""] rules: - if: $CI_COMMIT_BRANCH =~ /dev*/ tags: - dev artifacts: paths: - .sha-image expire_in: 1 week script: - mkdir -p /kaniko/.docker - | if [ $PROMOTE_SHA == 'yes' ]; then echo $CI_COMMIT_SHORT_SHA > .sha-image fi - echo "{\"auths\":{\"$REGISTRY_URL\":{\"username\":\"$REGISTRY_USER\",\"password\":\"$TOKEN\"}}}" > /kaniko/.docker/config.json - /kaniko/executor --context . --dockerfile docker/Dockerfile --destination ${REGISTRY_URL}/${REGISTRY_PROJECT}/${IMAGE_NAME}:${CI_COMMIT_SHORT_SHA} --skip-tls-verify $KANIKO_ARGS --image-fs-extract-retry 5 - echo ${CI_COMMIT_SHORT_SHA} > .sha-image -
Cree el archivo llamado
policy.regoen la carpetamanifestscon el siguiente contenidopackage main name = input.metadata.name required_deployment_labels { input.metadata.labels["app"] } deny[msg] { input.kind = "Deployment" not required_deployment_labels msg = sprintf("%s must include Kubernetes recommended labels: https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/#labels", [name]) } deny[msg] { input.kind = "Deployment" not input.spec.selector.matchLabels.app msg = "Containers must provide app label for pod selectors" } -
Agregue la siguiente variable
1 2 3 4 5 6 7 8 9 10 11
variables: SONAR_ARGS: "-Dsonar.projectName=${PROJECT_NAME} -Dsonar.projectKey=some.org:${PROJECT_NAME} -Dsonar.host.url=$SONAR_URL -Dsonar.sources=. -Dsonar.login=$SONAR_TOKEN -Dsonar.coverage.exclusions=node_modules/*" PROJECT_NAME: "angular-demo" SONAR_URL: "http://sonarqube.xx-xxx-xx-xx/" REGISTRY_PROJECT: "api-test" IMAGE_NAME: "angular-demo" DEPLOYMENT_NAME: "angular-demo" NAMESPACE: "userx" SERVICE_PORT: "80" INGRESS_HOST: "angular-demo.xx-xxx-xx-xx.nip.io" PROMOTE_SHA: "yes" -
Guarde y suba los cambios
git add . git commit -m "Initial structure" git push -
Revise en el portal de gitlab la ejecución del pipeline
-
Revise los recursos actualizados
kubectl get all,svc,ingress -n userx kubectl describe deploy/angular-demo -n userx -
Cree una rama nueva llamada
qagit checkout -b qa -
Cree el archivo llamado
.qa.yamlen la carpetaci-templatescon el siguiente contenido1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
variables: PROJECT_NAME: "angular-demo" REGISTRY_PROJECT: "api-test" IMAGE_NAME: "angular-demo" DEPLOYMENT_NAME: "angular-demo" NAMESPACE: "userx" SERVICE_PORT: "80" INGRESS_HOST: "angular-demo.x-x-x-x.nip.io" policy-test-qa: rules: - if: $CI_COMMIT_BRANCH =~ /qa*/ stage: analyze image: name: openpolicyagent/conftest:v0.32.0 entrypoint: - "" tags: - qa script: - conftest test -p manifests/policy.rego manifests/k8s-template.yaml --output=junit > policy_results.xml artifacts: reports: junit: policy_results.xml -
Agregue el Job de
promote-image1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
variables: PROJECT_NAME: "angular-demo" REGISTRY_PROJECT: "apis" IMAGE_NAME: "angular-demo" DEPLOYMENT_NAME: "angular-demo" NAMESPACE: "userx" SERVICE_PORT: "80" INGRESS_HOST: "angular-demo.x-x-x-x.nip.io" policy-test-qa: rules: - if: $CI_COMMIT_BRANCH =~ /qa*/ stage: analyze image: name: openpolicyagent/conftest:v0.32.0 entrypoint: - "" tags: - qa script: - conftest test -p manifests/policy.rego manifests/k8s-template.yaml --output=junit > policy_results.xml artifacts: reports: junit: policy_results.xml promote-image-qa: rules: - if: $CI_COMMIT_BRANCH =~ /qa*/ stage: promote tags: - qa image: name: quay.io/skopeo/stable:v1.20.0 entrypoint: ["/bin/bash", "-c"] script: - | echo ${PROJECT_TOKEN} echo $CI_API_V4_URL echo $CI_PROJECT_ID curl -o .sha-image --header "PRIVATE-TOKEN: ${PROJECT_TOKEN}" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/jobs/artifacts/dev/raw/.sha-image?job=build-image-dev" LAST_COMMIT=$(cat .sha-image) skopeo login -u "$REGISTRY_USER" --password "$TOKEN" $REGISTRY_URL --tls-verify=$DEST_TLS_VERIFY LAST_IMAGE=$(echo $REGISTRY_URL/$REGISTRY_PROJECT/$IMAGE_NAME):${LAST_COMMIT} echo $REGISTRY_URL/$REGISTRY_PROJECT/$IMAGE_NAME:${LAST_COMMIT} skopeo copy docker://$LAST_IMAGE docker://$REGISTRY_URL/$REGISTRY_PROJECT/$IMAGE_NAME:$CI_COMMIT_SHORT_SHA --src-tls-verify=${SRC_TLS_VERIFY} --dest-tls-verify=${DEST_TLS_VERIFY} -
Agregue el Job de
apply1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
apply-qa: rules: - if: $CI_COMMIT_BRANCH =~ /qa*/ stage: deploy image: quay.io/itmlabs/kubectl-tools:v0.0.1 tags: - qa script: - | echo "[INFO] Reemplazando variables en archivo template" envsubst < manifests/k8s-template.yaml > k8s-resources.yaml echo "[INFO] Recursos creados con valores substituidos" cat k8s-resources.yaml echo "[INFO] Desplegando aplicación" kubectl apply -f k8s-resources.yaml -
Agregue las siguientes variables
1 2 3 4 5 6 7 8 9 10
variables: PROJECT_NAME: "angular-demo" REGISTRY_PROJECT: "api-test" IMAGE_NAME: "angular-demo" DEPLOYMENT_NAME: "angular-demo" NAMESPACE: "userx" SERVICE_PORT: "80" INGRESS_HOST: "angular-demo.x-x-x-x.nip.io" SRC_TLS_VERIFY: "false" DEST_TLS_VERIFY: "false" -
Genere un
Access Tokende su cuenta de Gitlab con permisos sobre api y write repo y guarde como Variable en el repositorioVariable Type Value Mask PROJECT_TOKEN Variable token de gitlab asociado a la cuenta -
Guarde y suba los cambios
git add . git commit -m "Add qa pipeline" git push --set-upstream origin qa -
Revise en el portal de gitlab la ejecución del pipeline