Unverified Commit 191c88c7 authored by Nicolas Viallatte's avatar Nicolas Viallatte
Browse files

feat: add possibility to add a bucket prefix at deploy

parent 8773451b
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -68,6 +68,7 @@ The S3 template uses some global configuration used throughout all jobs.
| :lock: `S3_ACCESS_KEY` | Default S3 service Access Key                 | **has to be defined** |
| :lock: `S3_SECRET_KEY` | Default S3 service Secret Key                 | **has to be defined** |
| `S3_BASE_BUCKET_NAME`  | Base bucket name                              | `$CI_PROJECT_NAME` ([see GitLab doc](https://docs.gitlab.com/ee/ci/variables/predefined_variables.html)) |
| `S3_ROOT_PATH` | Default root path where files will be uploaded in the S3 bucket (:warning: don't forget the starting `/`)  | _none_ |

### Secrets management

@@ -115,6 +116,7 @@ Here are variables supported to configure review environments:
| `S3_REVIEW_BUCKET_NAME`  | Bucket name for `review` env      | `"${S3_BASE_BUCKET_NAME}-${CI_ENVIRONMENT_SLUG}"` (ex: `myproject-review-fix-bug-12`) |
| `S3_REVIEW_ENVIRONMENT_SCHEME` | The review environment protocol scheme | `https` |
| `S3_REVIEW_ENVIRONMENT_DOMAIN` | The review environment domain. | _none_ |
| `S3_REVIEW_ROOT_PATH`  |  S3 bucket root path (prefix) for `review` env _(only define if different from default)_ | `S3_ROOT_PATH` |

Note: By default, review `environment.url` will be built as `${S3_REVIEW_ENVIRONMENT_SCHEME}://${$CI_PROJECT_NAME}-${CI_ENVIRONMENT_SLUG}.${S3_REVIEW_ENVIRONMENT_DOMAIN}`

@@ -134,6 +136,7 @@ Here are variables supported to configure the integration environment:
| :lock: `S3_INTEG_SECRET_KEY` | S3 service Secret Key for `integration` env  _(only define if different from default)_    | `$S3_SECRET_KEY` |
| `S3_INTEG_BUCKET_NAME`   | Bucket name for `integration` env | `${S3_BASE_BUCKET_NAME}-integration` |
| `S3_INTEG_ENVIRONMENT_URL` | The integration environment url **including scheme** (ex: `https://my-project-integration.s3-website.nonpublic.domain.com`). Do not use variable inside variable definition as it will result in a two level cascade variable and gitlab does not allow that. | _none_ |
| `S3_INTEG_ROOT_PATH`  |  S3 bucket root path (prefix) for `integration` env _(only define if different from default)_ | `S3_ROOT_PATH` |

#### Staging environment

@@ -152,6 +155,7 @@ Here are variables supported to configure the staging environment:
| :lock: `S3_STAGING_SECRET_KEY` | S3 service Secret Key for `staging` env  _(only define if different from default)_    | `$S3_SECRET_KEY` |
| `S3_STAGING_BUCKET_NAME` | Bucket name for `staging` env     | `${S3_BASE_BUCKET_NAME}-staging` |
| `S3_STAGING_ENVIRONMENT_URL` | The staging environment url **including scheme** (ex: `https://my-project-staging.s3-website.nonpublic.domain`). Do not use variable inside variable definition as it will result in a two level cascade variable and gitlab does not allow that. | _none_ |
| `S3_STAGING_ROOT_PATH`  |  S3 bucket root path (prefix) for `staging` env _(only define if different from default)_ | `S3_ROOT_PATH` |

#### Production environment

@@ -170,6 +174,7 @@ Here are variables supported to configure the production environment:
| `S3_PROD_BUCKET_NAME`    | Bucket name for `production` env  | `$S3_BASE_BUCKET_NAME` |
| `S3_PROD_ENVIRONMENT_URL`| The production environment url **including scheme** (ex: `https://my-project.s3-website.public.domain.com`) Do not use variable inside variable definition as it will result in a two level cascade variable and gitlab does not allow that. | _none_ |
| `AUTODEPLOY_TO_PROD`     | Set this variable to auto-deploy to production. If not set deployment to production will be `manual` (default behaviour). | _none_ (disabled) |
| `S3_PROD_ROOT_PATH`  |  S3 bucket root path (prefix) for `production` env _(only define if different from default)_ | `S3_ROOT_PATH` |

### Deployment jobs

+24 −0
Original line number Diff line number Diff line
@@ -60,6 +60,10 @@
      "description": "[s3cmd](https://s3tools.org/usage) command and options to enable WebSite hosting on the bucket",
      "default": "ws-create --ws-index=index.html",
      "advanced": true
    },
    {
      "name": "S3_ROOT_PATH",
      "description": "Default root path where files will be uploaded in the S3 bucket (:warning: don't forget the starting `/`)"
    }
  ],
  "features": [
@@ -104,6 +108,11 @@
          "name": "CLEANUP_ALL_REVIEW",
          "description": "Enables a **manual** job to cleanup all review envs at once.\n\nYou may also use it to [schedule](https://docs.gitlab.com/ee/ci/pipelines/schedules.html) cloud resources cleanup. See documentation.",
          "type": "boolean"
        },
        {
          "name": "S3_REVIEW_ROOT_PATH",
          "description": "S3 bucket root path (prefix) for `review` env _(only define if different from default)_",
          "advanced": true
        }
      ]
    },
@@ -138,6 +147,11 @@
          "type": "url",
          "description": "The integration environment url including scheme (ex: `https://my-project-integration.s3-website.nonpublic.domain`).\n\nDo not use variable inside variable definition as it will result in a two level cascade variable and gitlab does not allow that.",
          "mandatory": true
        },
        {
          "name": "S3_INTEG_ROOT_PATH",
          "description": "S3 bucket root path (prefix) for `integration` env _(only define if different from default)_",
          "advanced": true
        }
      ]
    },
@@ -172,6 +186,11 @@
          "type": "url",
          "description": "The staging environment url including scheme (ex: `https://my-project-staging.s3-website.nonpublic.domain`).\n\nDo not use variable inside variable definition as it will result in a two level cascade variable and gitlab does not allow that.",
          "mandatory": true
        },
        {
          "name": "S3_STAGING_ROOT_PATH",
          "description": "S3 bucket root path (prefix) for `staging` env _(only define if different from default)_",
          "advanced": true
        }
      ]
    },
@@ -211,6 +230,11 @@
          "name": "AUTODEPLOY_TO_PROD",
          "type": "boolean",
          "description": "Set this variable to auto-deploy to production. If not set deployment to production will be manual (default behaviour)."
        },
        {
          "name": "S3_PROD_ROOT_PATH",
          "description": "S3 bucket root path (prefix) for `production` env _(only define if different from default)_",
          "advanced": true
        }
      ]
    }
+8 −2
Original line number Diff line number Diff line
@@ -277,6 +277,7 @@ stages:
  function deploy() {
    export env=$1
    bucket=$2
    root_path=$3

    # extract hostname from $CI_ENVIRONMENT_URL
    hostname=$(echo "$CI_ENVIRONMENT_URL" | awk -F[/:] '{print $4}')
@@ -284,6 +285,7 @@ stages:

    log_info "--- \\e[32mdeploy\\e[0m (env: \\e[33;1m${env}\\e[0m)"
    log_info "--- bucket: \\e[33;1m${bucket}\\e[0m"
    log_info "--- root_path: \\e[33;1m${root_path:-(none)}\\e[0m"
    log_info "--- env: \\e[33;1m${env}\\e[0m"
    log_info "--- hostname: \\e[33;1m${hostname}\\e[0m"

@@ -310,7 +312,7 @@ stages:
    # sync files
    log_info "... synchronize files"
    # shellcheck disable=SC2086
    s3cmd ${TRACE+-v} $S3_DEPLOY_ARGS $S3_DEPLOY_FILES "s3://${bucket}"
    s3cmd ${TRACE+-v} $S3_DEPLOY_ARGS $S3_DEPLOY_FILES "s3://${bucket}${root_path}"

    # create website
    if [[ "$S3_WEBSITE_DISABLED" != "true" ]]
@@ -432,7 +434,7 @@ stages:
    - assert_defined "${ENV_SECRET_KEY:-$S3_SECRET_KEY}" 'Missing required S3 secret key'
    - login "${ENV_ENDPOINT_HOST:-$S3_ENDPOINT_HOST}" "${S3_HOST_BUCKET}" "${ENV_ACCESS_KEY:-$S3_ACCESS_KEY}" "${ENV_SECRET_KEY:-$S3_SECRET_KEY}"
  script:
    - deploy "$ENV_TYPE" "${ENV_BUCKET_NAME:-${S3_BASE_BUCKET_NAME}${ENV_APP_SUFFIX}}"
    - deploy "$ENV_TYPE" "${ENV_BUCKET_NAME:-${S3_BASE_BUCKET_NAME}${ENV_APP_SUFFIX}}" "${ENV_ROOT_PATH:-${S3_ROOT_PATH}}"
  artifacts:
    name: "$ENV_TYPE env url for $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
    paths:
@@ -480,6 +482,7 @@ s3-review:
    ENV_ENDPOINT_HOST: "$S3_REVIEW_ENDPOINT_HOST"
    ENV_ACCESS_KEY: "$S3_REVIEW_ACCESS_KEY"
    ENV_SECRET_KEY: "$S3_REVIEW_SECRET_KEY"
    ENV_ROOT_PATH: "$S3_REVIEW_ROOT_PATH"
  environment:
    name: review/$CI_COMMIT_REF_NAME
    url: "${S3_REVIEW_ENVIRONMENT_SCHEME}://${CI_PROJECT_NAME}-${CI_ENVIRONMENT_SLUG}.${S3_REVIEW_ENVIRONMENT_DOMAIN}"
@@ -579,6 +582,7 @@ s3-integration:
    ENV_ENDPOINT_HOST: "$S3_INTEG_ENDPOINT_HOST"
    ENV_ACCESS_KEY: "$S3_INTEG_ACCESS_KEY"
    ENV_SECRET_KEY: "$S3_INTEG_SECRET_KEY"
    ENV_ROOT_PATH: "$S3_INTEG_ROOT_PATH"
  environment:
    name: integration
    url: "${S3_INTEG_ENVIRONMENT_URL}"
@@ -603,6 +607,7 @@ s3-staging:
    ENV_ENDPOINT_HOST: "$S3_STAGING_ENDPOINT_HOST"
    ENV_ACCESS_KEY: "$S3_STAGING_ACCESS_KEY"
    ENV_SECRET_KEY: "$S3_STAGING_SECRET_KEY"
    ENV_ROOT_PATH: "$S3_STAGING_ROOT_PATH"
  environment:
    name: staging
    url: "${S3_STAGING_ENVIRONMENT_URL}"
@@ -629,6 +634,7 @@ s3-production:
    ENV_ENDPOINT_HOST: "$S3_PROD_ENDPOINT_HOST"
    ENV_ACCESS_KEY: "$S3_PROD_ACCESS_KEY"
    ENV_SECRET_KEY: "$S3_PROD_SECRET_KEY"
    ENV_ROOT_PATH: "$S3_PROD_ROOT_PATH"
  environment:
    name: production
    url: "${S3_PROD_ENVIRONMENT_URL}"