deployer/docs/ci-cd.md

97 lines
2.5 KiB
Markdown
Raw Normal View History

2021-11-07 22:38:03 +01:00
# CI/CD
2021-12-05 10:14:28 +00:00
## GitHub Actions
Use official [GitHub Action for Deployer](https://github.com/deployphp/action).
Create `.github/workflows/deploy.yml` file with following content:
```yaml
name: deploy
on:
push:
2022-09-06 09:17:58 +02:00
branches: [master]
2021-12-05 10:14:28 +00:00
concurrency: production_environment
jobs:
deploy:
runs-on: ubuntu-latest
2022-09-06 09:17:58 +02:00
2021-12-05 10:14:28 +00:00
steps:
2022-09-06 09:17:58 +02:00
- uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.0"
- name: Deploy
uses: deployphp/action@v1
with:
private-key: ${{ secrets.PRIVATE_KEY }}
dep: deploy
2021-12-05 10:14:28 +00:00
```
:::warning
2022-09-06 09:17:58 +02:00
The `concurrency: production_environment` is important as it prevents concurrent
2021-12-05 10:14:28 +00:00
deploys.
:::
## GitLab CI/CD
Set the following variables in GitLab project:
- `SSH_KNOWN_HOSTS`: Content of `~/.ssh/known_hosts` file.
2022-09-06 09:17:58 +02:00
The public SSH keys for a host may be obtained using the utility `ssh-keyscan`.
For example: `ssh-keyscan deployer.org`.
- `SSH_PRIVATE_KEY`: Private key for connecting to remote hosts.
To generate private key: `ssh-keygen -t ed25519 -C 'gitlab@deployer.org'`.
Create .gitlab-ci.yml file with following content:
```yml
stages:
- deploy
deploy:
stage: deploy
image:
2022-03-22 20:39:45 +01:00
name: deployphp/deployer:7
entrypoint: [""]
before_script:
- mkdir -p ~/.ssh
- eval $(ssh-agent -s)
- echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
script:
- dep deploy -vvv
resource_group: production
only:
- master
```
2021-11-25 23:01:17 +01:00
### Deployment concurrency
Only one deployment job runs at a time with the [`resource_group` keyword](https://docs.gitlab.com/ee/ci/yaml/index.html#resource_group) in .gitlab-ci.yml.
2022-03-22 20:39:45 +01:00
In addition, you can ensure that older deployment jobs are cancelled automatically when a newer deployment runs by enabling the [skip outdated deployment jobs](https://docs.gitlab.com/ee/ci/pipelines/settings.html#skip-outdated-deployment-jobs) feature.
2021-11-25 23:01:17 +01:00
### Deploy secrets
2022-03-22 20:39:45 +01:00
Is not recommended committing secrets in the repository, you could use a GitLab variable to store them.
Many frameworks use dotenv to store secrets, let's create a GitLab file variable named `DOTENV`, so it can be deployed along with the code.
Set up a deployer task to copy secrets to the server:
```php
task('deploy:secrets', function () {
2022-03-22 20:39:45 +01:00
upload(getenv('DOTENV'), '{{deploy_path}}/shared/.env');
});
```
Run the task immediately after updating the code.