Skip to content

Commit 9229524

Browse files
authored
Merge pull request #11 from internetarchive/build-ci-build-cd
feature: build 2 docker images, one for CI, one for CD (opt-in by repo)
2 parents e8fdf0c + e05dad5 commit 9229524

File tree

5 files changed

+62
-11
lines changed

5 files changed

+62
-11
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ _**Note:** For urls like https://archive.org/services/project -- watch out for r
5656
### Customizing
5757
There are various options that can be used in conjunction with the `project.nomad` and `.gitlab-ci.yml` files, keys:
5858
```text
59+
NOMAD_VAR_BUILD_DEPLOY
5960
NOMAD_VAR_CHECK_PATH
6061
NOMAD_VAR_CHECK_PROTOCOL
6162
NOMAD_VAR_CHECK_TIMEOUT
@@ -81,12 +82,21 @@ NOMAD_VAR_VOLUMES
8182
- You can simply insert them, with values, in your project's `.gitlab-ci.yml` file before including _our_ [ci.yml](ci.yml) like above.
8283
- Examples 👇
8384
#### Don't actually deploy containers to nomad
84-
Perhaps your project just wants to leverage the CI (Continuous Integration) for [buil] and/or [test] steps - but not CD (Continuous Deployment). An example might be a back-end container that runs elsewhere and doesn't have web listener.
85+
Perhaps your project just wants to leverage the CI (Continuous Integration) for [build] and/or [test] steps - but not CD (Continuous Deployment). An example might be a back-end container that runs elsewhere and doesn't have web listener.
8586
```yaml
8687
variables:
8788
NOMAD_VAR_NO_DEPLOY: 'true'
8889
```
8990

91+
#### Build one docker image for CI and one docker image for CD
92+
If your project might want to build & use a larger docker image for the CI (Continuous Integration)
93+
and a smaller docker image for CD (Continuous Deploy), you can set this variable to an alternate
94+
Dockerfile location in your repo, relative to the top dir.
95+
```yaml
96+
variables:
97+
NOMAD_VAR_BUILD_DEPLOY: 'Dockerfile.deploy'
98+
```
99+
90100
#### Custom default RAM expectations from (default) 300 MB to 1 GB
91101
This value is the _expected_ value for your container's average running needs/usage, helpful for `nomad` scheduling purposes. It is a "soft limit" and we use *ten times* this amount to be the amount used for a "hard limit". If your allocated container exceeds the hard limit, the container may be restarted by `nomad` if there is memory pressure on the Virtual Machine the container is running on.
92102
```yaml
@@ -216,6 +226,16 @@ variables:
216226
NOMAD_VAR_NAMESPACE: 'team-titan'
217227
```
218228

229+
#### Only `docker tag` to `:latest` after all CI passes
230+
This is useful for repos that are setting up "serverless" docker images (typically don't do CD),
231+
and the `:latest` tag could get re-pulled anytime after that tag is pushed to the registry.
232+
The normal commit hash related tag will still get tagged & pushed, during [build], for CI tests.
233+
The `:latest` tag will get tagged & pushed *after* all CI tests have succeeded.
234+
```yaml
235+
variables:
236+
NOMAD_VAR_SERVERLESS: 'true'
237+
```
238+
219239

220240

221241
#### More customizations

build.sh

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,24 @@ gl_write_auto_build_variables_file() {
2929
echo "CI_APPLICATION_TAG=$CI_APPLICATION_TAG@$(podman --remote image inspect --format='{{ index (split (index .RepoDigests 0) "@") 1 }}' "$image_tagged")" > gl-auto-build-variables.env
3030
}
3131

32+
PUSH_LATEST=true
33+
if [ "$NOMAD_VAR_SERVERLESS" != "" ]; then
34+
PUSH_LATEST=
35+
fi
36+
37+
if [ "$NOMAD_VAR_BUILD_DEPLOY" ]; then
38+
PUSH_LATEST=
39+
export CI_REGISTRY_TAG=${CI_COMMIT_SHA}-deploy
40+
export DOCKERFILE_PATH=$NOMAD_VAR_BUILD_DEPLOY
41+
fi
42+
43+
if [[ -z "$CI_REGISTRY_TAG" ]]; then
44+
export CI_REGISTRY_TAG=$CI_COMMIT_SHA
45+
fi
3246

3347
if [[ -z "$CI_COMMIT_TAG" ]]; then
3448
export CI_APPLICATION_REPOSITORY=${CI_APPLICATION_REPOSITORY:-$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG}
35-
export CI_APPLICATION_TAG=${CI_APPLICATION_TAG:-$CI_COMMIT_SHA}
49+
export CI_APPLICATION_TAG=${CI_APPLICATION_TAG:-$CI_REGISTRY_TAG}
3650
else
3751
export CI_APPLICATION_REPOSITORY=${CI_APPLICATION_REPOSITORY:-$CI_REGISTRY_IMAGE}
3852
export CI_APPLICATION_TAG=${CI_APPLICATION_TAG:-$CI_COMMIT_TAG}
@@ -57,7 +71,7 @@ build_args=(
5771
--tag "$image_tagged"
5872
)
5973

60-
if [ "$NOMAD_VAR_SERVERLESS" = "" ]; then
74+
if [ $PUSH_LATEST ]; then
6175
build_args+=(--tag "$image_latest")
6276
fi
6377

@@ -83,7 +97,7 @@ fi
8397
set -x
8498
podman --remote push "$image_tagged"
8599
)
86-
if [ "$NOMAD_VAR_SERVERLESS" = "" ]; then
100+
if [ $PUSH_LATEST ]; then
87101
(
88102
set -x
89103
podman --remote push "$image_latest"

ci.yml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# to whatever your Nomad cluster was setup to.
1414

1515

16-
# NOTE: changes to *this* repo, will fire of GitHub Actions here:
16+
# NOTE: changes to *this* repo, will fire off GitHub Actions here:
1717
# https://github.com/internetarchive/nomad/blob/main/.github/workflows/cicd.yml
1818
# which will re/make this container image:
1919
image: ghcr.io/internetarchive/nomad:main
@@ -29,12 +29,9 @@ build:
2929
# This was adapted & simplified from:
3030
# https://gitlab.com/gitlab-org/gitlab/-/raw/master/lib/gitlab/ci/templates/Jobs/Build.gitlab-ci.yml
3131
stage: build
32-
variables:
33-
DOCKER_HOST: 'unix:///run/podman/podman.sock'
34-
DOCKER_TLS_CERTDIR: ''
35-
DOCKER_BUILDKIT: 1
3632
script:
3733
# https://github.com/internetarchive/nomad/blob/main/build.sh
34+
- export NOMAD_VAR_BUILD_DEPLOY=""
3835
- /build.sh
3936
artifacts:
4037
reports:
@@ -44,6 +41,16 @@ build:
4441
when: never
4542
- if: '$CI_COMMIT_TAG || $CI_COMMIT_BRANCH'
4643

44+
build_deploy:
45+
stage: build
46+
script:
47+
# https://github.com/internetarchive/nomad/blob/main/build.sh
48+
- /build.sh
49+
rules:
50+
- if: '$BUILD_DISABLED'
51+
when: never
52+
- if: '$NOMAD_VAR_BUILD_DEPLOY && $CI_COMMIT_BRANCH'
53+
4754
deploy:
4855
stage: deploy
4956
script:

deploy.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,16 @@ function main() {
2222
PRIVATE_REPO=${PRIVATE_REPO:-""}
2323
NOMAD_VAR_DEPLOY_WITH_PODMAN=${NOMAD_VAR_DEPLOY_WITH_PODMAN:-""}
2424
NOMAD_VAR_LEGACY_SERVICE_NAMES_URLPREFIX=${NOMAD_VAR_LEGACY_SERVICE_NAMES_URLPREFIX:-""}
25+
CI_REGISTRY_TAG=${CI_REGISTRY_TAG:-""}
26+
CI_COMMIT_SHA=${CI_COMMIT_SHA:-""}
27+
NOMAD_VAR_BUILD_DEPLOY=${NOMAD_VAR_BUILD_DEPLOY:-""}
2528
fi
2629

30+
if [ "$NOMAD_VAR_BUILD_DEPLOY" ]; then
31+
CI_REGISTRY_TAG=${CI_COMMIT_SHA}-deploy
32+
else
33+
CI_REGISTRY_TAG=$CI_COMMIT_SHA
34+
fi
2735

2836
# IF someone set this programmatically in their project yml `before_script:` tag, etc., exit
2937
if [ "$NOMAD_VAR_NO_DEPLOY" ]; then exit 0; fi
@@ -260,6 +268,7 @@ EOF
260268
export NOMAD_VAR_CI_PROJECT_PATH_SLUG="$CI_PROJECT_PATH_SLUG"
261269
export NOMAD_VAR_CI_REGISTRY="$CI_REGISTRY"
262270
export NOMAD_VAR_CI_REGISTRY_IMAGE="$CI_REGISTRY_IMAGE"
271+
export NOMAD_VAR_CI_REGISTRY_TAG="$CI_REGISTRY_TAG"
263272
export NOMAD_VAR_CI_REGISTRY_PASSWORD="$CI_REGISTRY_PASSWORD"
264273
export NOMAD_VAR_CI_REGISTRY_READ_TOKEN="$CI_REGISTRY_READ_TOKEN"
265274
export NOMAD_VAR_CI_REGISTRY_USER="$CI_REGISTRY_USER"

project.nomad

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ variables {
77
# what's needed for these first 7-8 vars.
88
# GitLab:
99
# these all pass through from [deploy] phase.
10-
# all 7-8 of these first vars get replaced during normal GitLab CI/CD from CI/CD variables.
10+
# all 8-9 of these first vars get replaced during normal GitLab CI/CD from CI/CD variables.
1111
CI_REGISTRY = "ghcr.io" # registry hostname
1212
CI_REGISTRY_IMAGE = "ghcr.io/internetarchive" # registry image location
13+
CI_REGISTRY_TAG = "main" # GH: branch name; GL: commit sha
1314
CI_COMMIT_REF_SLUG = "hello-js" # GH: repo name; GL: branch name. slugged
1415
CI_COMMIT_SHA = "main" # GH: branch name; GL: commit sha
1516
CI_PROJECT_PATH_SLUG = "internetarchive-hello-js" # repo and group it is part of, slugged
@@ -149,7 +150,7 @@ locals {
149150
ports_all = merge(local.ports_main, local.ports_extra_https, local.ports_extra_tcp, {})
150151

151152
# Use CI_GITHUB_IMAGE if set, otherwise use GitLab vars interpolated string
152-
docker_image = var.CI_GITHUB_IMAGE != "" ? var.CI_GITHUB_IMAGE : "${var.CI_REGISTRY_IMAGE}/${var.CI_COMMIT_REF_SLUG}:${var.CI_COMMIT_SHA}"
153+
docker_image = var.CI_GITHUB_IMAGE != "" ? var.CI_GITHUB_IMAGE : "${var.CI_REGISTRY_IMAGE}/${var.CI_COMMIT_REF_SLUG}:${var.CI_REGISTRY_TAG}"
153154
# "
154155

155156
# GitLab docker login user/pass timeout rather quickly. If admin set CI_REGISTRY_READ_TOKEN key

0 commit comments

Comments
 (0)