This is my gitlab pipeline setup. I have a deploy task that deploys the code and backup task that backups the database. But I keep getting this error:
jobs:backup config key may not be used with
rules
: only
There are two pipelines. I want to run backup IF the branch is master and WHATEVER environment variable is present. Similarly, I want to run deploy if the branch is master and WHATEVER environment variable is not present.
I am struggling with this. Appreciate any help or hint.
.gitlab-ci.yml
include:
- local: "ci/backup.gitlab-ci.yml"
- local: "ci/deploy.gitlab-ci.yml"
stages:
- deploy
- backup
deploy:
stage: deploy
rules:
- if: '$CI_COMMIT_BRANCH == "master"'
- if: "$WHATEVER"
when: never
script:
- echo "Running build"
backup:
stage: backup
rules:
- if: '$CI_COMMIT_BRANCH == "master"'
- if: "$WHATEVER"
script:
- echo "Running backup"
backup.gitlab-ci.yml
image: docker:latest
services:
- name: docker:dind
alias: docker
variables:
HEROKU_API_KEY: $HEROKU_TOKEN
APP_NAME: "milwaukee-internationals-core"
HEROKU_EMAIL: "whatever"
AWS_DEFAULT_REGION: "us-east-1"
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
BUCKET_NAME: milwaukee-internationals-backup
backup:
stage: backup
image: registry.gitlab.com/gitlab-org/cloud-deploy/aws-base:latest
resource_group: heroku
only:
- master
allow_failure: false
before_script:
- export CI_JOB_TIMESTAMP=$(date --utc -Iseconds)
- aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
- aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
- aws configure set default.region $AWS_DEFAULT_REGION
script:
- heroku login
- export PRESIGNED_URL=$(aws s3 presign s3://$BUCKET_NAME/$CI_JOB_TIMESTAMP --expires-in 3600)
- heroku pg:backups:capture --app $APP_NAME
- heroku pg:backups:url --app $APP_NAME | xargs -I{} wget -O "$CI_JOB_TIMESTAMP.dump" "{}"
- wget --method=PUT --body-file="$CI_JOB_TIMESTAMP.dump" $PRESIGNED_URL --header="Content-Type: application/octet-stream"
deploy.gitlab-ci.yml
image: docker:latest
services:
- name: docker:dind
alias: docker
variables:
HEROKU_API_KEY: $HEROKU_TOKEN
APP_NAME: "milwaukee-internationals-core"
DOCKER_TLS_CERTDIR: ""
DOCKER_HOST: "tcp://docker:2375"
HEROKU_REGISTRY: "registry.heroku.com"
HEROKU_EMAIL: "whatever"
deploy:
stage: deploy
resource_group: heroku
only:
- master
allow_failure: false
before_script:
- apk --no-cache add curl nodejs npm
- npm install -g heroku
script:
- heroku container:login
- heroku container:push --app $APP_NAME web
- heroku container:release --app $APP_NAME web