Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset host from Gitlab artifact #20

Merged
merged 8 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,41 @@ after('deploy:update_code', 'deploy:upload_code');
```

The files to upload become configured via [`upload_paths`](https://github.com/xima-media/xima-deployer-extended-typo3/blob/main/set.php#L61).

## Reset from Gitlab Artifact

If Host A needs current data from Host B without direct access, Gitlab artifacts may be used as an intermediary.

Prerequisites:
- Gitlab API token with download access
- Artifact download url in *deploy.php* of Host A:
```php
set('reset_gitlab_artifact_url', 'https://<domain>/api/v4/projects/<project-id>/jobs/artifacts/<branch>/download?job=export-job');
```

1. Host B: Exports database and media files, which will then be uploaded as artifact.
```yaml
export-job:
...
script:
- vendor/bin/dep db:export --options=dumpcode:myArtifact --no-interaction -vvv host-b
- vendor/bin/dep db:process --options=dumpcode:myArtifact --no-interaction -vvv host-b
- vendor/bin/dep db:compress --options=dumpcode:myArtifact --no-interaction -vvv host-b
- vendor/bin/dep db:download --options=dumpcode:myArtifact --no-interaction -vvv host-b
- vendor/bin/dep media:pull --no-interaction host-b
artifacts:
paths:
- .dep/database
- public/fileadmin
- public/uploads
expire_in: 1 day
```
2. Host A: Uses task **reset:from_gitlab_artifact** to download and import the artifact.
```yaml
...
import-job:
...
script:
- vendor/bin/dep reset:from_gitlab_artifact --options="token:$CI_VARIABLE_WITH_API_TOKEN,dumpcode:myArtifact" host-a
when: manual
```
1 change: 1 addition & 0 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require_once(__DIR__ . '/recipe/logs_php.php');
require_once(__DIR__ . '/recipe/check_requirements.php');
require_once(__DIR__ . '/recipe/sequelace.php');
require_once(__DIR__ . '/recipe/reset_from_gitlab_artifact');

// prevent pipeline fail on first deploy (no tables)
// + enable database copy in feature branch deployment
Expand Down
37 changes: 37 additions & 0 deletions recipe/reset_from_gitlab_artifact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Deployer;

use Deployer\Exception\GracefulShutdownException;
use SourceBroker\DeployerExtendedDatabase\Utility\ConsoleUtility;

task('reset:from_gitlab_artifact', function () {
// set in deploy.php as https://gitlab.example.org/api/v4/projects/<project-id>/jobs/artifacts/<branch>/download?job=<job-of-artifact>
$url = get('reset_gitlab_artifact_url');
// Gitlab API token for the repository where the artifact is stored. Can be a project token.
$gitlabApiToken = (new ConsoleUtility())->getOption('token', true);
// Database dumpcode that was used during the creation of the Gitlab artifact.
$dumpCode = (new ConsoleUtility())->getOption('dumpcode', true);

if (!filter_var($url, FILTER_VALIDATE_URL) || !preg_match('#jobs\/artifacts\/.+\/download$#', parse_url($url, PHP_URL_PATH))) {
throw new GracefulShutdownException('Gitlab API URL is invalid: "' . $url . '"');
return;
maikschneider marked this conversation as resolved.
Show resolved Hide resolved
}
if (get('is_argument_host_the_same_as_local_host')) {
$activeDir = get('deploy_path') . (testLocally('[ -e {{deploy_path}}/release ]') ? '/release' : '/current');
$activeDir = testLocally('[ -e ' . $activeDir . ' ]') ? $activeDir : get('deploy_path');
runLocally('cd ' . $activeDir . ' && curl --location --output artifacts.zip --header "PRIVATE-TOKEN: ' . $gitlabApiToken . '" "' . $url . '"');
runLocally('cd ' . $activeDir . ' && vendor/bin/dep db:rmdump {{argument_host}} --options=dumpcode:' . $dumpCode . ' --no-interaction');
runLocally('cd ' . $activeDir . ' && unzip -o artifacts.zip');
runLocally('cd ' . $activeDir . ' && mv -n .dep/database/dumps/*dumpcode=' . $dumpCode . '* {{db_storage_path_local}}/');
runLocally('cd ' . $activeDir . ' && vendor/bin/dep db:decompress {{argument_host}} --options=dumpcode:' . $dumpCode . ' --no-interaction');
runLocally('cd ' . $activeDir . ' && vendor/bin/dep db:import {{argument_host}} --options=dumpcode:' . $dumpCode . ' --no-interaction');
runLocally('cd ' . $activeDir . ' && vendor/bin/dep db:rmdump {{argument_host}} --options=dumpcode:' . $dumpCode . ' --no-interaction');
runLocally('cd ' . $activeDir . ' && rm -f artifacts.zip');
runLocally('cd ' . $activeDir . ' && {{local/bin/php}} {{bin/typo3cms}} cache:flush');
runLocally('cd ' . $activeDir . ' && {{local/bin/php}} {{bin/typo3cms}} cache:warmup');
} else {
$verbosity = (new ConsoleUtility())->getVerbosityAsParameter();
run('cd {{release_or_current_path}} && {{bin/php}} {{bin/deployer}} reset:from_gitlab_artifact ' . $verbosity . ' --options="token:' . $gitlabApiToken . ',dumpcode:' . $dumpCode . '" {{argument_host}}');
}
});