Skip to content

Commit

Permalink
feat(ci): add sitemap uploader (#259)
Browse files Browse the repository at this point in the history
* feat(ci): add sitemap uploader

* test(ci): fix tests

* fix(sitemap): handle BASE_URL replacement

* fix: make job multiple

* fix: use curl, better BASE_URL replacement

* chore(ci): add Generate sitemap (prod) job

* Update .gitlab-ci.yml

* Update .gitlab-ci.yml

* fix: curl exitError when fail

* fix: add allow_failure

* chore(ci): use internal service url to fetch sitemap

* chore(ci): use real url

* fix: fix namespace

* ci: add create sitemap dev

* Update HOWTO.md

Co-authored-by: Julien Bouquillon <[email protected]>

* Apply suggestions from code review

Co-authored-by: Julien Bouquillon <[email protected]>

* Apply suggestions from code review

* Update .gitlab-ci.yml

Co-authored-by: Julien Bouquillon <[email protected]>

* ci: filter manul job

* ci: move manual job to .post stage

* ci: manual job don't have to wait for jobs

* ci: fix manual

* fix: ci

Co-authored-by: LionelB <[email protected]>
Co-authored-by: Lionel <[email protected]>
  • Loading branch information
3 people authored Jan 22, 2021
1 parent 7897f1f commit f3a87ea
Show file tree
Hide file tree
Showing 17 changed files with 241 additions and 14 deletions.
42 changes: 42 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,45 @@ Restore container:
BACKUP_DB_FILE: "hasura_prod_db.psql.gz"
# kosko options
KOSKO_GENERATE_ARGS: --env prod restore

Generate sitemap (prod):
stage: Install
needs: []
extends:
- .base_deploy_kosko_stage
rules:
- if: '($CI_PIPELINE_SOURCE == "trigger" || $CI_PIPELINE_SOURCE == "web") && $ACTION == "SITEMAP"'
when: always
- if: '($CI_COMMIT_TAG || $CI_COMMIT_REF_SLUG == "master")'
when: manual
allow_failure: true
- when: never
environment:
name: prod2
variables:
SITEMAP_ENDPOINT: "https://cdtn-admin.fabrique.social.gouv.fr/api/sitemap"
DESTINATION_CONTAINER: sitemap
DESTINATION_NAME: sitemap.xml
SECRET_NAME: azure-cdtnadminprod-volume
KOSKO_GENERATE_ARGS: --env prod sitemap-uploader

Generate sitemap (dev):
stage: Install
needs: []
extends:
- .base_deploy_kosko_stage
rules:
- if: '($CI_PIPELINE_SOURCE == "trigger" || $CI_PIPELINE_SOURCE == "web") && $ACTION == "SITEMAP"'
when: always
- if: '($CI_COMMIT_TAG == "" || $CI_COMMIT_REF_SLUG != "master")'
when: manual
allow_failure: true
- when: never
environment:
name: prod2
variables:
SITEMAP_ENDPOINT: "https://cdtn-admin.fabrique.social.gouv.fr/api/sitemap"
DESTINATION_CONTAINER: sitemap
DESTINATION_NAME: sitemap.xml
SECRET_NAME: azure-cdtnadmindev-volume
KOSKO_GENERATE_ARGS: --env dev sitemap-uploader
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`kosko generate --prod sitemap-uploader 1`] = `
"metadata:
name: sitemap-uploader-424242
namespace: cdtn-admin-secret
spec:
backoffLimit: 3
template:
metadata:
name: sitemap-uploader-424242
namespace: cdtn-admin-secret
spec:
restartPolicy: OnFailure
containers:
- name: az-sitemap-uploader
image: 'mcr.microsoft.com/azure-cli:2.9.1'
command:
- bash
args:
- '-c'
- |+
echo \\"Fetch sitemap from $SITEMAP_ENDPOINT\\"
curl --fail -L $SITEMAP_ENDPOINT -o sitemap.xml
if [[ -f sitemap.xml ]]; then
# replace the default urls hostname if $BASE_URL is given
[[ -n $BASE_URL ]] && sed -i -E 's#<loc>https?://[^/]*/#<loc>'$BASE_URL'/#' sitemap.xml
# upload
echo \\"Upload sitemap to azure/$DESTINATION_CONTAINER/$DESTINATION_NAME\\"
az storage blob upload --account-name $AZ_ACCOUNT_NAME --account-key $AZ_ACCOUNT_KEY --container-name $DESTINATION_CONTAINER --file sitemap.xml --name $DESTINATION_NAME
else
echo \\"Cannot fetch sitemap.xml, abort\\"
exit 1
fi
env:
- name: DESTINATION_CONTAINER
value: destination-container
- name: DESTINATION_NAME
value: sitemap.xml
- name: SITEMAP_ENDPOINT
value: 'https://path/to/sitemap'
- name: BASE_URL
value: 'https://host.tmp'
- name: AZ_ACCOUNT_NAME
value: $(azurestorageaccountname)
- name: AZ_ACCOUNT_KEY
value: $(azurestorageaccountkey)
envFrom:
- secretRef:
name: azure-volume-dev-secret
apiVersion: batch/v1
kind: Job
"
`;
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { project } from "@socialgouv/kosko-charts/testing/fake/gitlab-ci.env";

jest.setTimeout(1000 * 60);
test("kosko generate --prod restore", async () => {
process.env.SOURCE_CONTAINER = "source-container";
process.env.SOURCE_SERVER = "prod";
process.env.DESTINATION_CONTAINER = "destination-container";
process.env.DESTINATION_SERVER = "dev";
process.env.BACKUP_DB_OWNER = "some-owner";
process.env.BACKUP_DB_NAME = "some-database";
process.env.BACKUP_DB_FILE = "some-backup.sql.gz";
expect(
await getEnvManifests("prod", "restore", {
...project("cdtn-admin").prod,
SOURCE_CONTAINER: "source-container",
SOURCE_SERVER: "prod",
DESTINATION_CONTAINER: "destination-container",
DESTINATION_SERVER: "dev",
BACKUP_DB_OWNER: "some-owner",
BACKUP_DB_NAME: "some-database",
BACKUP_DB_FILE: "some-backup.sql.gz",
})
).toMatchSnapshot();
});
18 changes: 18 additions & 0 deletions .k8s/__tests__/generate-prod-sitemap-uploader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//

import { getEnvManifests } from "@socialgouv/kosko-charts/testing";
import { project } from "@socialgouv/kosko-charts/testing/fake/gitlab-ci.env";

jest.setTimeout(1000 * 60);
test("kosko generate --prod sitemap-uploader", async () => {
expect(
await getEnvManifests("prod", "sitemap-uploader", {
...project("cdtn-admin").prod,
SITEMAP_ENDPOINT: "https://path/to/sitemap",
DESTINATION_CONTAINER: "destination-container",
DESTINATION_NAME: "sitemap.xml",
SECRET_NAME: "azure-volume-dev-secret",
BASE_URL: "https://host.tmp",
})
).toMatchSnapshot();
});
File renamed without changes.
96 changes: 96 additions & 0 deletions .k8s/components/sitemap-uploader/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { ok } from "assert";
import { Job } from "kubernetes-models/batch/v1/Job";

// renovate: datasource=docker depName=mcr.microsoft.com/azure-cli versioning=2.9.1
const AZ_DOCKER_TAG = "2.9.1";

const uploadSitemapScript = `
echo "Fetch sitemap from $SITEMAP_ENDPOINT"
curl --fail -L $SITEMAP_ENDPOINT -o sitemap.xml
if [[ -f sitemap.xml ]]; then
# replace the default urls hostname if $BASE_URL is given
[[ -n $BASE_URL ]] && sed -i -E 's#<loc>https?://[^/]*/#<loc>'$BASE_URL'/#' sitemap.xml
# upload
echo "Upload sitemap to azure/$DESTINATION_CONTAINER/$DESTINATION_NAME"
az storage blob upload --account-name $AZ_ACCOUNT_NAME --account-key $AZ_ACCOUNT_KEY --container-name $DESTINATION_CONTAINER --file sitemap.xml --name $DESTINATION_NAME
else
echo "Cannot fetch sitemap.xml, abort"
exit 1
fi
`;

ok(process.env.SITEMAP_ENDPOINT); // https://url/sitemap
ok(process.env.DESTINATION_CONTAINER); // sitemap
ok(process.env.DESTINATION_NAME); // sitemap.xml
ok(process.env.SECRET_NAME); // azure-cdtnadmindev-volume | azure-cdtnadminprod-volume

const createSitemapJob = () => {
const job = new Job({
metadata: {
name: `sitemap-uploader-${process.env.CI_JOB_ID}`,
namespace: "cdtn-admin-secret",
},

spec: {
backoffLimit: 3,
template: {
metadata: {
name: `sitemap-uploader-${process.env.CI_JOB_ID}`,
namespace: "cdtn-admin-secret",
},
spec: {
restartPolicy: "OnFailure",
containers: [
{
name: "az-sitemap-uploader",
image: `mcr.microsoft.com/azure-cli:${AZ_DOCKER_TAG}`,
command: ["bash"],
args: ["-c", uploadSitemapScript],
env: [
{
name: "DESTINATION_CONTAINER",
value: process.env.DESTINATION_CONTAINER,
},
{
name: "DESTINATION_NAME",
value: process.env.DESTINATION_NAME,
},
{
name: "SITEMAP_ENDPOINT",
value: process.env.SITEMAP_ENDPOINT,
},
{
// should not contain ending slash
name: "BASE_URL",
value: process.env.BASE_URL,
},
{
name: "AZ_ACCOUNT_NAME",
value: "$(azurestorageaccountname)",
},
{
name: "AZ_ACCOUNT_KEY",
value: "$(azurestorageaccountkey)",
},
],
envFrom: [
{
secretRef: {
name: process.env.SECRET_NAME,
},
},
],
},
],
},
},
},
});
return job;
};

const job = createSitemapJob();
export default job;
3 changes: 2 additions & 1 deletion .k8s/environments/.gitlab-ci.env
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ KUBE_INGRESS_BASE_DOMAIN=dev2.fabrique.social.gouv.fr
KUBE_NAMESPACE=cdtn-admin-24-branch-42
RANCHER_PROJECT_ID=c-kk8xm:p-4fxg8
CI_COMMIT_SHA=8843083edb7f873cad1d1420731a60773594ffae
CI_COMMIT_SHORT_SHA=8843083e
CI_COMMIT_SHORT_SHA=8843083e
CI_JOB_ID=424242
2 changes: 1 addition & 1 deletion .k8s/kosko.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

# dont include restore jobs on deploys
components = ["!restore", "*"]
components = ["!restore", "!sitemap-uploader","*"]
require = ["ts-node/register"]

# dont include underscores-prefix components
Expand Down
2 changes: 1 addition & 1 deletion .k8s/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dependencies": {
"@kosko/env": "^1.0.2",
"@kubernetes-models/sealed-secrets": "^1.0.3",
"@socialgouv/kosko-charts": "^4.2.2",
"@socialgouv/kosko-charts": "^4.3.0",
"@types/node": "^14.14.22",
"kosko": "^1.0.2",
"kubernetes-models": "^1.0.3",
Expand Down
8 changes: 4 additions & 4 deletions .k8s/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -626,10 +626,10 @@
dependencies:
"@sinonjs/commons" "^1.7.0"

"@socialgouv/kosko-charts@^4.2.2":
version "4.2.2"
resolved "https://registry.yarnpkg.com/@socialgouv/kosko-charts/-/kosko-charts-4.2.2.tgz#6c8f445fcf767d3d9611dcc071c845c667028f65"
integrity sha512-i/YnlkNC/xtUbRdce2GQYAQRUJatg6WT8pcjH856VGFOeww2W30f7SDnsZ0zlAIR+ZPSsFcmw4QIo2Ho5qh8MQ==
"@socialgouv/kosko-charts@^4.3.0":
version "4.3.0"
resolved "https://registry.yarnpkg.com/@socialgouv/kosko-charts/-/kosko-charts-4.3.0.tgz#362102758803285e1a9313407684babb54b3fbc0"
integrity sha512-O5J1nFsFVJ65xFn6MOYOlx9/ZyRqGCz5kiICdBxc+RqLxL7LlDMyAsmnGdeQ+cpwsUi/WDBRJ7MqWOc4uAHUTA==
dependencies:
"@kosko/env" "^1.0.1"
"@kubernetes-models/sealed-secrets" "^1.0.2"
Expand Down
12 changes: 12 additions & 0 deletions HOWTO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Comment on fait pour... ?

- [Créer un fichier sitemap.xml](#Créer-un-fichier-sitemap.xml)

## Créer un fichier sitemap-branch.xml

Lancer la tâche `Generate Sitemap (dev)` en spécifiant les paramètres

```sh
SITEMAP_ENDPOINT: "https://cdtn-admin.fabrique.social.gouv.fr/api/sitemap"
DESTINATION_NAME: sitemap-branch.xml
```

0 comments on commit f3a87ea

Please sign in to comment.