diff --git a/documentation/azure-devops/setup-deploy-pipeline.asciidoc b/documentation/azure-devops/setup-deploy-pipeline.asciidoc index 2d77b3284..f91c49785 100644 --- a/documentation/azure-devops/setup-deploy-pipeline.asciidoc +++ b/documentation/azure-devops/setup-deploy-pipeline.asciidoc @@ -1,3 +1,8 @@ +:provider: Azure Devops +:pipeline_type: Pipeline +:trigger_sentence_azure: +:pipeline_type2: pipeline +:path_provider: azure-devops = Setting up a Deploy Pipeline on Azure DevOps In this section we will create a deploy pipeline on Azure DevOps to deploy the project application on an already provisioned Kubernetes cluster. This pipeline will be configured in order to be triggered every time package pipeline is executed successfully on a commit for `release/*` and `develop` branches, requiring manual launch for other branches but still enforcing that package pipeline has passed. By default, it depends on the environment provisioning pipeline being successfully run on beforehand and, depending on the Kubernetes provider, it consumes the artifact produced by that. It also consumes variable groups created by package and environment provisioning pipelines. @@ -23,10 +28,10 @@ pipeline_generator.sh \ -n \ -d \ --package-pipeline-name \ - --env-provision-pipeline-name ] \ + --env-provision-pipeline-name \ --k8s-provider \ --k8s-namespace \ - --k8s-deploy-files-path \ + --k8s-deploy-files-path \ [--k8s-image-pull-secret-name ] \ [-b ] \ [-w] @@ -51,7 +56,8 @@ NOTE: The config file for the deploy pipeline is located at `/scripts/pipelines === Examples ``` -./pipeline_generator.sh -c ./templates/deploy/deploy-pipeline.cfg -n quarkus-project-deploy -d C:/Users/$USERNAME/Desktop/quarkus-project --package-pipeline-name quarkus-project-package --env-provision-pipeline-name eks-provisioning --k8s-provider EKS --k8s-namespace hangar --k8s-deploy-files-path k8s -b develop -w + ./pipeline_generator.sh -c ./templates/deploy/deploy-pipeline.cfg -n quarkus-project-deploy -d C:/Users/$USERNAME/Desktop/quarkus-project --package-pipeline-name quarkus-project-package --env-provision-pipeline-name eks-provisioning --k8s-provider EKS --k8s-namespace hangar --k8s-deploy-files-path k8s -b develop -w + ``` diff --git a/documentation/gitlab/setup-deploy-pipeline.asciidoc b/documentation/gitlab/setup-deploy-pipeline.asciidoc new file mode 100644 index 000000000..ddebff7e8 --- /dev/null +++ b/documentation/gitlab/setup-deploy-pipeline.asciidoc @@ -0,0 +1,64 @@ +:provider: Gitlab +:pipeline_type: pipeline +:trigger_sentence_gitlab: +:pipeline_type2: Gitlab pipeline +:path_provider: gitlab += Setting up a Deploy Pipeline on Azure DevOps + +In this section we will create a deploy pipeline on Azure DevOps to deploy the project application on an already provisioned Kubernetes cluster. This pipeline will be configured in order to be triggered every time package pipeline is executed successfully on a commit for `release/*` and `develop` branches, requiring manual launch for other branches but still enforcing that package pipeline has passed. By default, it depends on the environment provisioning pipeline being successfully run on beforehand and, depending on the Kubernetes provider, it consumes the artifact produced by that. It also consumes variable groups created by package and environment provisioning pipelines. + +The creation of the pipeline will follow the project workflow, so a new branch named `feature/deploy-pipeline` will be created and the YAML file for the pipeline will be pushed to it. + +Then, a Pull Request (PR) will be created in order to merge the new branch into the appropriate branch (provided in `-b` flag). The PR will be automatically merged if the repository policies are met. If the merge is not possible, either the PR URL will be shown as output, or it will be opened in your web browser if using `-w` flag. + +The script located at `/scripts/pipelines/azure-devops/pipeline_generator.sh` will automatically create the new branch, create a deploy pipeline based on a YAML template appropriate for the project manifests files, create the Pull Request, and if it is possible, merge this new branch into the specified branch. + +== Prerequisites + +* This script will commit and push the corresponding YAML template into your repository, so please be sure your local repository is up-to-date (i.e you have pulled the latest changes with `git pull`). + +* The YAML containing Deployment object should be named `application-deployment.yaml` for allowing automated `imagePullSecret` injection. + +== Creating the pipeline using provided script + +=== Usage +``` +pipeline_generator.sh \ + -c \ + -d \ + --k8s-provider \ + --k8s-namespace \ + --k8s-deploy-files-path \ + [--k8s-image-pull-secret-name ] \ + [-b ] \ + [-w] +``` +NOTE: The config file for the deploy pipeline is located at `/scripts/pipelines/azure-devops/templates/deploy/deploy-pipeline.cfg`. + +=== Flags +``` +-c --config-file [Required] Configuration file containing pipeline definition. +-d --local-directory [Required] Local directory of your project. + --k8s-provider [Required] Kubernetes cluster provider name. Accepted values: EKS, AKS. + --k8s-namespace [Required] Kubernetes namespace where the application will be deployed. + --k8s-deploy-files-path [Required] Path from the root of the project to the YAML manifests directory. + --k8s-image-pull-secret-name Name for the generated secret containing registry credentials. Required when using a private registry to host images. +-b --target-branch Name of the branch to which the Pull Request will target. PR is not created if the flag is not provided. +-w Open the Pull Request on the web browser if it cannot be automatically merged. Requires -b flag. +``` + +=== Examples + +``` + + ./pipeline_generator.sh -c ./templates/deploy/deploy-pipeline.cfg -d C:/Users/$USERNAME/Desktop/quarkus-project --k8s-provider EKS --k8s-namespace hangar --k8s-deploy-files-path k8s -b develop -w +``` + + +=== Appendix: accessing the application + +Once the pipeline is executed and your application is deployed, you can list the hostname to access it with: + +``` +kubectl get ingress -n +``` diff --git a/documentation/src/azure-devops/setup-deploy-pipeline.asciidoc b/documentation/src/azure-devops/setup-deploy-pipeline.asciidoc new file mode 100644 index 000000000..a3f1477e4 --- /dev/null +++ b/documentation/src/azure-devops/setup-deploy-pipeline.asciidoc @@ -0,0 +1,6 @@ +:provider: Azure Devops +:pipeline_type: Pipeline +:trigger_sentence_azure: +:pipeline_type2: pipeline +:path_provider: azure-devops +include::../common_templates/setup-deploy-pipeline.asciidoc[] diff --git a/documentation/src/common_templates/setup-deploy-pipeline.asciidoc b/documentation/src/common_templates/setup-deploy-pipeline.asciidoc new file mode 100644 index 000000000..966abcdac --- /dev/null +++ b/documentation/src/common_templates/setup-deploy-pipeline.asciidoc @@ -0,0 +1,66 @@ += Setting up a Deploy Pipeline on Azure DevOps + +In this section we will create a deploy pipeline on Azure DevOps to deploy the project application on an already provisioned Kubernetes cluster. This pipeline will be configured in order to be triggered every time package pipeline is executed successfully on a commit for `release/*` and `develop` branches, requiring manual launch for other branches but still enforcing that package pipeline has passed. By default, it depends on the environment provisioning pipeline being successfully run on beforehand and, depending on the Kubernetes provider, it consumes the artifact produced by that. It also consumes variable groups created by package and environment provisioning pipelines. + +The creation of the pipeline will follow the project workflow, so a new branch named `feature/deploy-pipeline` will be created and the YAML file for the pipeline will be pushed to it. + +Then, a Pull Request (PR) will be created in order to merge the new branch into the appropriate branch (provided in `-b` flag). The PR will be automatically merged if the repository policies are met. If the merge is not possible, either the PR URL will be shown as output, or it will be opened in your web browser if using `-w` flag. + +The script located at `/scripts/pipelines/azure-devops/pipeline_generator.sh` will automatically create the new branch, create a deploy pipeline based on a YAML template appropriate for the project manifests files, create the Pull Request, and if it is possible, merge this new branch into the specified branch. + +== Prerequisites + +* This script will commit and push the corresponding YAML template into your repository, so please be sure your local repository is up-to-date (i.e you have pulled the latest changes with `git pull`). + +* The YAML containing Deployment object should be named `application-deployment.yaml` for allowing automated `imagePullSecret` injection. + +== Creating the pipeline using provided script + +=== Usage +``` +pipeline_generator.sh \ + -c \ +ifdef::trigger_sentence_azure,trigger_sentence_github[ -n \] + -d \ +ifdef::trigger_sentence_azure,trigger_sentence_github[ --package-pipeline-name \] +ifdef::trigger_sentence_azure,trigger_sentence_github[ --env-provision-pipeline-name \] + --k8s-provider \ + --k8s-namespace \ + --k8s-deploy-files-path \ + [--k8s-image-pull-secret-name ] \ + [-b ] \ + [-w] +``` +NOTE: The config file for the deploy pipeline is located at `/scripts/pipelines/azure-devops/templates/deploy/deploy-pipeline.cfg`. + +=== Flags +``` +-c --config-file [Required] Configuration file containing pipeline definition. +ifdef::trigger_sentence_azure,trigger_sentence_github[-n --pipeline-name [Required] Name that will be set to the pipeline.] +-d --local-directory [Required] Local directory of your project. +ifdef::trigger_sentence_azure,trigger_sentence_github[ --package-pipeline-name [Required] Package pipeline name.] +ifdef::trigger_sentence_azure,trigger_sentence_github[ --env-provision-pipeline-name [Required] Environment provisioning pipeline name.] + --k8s-provider [Required] Kubernetes cluster provider name. Accepted values: EKS, AKS. + --k8s-namespace [Required] Kubernetes namespace where the application will be deployed. + --k8s-deploy-files-path [Required] Path from the root of the project to the YAML manifests directory. + --k8s-image-pull-secret-name Name for the generated secret containing registry credentials. Required when using a private registry to host images. +-b --target-branch Name of the branch to which the Pull Request will target. PR is not created if the flag is not provided. +-w Open the Pull Request on the web browser if it cannot be automatically merged. Requires -b flag. +``` + +=== Examples + +``` +ifdef::trigger_sentence_azure,trigger_sentence_github[ ./pipeline_generator.sh -c ./templates/deploy/deploy-pipeline.cfg -n quarkus-project-deploy -d C:/Users/$USERNAME/Desktop/quarkus-project --package-pipeline-name quarkus-project-package --env-provision-pipeline-name eks-provisioning --k8s-provider EKS --k8s-namespace hangar --k8s-deploy-files-path k8s -b develop -w] + +ifdef::trigger_sentence_gitlab[ ./pipeline_generator.sh -c ./templates/deploy/deploy-pipeline.cfg -d C:/Users/$USERNAME/Desktop/quarkus-project --k8s-provider EKS --k8s-namespace hangar --k8s-deploy-files-path k8s -b develop -w] +``` + + +=== Appendix: accessing the application + +Once the pipeline is executed and your application is deployed, you can list the hostname to access it with: + +``` +kubectl get ingress -n +``` diff --git a/documentation/src/gitlab/setup-deploy-pipeline.asciidoc b/documentation/src/gitlab/setup-deploy-pipeline.asciidoc new file mode 100644 index 000000000..1a47ae966 --- /dev/null +++ b/documentation/src/gitlab/setup-deploy-pipeline.asciidoc @@ -0,0 +1,6 @@ +:provider: Gitlab +:pipeline_type: pipeline +:trigger_sentence_gitlab: +:pipeline_type2: Gitlab pipeline +:path_provider: gitlab +include::../common_templates/setup-deploy-pipeline.asciidoc[] diff --git a/scripts/pipelines/common/pipeline_generator.lib b/scripts/pipelines/common/pipeline_generator.lib index 5ac0473f0..bc6d7c67b 100644 --- a/scripts/pipelines/common/pipeline_generator.lib +++ b/scripts/pipelines/common/pipeline_generator.lib @@ -73,7 +73,6 @@ function help { echo " --k8s-deploy-files-path [Required] Path from the root of the project to the YAML manifests directory." echo " --k8s-image-pull-secret-name Name for the generated secret containing registry credentials. Required when using a private registry to host images." echo "" - exit } diff --git a/scripts/pipelines/gitlab/pipeline_generator.sh b/scripts/pipelines/gitlab/pipeline_generator.sh new file mode 100644 index 000000000..352234689 --- /dev/null +++ b/scripts/pipelines/gitlab/pipeline_generator.sh @@ -0,0 +1,188 @@ +#!/bin/bash +set -e +FLAGS=$(getopt -a --options c:n:d:a:b:l:i:u:p:hw --long "config-file:,pipeline-name:,local-directory:,artifact-path:,target-branch:,language:,build-pipeline-name:,sonar-url:,sonar-token:,image-name:,registry-user:,registry-password:,storage-container:,cluster-name:,s3-bucket:,s3-key-path:,quality-pipeline-name:,dockerfile:,test-pipeline-name:,aws-access-key:,aws-secret-access-key:,aws-region:,package-pipeline-name:,env-provision-pipeline-name:,k8s-provider:,k8s-namespace:,k8s-deploy-files-path:,k8s-image-pull-secret-name:,help,rancher" -- "$@") + +eval set -- "$FLAGS" +while true; do + case "$1" in + -c | --config-file) configFile=$2; shift 2;; + -n | --pipeline-name) export pipelineName=$2; shift 2;; + -d | --local-directory) localDirectory=$2; shift 2;; + -a | --artifact-path) artifactPath=$2; shift 2;; + -b | --target-branch) targetBranch=$2; shift 2;; + -l | --language) language=$2; shift 2;; + --build-pipeline-name) export buildPipelineName=$2; shift 2;; + --sonar-url) sonarUrl=$2; shift 2;; + --sonar-token) sonarToken=$2; shift 2;; + -i | --image-name) imageName=$2; shift 2;; + -u | --registry-user) dockerUser=$2; shift 2;; + -p | --registry-password) dockerPassword=$2; shift 2;; + --storage-container) storageContainerName=$2; shift 2;; + --rancher) installRancher="true"; shift 1;; + --cluster-name) clusterName=$2; shift 2;; + --s3-bucket) s3Bucket=$2; shift 2;; + --s3-key-path) s3KeyPath=$2; shift 2;; + --quality-pipeline-name) export qualityPipelineName=$2; shift 2;; + --test-pipeline-name) export testPipelineName=$2; shift 2;; + --dockerfile) dockerFile=$2; shift 2;; + --aws-access-key) awsAccessKey="$2"; shift 2;; + --aws-secret-access-key) awsSecretAccessKey="$2"; shift 2;; + --aws-region) awsRegion="$2"; shift 2;; + --package-pipeline-name) export packagePipelineName=$2; shift 2;; + --env-provision-pipeline-name) envProvisionPipelineName="$2"; shift 2;; + --k8s-provider) k8sProvider=$2; shift 2;; + --k8s-namespace) k8sNamespace="$2"; shift 2;; + --k8s-deploy-files-path) k8sDeployFiles=$2; shift 2;; + --k8s-image-pull-secret-name) k8sImagePullSecret=$2; shift 2;; + -h | --help) help="true"; shift 1;; + -w) webBrowser="true"; shift 1;; + --) shift; break;; + esac +done + +# Colours for the messages. +white='\e[1;37m' +green='\e[1;32m' +red='\e[0;31m' + +# Common var +commonTemplatesPath="scripts/pipelines/gitlab/templates/common" # Path for common files of the pipelines +pipelinePath=".pipelines" # Path to the pipelines. +scriptFilePath=".pipelines/scripts" # Path to the scripts. +gitlabCiFile=".gitlab-ci.yml" +export provider="gitlab" + +function obtainHangarPath { + + # This line goes to the script directory independent of wherever the user is and then jumps 3 directories back to get the path + hangarPath=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && cd ../../.. && pwd ) +} + +function addAdditionalArtifact { + # Check if an extra artifact to store is supplied. + if test ! -z "$artifactPath" + then + # Add the extra step to the YAML. + grep " artifacts:" "${localDirectory}/${pipelinePath}/${yamlFile}" > /dev/null && storeExtraPathContent=" - \"$artifactPath\"" + grep " artifacts:" "${localDirectory}/${pipelinePath}/${yamlFile}" > /dev/null || storeExtraPathContent="\n artifacts:\n paths:\n - \"$artifactPath\"" + sed -i "s/# mark to insert step for additonal artifact #/$storeExtraPathContent\n/" "${localDirectory}/${pipelinePath}/${yamlFile}" + else + echo "The '-a' flag has not been set, skipping the step to add additional artifact." + sed -i '/# mark to insert step for additonal artifact #/d' "${localDirectory}/${pipelinePath}/${yamlFile}" + fi +} + +# Function that adds the variables to be used in the pipeline. +function addCommonPipelineVariables { + if test -z "${artifactPath}" + then + echo "Skipping creation of the variable artifactPath as the flag has not been used." + # Delete the commentary to set the artifactPath input/var + sed -i '/# mark to insert additional artifact env var #/d' "${localDirectory}/${pipelinePath}/${yamlFile}" + else + # add the input for the additional artifact + grep "variables:" "${localDirectory}/${pipelinePath}/${yamlFile}" > /dev/null && textArtifactPathVar=" artifactPath: ${artifactPath//\//\\/}" + grep "variables:" "${localDirectory}/${pipelinePath}/${yamlFile}" > /dev/null || textArtifactPathVar="variables:\n artifactPath: \"${artifactPath//\//\\/}\"" + sed -i "s/# mark to insert additional artifact env var #/$textArtifactPathVar/" "${localDirectory}/${pipelinePath}/${yamlFile}" + fi +} + +function addCiFile { + echo -e "${green}Copying and commiting the gitlab ci file." + echo -ne ${white} + + cp "${hangarPath}/${commonTemplatesPath}/${gitlabCiFile}" "${localDirectory}/${gitlabCiFile}" + testCommit=$(git status) + if echo "$testCommit" | grep "nothing to commit, working tree clean" > /dev/null + then + echo "gilab-ci file already present with same content, nothing to commit." + else + git add "${gitlabCiFile}" -f + git commit -m "adding gitlab-ci.yml" + git push + fi +} + +function createPR { + # Check if a target branch is supplied. + if test -z "$targetBranch" + then + # No branch specified in the parameters, no Pull Request is created, the code will be stored in the current branch. + echo -e "${green}No branch specified to do the Pull Request, changes left in the ${sourceBranch} branch." + exit + else + echo -e "${green}Creating a Pull Request..." + echo -ne "${white}" + repoURL=$(git config --get remote.origin.url) + repoNameWithGit="${repoURL/https:\/\/gitlab.com\/}" + repoName="${repoNameWithGit/.git}" + # Create the Pull Request to merge into the specified branch. + #debug + echo "glab mr create -b \"$targetBranch\" -d \"merge request $sourceBranch\" -s \"$sourceBranch\" -H \"${repoName}\" -t \"merge $sourceBranch\"" + pr=$(glab mr create -b "$targetBranch" -d "merge request $sourceBranch" -s "$sourceBranch" -H "${repoName}" -t "merge $sourceBranch") + + # Create merge approval to the pull request. + glab mr approvers "$sourceBranch" -R "${repoName}" + + #trying to merge + if glab mr merge -s $(basename "$pr") -y + then + # Pull Request merged successfully. + echo -e "${green}Pull Request merged into $targetBranch branch successfully." + exit + else + # Check if the -w flag is activated. + if [[ "$webBrowser" == "true" ]] + then + # -w flag is activated and a page with the corresponding Pull Request is opened in the web browser. + echo -e "${green}Pull Request successfully created." + echo -e "${green}Opening the Pull Request on the web browser..." + python -m webbrowser "$pr" + exit + else + # -w flag is not activated and the URL to the Pull Request is shown in the console. + echo -e "${green}Pull Request successfully created." + echo -e "${green}To review the Pull Request and accept it, click on the following link:" + echo "${pr}" + exit + fi + fi + fi +} + + +obtainHangarPath + +# Load common functions +. "$hangarPath/scripts/pipelines/common/pipeline_generator.lib" + +if [[ "$help" == "true" ]]; then help; fi + +ensurePathFormat + +importConfigFile + +checkInstallations + +createNewBranch + +type addPipelineVariables &> /dev/null && addPipelineVariables + +copyYAMLFile + +addAdditionalArtifact + +copyCommonScript + +type copyScript &> /dev/null && copyScript + +# This function does not exists for the github pipeline generator at this moment, but I let the line with 'type' to keep the same structure as the others pipeline generator +type addCommonPipelineVariables &> /dev/null && addCommonPipelineVariables + +commitCommonFiles + +type commitFiles &> /dev/null && commitFiles + +addCiFile + +createPR diff --git a/scripts/pipelines/gitlab/templates/common/.gitlab-ci.yml b/scripts/pipelines/gitlab/templates/common/.gitlab-ci.yml new file mode 100644 index 000000000..4f5b09c04 --- /dev/null +++ b/scripts/pipelines/gitlab/templates/common/.gitlab-ci.yml @@ -0,0 +1,19 @@ +include: + - '.pipelines/*.yml' + +workflow: + rules: + - if: '$CI_PIPELINE_SOURCE == "web"' + when: always + - when: never + +# stages: +# - build +# - test +# - quality +# - package +# - deploy + +# default: +# image: maven:3-jdk-11 +# tags: ['docker_ruby'] diff --git a/scripts/pipelines/gitlab/templates/deploy/deploy-pipeline.cfg b/scripts/pipelines/gitlab/templates/deploy/deploy-pipeline.cfg new file mode 100644 index 000000000..fd0233178 --- /dev/null +++ b/scripts/pipelines/gitlab/templates/deploy/deploy-pipeline.cfg @@ -0,0 +1,29 @@ +# List of the flages we used in different stages for deploy pipeline. +# Comma separated list of flags. Make sure to end with a comma, Eg: $name,$xyz, +mandatoryFlags="$localDirectory,$k8sProvider,$k8sNamespace,$k8sDeployFiles," +# Path to the templates. +templatesPath="scripts/pipelines/gitlab/templates/deploy" +# YAML file name. +yamlFile="deploy-pipeline.yml" +# Source branch. +sourceBranch="feature/deploy-pipeline" + +# Function that copies the extra yaml files into the directory. +function copyScript { + # Copy the deploy script. + cp "${hangarPath}/${templatesPath}/deploy.sh" "${localDirectory}/${scriptFilePath}" + + # Copy the script for generating imagePullSecrets for private registries. + cp "${hangarPath}/${templatesPath}/secrets.sh" "${localDirectory}/${scriptFilePath}" +} + +function addPipelineVariables { + # We cannot use a variable in the definition of resource in the pipeline so we have to use a placeholder to replace it with the value we need + # export packagePipelineName + # export envProvisionPipelineName + export k8sProvider + export k8sNamespace + export k8sDeployFiles + export k8sImagePullSecret + specificEnvSubstList='${k8sProvider} ${k8sNamespace} ${k8sDeployFiles} ${k8sImagePullSecret}' +} diff --git a/scripts/pipelines/gitlab/templates/deploy/deploy-pipeline.yml.template b/scripts/pipelines/gitlab/templates/deploy/deploy-pipeline.yml.template new file mode 100644 index 000000000..178f53eb9 --- /dev/null +++ b/scripts/pipelines/gitlab/templates/deploy/deploy-pipeline.yml.template @@ -0,0 +1,79 @@ +default: + image: ubuntu:latest + +workflow: + rules: + - if: '$CI_PIPELINE_SOURCE == "web"' + when: always + - when: never + +.Prerequisites_install: &Prerequisites_install + before_script: + - apt-get update + - apt-get install sudo -y + - apt-get install -y wget + - apt-get install curl -y + - apt-get install zip -y + # INSTALL yq + - wget -qO /usr/local/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 + - chmod a+x /usr/local/bin/yq + # INSTALL KUBECTL + - curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" + - chmod +x ./kubectl + - mv ./kubectl /usr/local/bin/kubectl + # INSTALL AWS CLI + - curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" + - unzip awscliv2.zip + - sudo ./aws/install + +variables: + K8SPROVIDER: + value: "$k8sProvider" + description: 'Deploy Cluster Name' + SECRETSNAME: + value: "$k8sImagePullSecret" + description: "ImagePullSecret Name" + deploymentYamlPath: "$CI_PROJECT_DIR/$k8sDeployFiles/application-deployment.yaml" + ingressYamlPath: "$CI_PROJECT_DIR/$k8sDeployFiles/ingress.yaml" + deployFilesPath: "$CI_PROJECT_DIR/$k8sDeployFiles" + packageTagScript: ".pipelines/scripts/package-extra.sh" + +Create secrets AKS: + <<: *Prerequisites_install + script: + - export KUBECONFIG=${KUBECONFIG} + - chmod +x .pipelines/scripts/secrets.sh + - .pipelines/scripts/secrets.sh "$k8sNamespace" "$k8sImagePullSecret" "$docker_username" "$docker_password" "$registry" "$deploymentYamlPath" + rules: + - if: '$SECRETSNAME != "" && $K8SPROVIDER == "AKS"' + when: always + +Deploy app AKS: + <<: *Prerequisites_install + script: + - export KUBECONFIG=${KUBECONFIG} + - chmod +x .pipelines/scripts/deploy.sh + - .pipelines/scripts/deploy.sh "$k8sNamespace" "$imageName" "$aks_dns_name" "$deploymentYamlPath" "$ingressYamlPath" "$deployFilesPath" "$packageTagScript" "$branch" + rules: + - if: '$K8SPROVIDER == "AKS"' + when: always + +Create secrets EKS: + <<: *Prerequisites_install + script: + - aws eks update-kubeconfig --name ${CLUSTER_NAME} --region ${AWS_REGION} + - chmod +x .pipelines/scripts/secrets.sh + - .pipelines/scripts/secrets.sh "$k8sNamespace" "$k8sImagePullSecret" "$aws_access_key" "$aws_secret_access_key" "$registry" "$deploymentYamlPath" + rules: + - if: '$SECRETSNAME != "" && $K8SPROVIDER == "EKS"' + when: always + +Deploy app EKS: + <<: *Prerequisites_install + script: + - aws eks update-kubeconfig --name ${CLUSTER_NAME} --region ${AWS_REGION} + - chmod +x .pipelines/scripts/deploy.sh + - .pipelines/scripts/deploy.sh "$k8sNamespace" "$imageName" "$eks_dns_name" "$deploymentYamlPath" "$ingressYamlPath" "$deployFilesPath" "$packageTagScript" "$branch" + rules: + - if: '$K8SPROVIDER == "EKS"' + when: always diff --git a/scripts/pipelines/gitlab/templates/deploy/deploy.sh b/scripts/pipelines/gitlab/templates/deploy/deploy.sh new file mode 100644 index 000000000..852adda8e --- /dev/null +++ b/scripts/pipelines/gitlab/templates/deploy/deploy.sh @@ -0,0 +1,21 @@ +#!/bin/bash +set -e +# Add image name, dns_name and tag. +# Source package-extra.sh to get ${tag} +. "$7" +# we get what is located after the last '/' in the branch name, so it removes /ref/head or /ref/head/ if your branche is named correctly" +branch_short=$(echo "$8" | awk -F '/' '{ print $NF }') + +# We change the name of the tag depending if it is a release or another branch +echo "tag_completed: $8" | grep release && tag_completed="${tag}" +echo "tag_completed_branch: $8" | grep release || tag_completed="${tag}_${branch_short}" + +export image="$2" tag_completed="${tag_completed}" dns="$3" +yq eval '.spec.template.spec.containers[0].image = "'"$image:$tag_completed"'"' -i "$4" +yq eval '.spec.rules[0].host = "'"$dns"'"' -i "$5" +# Create namespace if not exists +kubectl get namespace | grep -q "^$1" || kubectl create namespace "$1" +# Apply manifest files +kubectl apply -f "$6" --namespace="$1" +# Rollout deployments in the namespace +kubectl rollout restart deploy --namespace="$1" diff --git a/scripts/pipelines/gitlab/templates/deploy/secrets.sh b/scripts/pipelines/gitlab/templates/deploy/secrets.sh new file mode 100644 index 000000000..7efd569c1 --- /dev/null +++ b/scripts/pipelines/gitlab/templates/deploy/secrets.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -e +# Create namespace if not exists +kubectl get namespace | grep -q "^$1" || kubectl create namespace "$1" +# Fill imagePullSecrets +export secrets="$2" +yq e '.spec.template.spec."imagePullSecrets"=[{"name":"secrets"}]' -i "$6" +yq e '.spec.template.spec.imagePullSecrets[0].name = "'"$secrets"'"' -i "$6" +# Apply the changes. +# Create imagePullSecret if not exists +kubectl create secret docker-registry "$2" --docker-server="$5" --docker-username="$3" --docker-password="$4" --namespace="$1" --save-config --dry-run=client -o yaml | kubectl apply -f - --namespace="$1" \ No newline at end of file