|
| 1 | +OpenShift CLI for CI/CD |
| 2 | +======================= |
| 3 | + |
| 4 | +This [Openshift command line tool](https://docs.openshift.com/enterprise/3.0/cli_reference/get_started_cli.html) docker |
| 5 | +image ships `oc` and includes `gettext` so you can use `envsubst` to substitute |
| 6 | +environment variables in your CI/CD pipeline, for example using in |
| 7 | +[Jenkins](https://jenkins.io/) or a job in [GitLab CI `.gitlab-ci.yaml` file](https://docs.gitlab.com/ce/ci/yaml/README.html#gitlab-ci-yml). |
| 8 | + |
| 9 | +Examples |
| 10 | +-------- |
| 11 | + |
| 12 | +Why should I use `envsubst`? You should never put secrets into your version |
| 13 | +control, so you might want to keep them in secret variables in your CI/CD |
| 14 | +system. You can use `envsubst` to substituted them correctly in your templates. |
| 15 | + |
| 16 | +A CI/CD system usually sets a bunch of variables to the build environment. |
| 17 | +Below I'll show an example of [GitLab CI variables set in the build environment](https://docs.gitlab.com/ce/ci/variables/#predefined-variables-environment-variables). |
| 18 | + |
| 19 | +- `CI_PROJECT_NAME`: my_awesome_project |
| 20 | +- `CI_BUILD_REF_SLUG`: f1234d |
| 21 | + |
| 22 | +Your `app.yaml` could look similar the one below: |
| 23 | + |
| 24 | + $ cat app.yaml |
| 25 | + ... |
| 26 | + - apiVersion: v1 |
| 27 | + kind: DeploymentConfig |
| 28 | + metadata: |
| 29 | + labels: |
| 30 | + app: ${CI_PROJECT_NAME} |
| 31 | + name: sample |
| 32 | + spec: |
| 33 | + replicas: 1 |
| 34 | + selector: |
| 35 | + app: ${CI_PROJECT_NAME} |
| 36 | + deployment: ${CI_BUILD_REF_SLUG} |
| 37 | + ... |
| 38 | + |
| 39 | +After `cat app.yaml | envsubst > app.yaml` you'll notice the variables have |
| 40 | +been replaced with their actual values: |
| 41 | + |
| 42 | + $ cat app.yaml |
| 43 | + ... |
| 44 | + - apiVersion: v1 |
| 45 | + kind: DeploymentConfig |
| 46 | + metadata: |
| 47 | + labels: |
| 48 | + app: my_awesome_project |
| 49 | + name: sample |
| 50 | + spec: |
| 51 | + replicas: 1 |
| 52 | + selector: |
| 53 | + app: my_awesome_project |
| 54 | + deployment: f1234d |
| 55 | + ... |
| 56 | + |
| 57 | +GitLab CI example |
| 58 | +----------------- |
| 59 | + |
| 60 | +Below a sample job in an `.gitlab-ci.yml` file, please note that OpenShift does |
| 61 | +not allow `_` in project names: |
| 62 | + |
| 63 | + deploy: |
| 64 | + image: widerin/openshift-cli |
| 65 | + stage: deploy |
| 66 | + script: |
| 67 | + - oc login "$OPENSHIFT_SERVER" --token="$OPENSHIFT_TOKEN" |
| 68 | + - cat app.yaml | envsubst > app.yaml |
| 69 | + - oc replace -f app.yaml -n ${CI_PROJECT_NAME/_/} |
0 commit comments