如何在 gitlab-ci.yml 文件的“脚本"部分中设置变量 [英] How to set variable within 'script' section of gitlab-ci.yml file

查看:20
本文介绍了如何在 gitlab-ci.yml 文件的“脚本"部分中设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据提交的分支为我的 GitLab Runner 设置环境变量.

I am trying to set an environment variable for my GitLab Runner based on the branch that the commit originated from.

我有 4 个 kubernetes 集群:staging、integration、production 和 qa.本质上,我想根据我推送到的分支将我的应用程序部署到适当的集群.

I have 4 kubernetes clusters: staging, integration, production, and qa. Essentially I want to deploy my application to the proper cluster based on the branch I am pushing to.

image: google/cloud-sdk:latest
variables:
DOCKER_HOST: tcp://docker:2375/
DOCKER_DRIVER: overlay2
services:
  - docker:dind
before_script:
  - docker info
stages:
  - publish
publish:
stage: publish
script:
  - if [ "$CI_COMMIT_REF_NAME" = "master" ]; then $ENVIRONMENT="production"; else $ENVIRONMENT="$CI_COMMIT_REF_NAME"; fi
  - echo $ENVIRONMENT
   .
   .
   .
  - kubectl apply -f cfg/${ENVIRONMENT}/web-deployment.yaml
only:
 - master
 - integration
 - qa
 - staging

每当我使用不同形式的 if 语句运行脚本时,我都会收到以下错误:

Any time I run my script with a different form of the if statement I get the following error:

/bin/bash: line 83: =integration: command not found
ERROR: Job failed: exit code 1

所以据我所知,正在设置变量,但脚本退出了.我已经看到了几个与此问题相关的 SO 问题,但没有关于如何设置变量然后继续执行脚本.我该如何解决这个问题?

So from what I can tell the variable is being set, but the script exits. I've seen several SO questions related to this problem, but nothing about how to set a variable and then continue a script. How can I fix this issue?

推荐答案

上面的评论帮我弄明白了.所以我使用了一个 VERSION 文件,它现在包含 0.0.0 我操纵它来创建其他变量

The comment above helped me figure it out. So I use a VERSION file that right now contains 0.0.0 which I manipulate to create other variables

  # determine what branch I am on
  - if [ "$CI_COMMIT_REF_NAME" = "master" ]; then ENVIRONMENT="qa"; else ENVIRONMENT="$CI_COMMIT_REF_NAME"; fi

  # determine patch number for semver
  - PATCH=`git log --pretty=oneline | wc -l | sed -e 's/^[[:space:]]*//'`
  - VERSION=`cat VERSION`

  # drop trailing 0 from VERSION
  - VERSION=${VERSION%?}

  # set all env variables
  - TAG="${VERSION}${PATCH}"
  - IMAGE="${TAG}-${ENVIRONMENT}" # used for Kubernetes
  - API_HOST="https://api.${ENVIRONMENT}.my-app.com/"
  - WEB_HOST="https://www.${ENVIRONMENT}.my-app.com/"

  # pass enviornment variables to make 
  - ENVIRONMENT="$ENVIRONMENT" IMAGE="$IMAGE" API_HOST="$API_HOST" WEB_HOST="$WEB_HOST" make

  # make has a step that calls sed and replaces text inline in this file to prepare Kubernetes
  - kubectl apply -f cfg/web-deployment.yaml

  # create a tag in the repo after deployment is done
  - curl -X POST --silent --insecure --show-error --fail "https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/repository/tags?tag_name=${TAG}&ref=${CI_COMMIT_SHA}&private_token=${GITLAB_TOKEN}"

这篇关于如何在 gitlab-ci.yml 文件的“脚本"部分中设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆