Slackbot
11/14/2023, 10:26 PMDeliush
11/15/2023, 9:59 AMrepos.yaml
repos:
- id: /.*/
# branch is an regex matching pull requests by base branch
# (the branch the pull request is getting merged into).
# By default, all branches are matched
branch: /.*/
pre_workflow_hooks:
- description: "Gate prod deployments within develop git branch"
run: |
if [ "${BASE_BRANCH_NAME}" = "develop" -a "${PROJECT_NAME}" = "prod" ]; then
echo "Can't run prod pipeline for develop branch"
exit 1
fi
repo_config_file: ./atlantis.yaml
apply_requirements: [approved, mergeable, undiverged]
import_requirements: [approved, mergeable, undiverged]
allowed_overrides: [workflow]
allowed_workflows: [custom]
allow_custom_workflows: true
# workflows lists server-side custom workflows
workflows:
custom:
plan:
steps:
- run:
command: echo this is branch ${BASE_BRANCH_NAME} and the project is ${PROJECT_NAME}
output: show
- env: <--- (NOTE I have to do this to expose the PROJECT_NAME otherwise it wouldnt work by referencing directly in the extra_args key)
name: ENVIRONMENT
command: echo ${PROJECT_NAME}
- init:
extra_args:
- -backend-config=environment/${ENVIRONMENT}/backend.tfvars
- plan:
extra_args:
- -var-file=environment/${ENVIRONMENT}/${ENVIRONMENT}.tfvars
atlantis.yaml
# atlantis.yaml
version: 3
# repos lists the config for specific repos.
projects:
# If two or more projects have the same dir and workspace, they must also have
# a 'name' key to differentiate them.
- name: dev
autoplan:
enabled: false
when_modified: ['*.tf*']
dir: .
workflow: custom
- name: prod
dir: .
autoplan:
enabled: false
when_modified: ['*.tf*']
workflow: customDeliush
11/15/2023, 10:25 AMatlantis.yaml config but it's not being honoured:
# atlantis.yaml
version: 3
# repos lists the config for specific repos.
projects:
# If two or more projects have the same dir and workspace, they must also have
# a 'name' key to differentiate them.
- name: dev
branch: /develop/
autoplan:
enabled: false
when_modified: ['*.tf*']
dir: .
workflow: custom
- name: preprod
branch: /develop/
autoplan:
enabled: false
when_modified: ['*.tf*']
dir: .
workflow: custom
- name: prod
branch: /main/
dir: .
autoplan:
enabled: false
when_modified: ['*.tf*']
workflow: custom
when I run atlantis plan -p prod inside a PR from source branch: feature/add_resource to destination branch: develop it still executes the plan for prod. I would expect that to fail because its not matching the base branchDeliush
11/15/2023, 6:43 PMPePe Amengual
11/15/2023, 6:44 PMPePe Amengual
11/15/2023, 6:44 PMPePe Amengual
11/15/2023, 6:46 PMPePe Amengual
11/15/2023, 6:47 PMPePe Amengual
11/15/2023, 6:47 PMbranch: branchnameDeliush
11/15/2023, 7:04 PMdevelop instead of /develop/ ? didn't follow that sorryDeliush
11/15/2023, 7:07 PMrun steps in the main workflow are executed with the following environment variables:
note: these variables are not available to pre or post workflows
so it seems like {PROJECT_NAME} would probably not be captured. Not sure why {BASE_BRANCH_NAME} is though, because its included in that listPePe Amengual
11/15/2023, 7:12 PMPePe Amengual
11/15/2023, 7:13 PMDeliush
11/15/2023, 7:13 PMDeliush
11/15/2023, 7:18 PMworkflows:
dev:
plan:
steps:
# needed otherwise TF will complain that backend changed between diff commands
- env:
name: ENVI
command: echo ${PROJECT_NAME}
- run: rm -rf .terraform
- run: 'if [ "${ENVI}" == "prod" ]; then exit 1;'
- init:
extra_args:
- -backend-config=environment/${ENVI}/backend.tfvars
- plan:
extra_args:
- -var-file=environment/${ENVI}/${ENVI}.tfvars
preprod:
plan:
steps:
# needed otherwise TF will complain that backend changed between diff commands
- env:
name: ENVI
command: echo ${PROJECT_NAME}
- run: rm -rf .terraform
- run: 'if [ "${ENVI}" == "prod" ]; then exit 1;'
- init:
extra_args:
- -backend-config=environment/${ENVI}/backend.tfvars
- plan:
extra_args:
- -var-file=environment/${ENVI}/${ENVI}.tfvars
prod:
plan:
steps:
# needed otherwise TF will complain that backend changed between diff commands
- env:
name: ENVI
command: echo ${PROJECT_NAME}
- run: rm -rf .terraform
- init:
extra_args:
- -backend-config=environment/${ENVI}/backend.tfvars
- plan:
extra_args:
- -var-file=environment/${ENVI}/${ENVI}.tfvars
I have added 3x workflows to represent the different environments, and i've added the following run in the preprod and dev:
- run: 'if [ "${ENVI}" == "prod" ]; then exit 1;'
I will test shortly to see if it worksPePe Amengual
11/15/2023, 7:23 PMPePe Amengual
11/15/2023, 7:24 PMDeliush
11/15/2023, 7:35 PMno project with name "prod" is defined in ./atlantis.yaml
this is the atlantis.yml:
# atlantis.yaml
version: 3
# repos lists the config for specific repos.
projects:
# If two or more projects have the same dir and workspace, they must also have
# a 'name' key to differentiate them.
- name: dev
branch: /develop/
autoplan:
enabled: false
when_modified: ['*.tf*']
repo_locking: true
dir: .
workflow: dev
- name: preprod
repo_locking: true
branch: /develop/
autoplan:
enabled: false
when_modified: ['*.tf*']
dir: .
workflow: preprod
- name: prod
repo_locking: true
branch: /main/
dir: .
autoplan:
enabled: false
when_modified: ['*.tf*']
workflow: prodDeliush
11/15/2023, 7:36 PMPePe Amengual
11/15/2023, 7:37 PMPePe Amengual
11/15/2023, 7:37 PMPePe Amengual
11/15/2023, 7:39 PMDeliush
11/15/2023, 7:40 PMDeliush
11/15/2023, 7:40 PMDeliush
11/15/2023, 7:40 PM# atlantis.yaml
version: 3
# repos lists the config for specific repos.
projects:
# If two or more projects have the same dir and workspace, they must also have
# a 'name' key to differentiate them.
- name: dev
branch: /develop/
autoplan:
enabled: false
when_modified: ['*.tf*']
repo_locking: true
dir: .
workflow: dev
- name: preprod
repo_locking: true
branch: /develop/
autoplan:
enabled: false
when_modified: ['*.tf*']
dir: .
workflow: preprod
- name: prod
repo_locking: true
branch: /main/
dir: .
autoplan:
enabled: false
when_modified: ['*.tf*']
workflow: prodDeliush
11/15/2023, 7:40 PMDeliush
11/15/2023, 7:41 PMrepos:
# id can either be an exact repo ID or a regex.
# If using a regex, it must start and end with a slash.
# Repo ID's are of the form {VCS hostname}/{org}/{repo name}, ex.
# <http://github.com/runatlantis/atlantis|github.com/runatlantis/atlantis>.
# this can be used for project specific settings if there's ever a use case for.
- id: /.*/
# branch is an regex matching pull requests by base branch
# (the branch the pull request is getting merged into).
# By default, all branches are matched
branch: /.*/
#pre_workflow_hooks:
#- description: "Running some checks"
#run: |
# if [ "${BASE_BRANCH_NAME}" = "develop" -a "${PROJECT_NAME}" = "prod" ]; then
# echo "Can't run prod pipeline for develop branch"
# exit 1
#fi
# repo_config_file specifies which repo config file to use for this repo.
# By default, atlantis.yaml is used.
repo_config_file: ./atlantis.yaml
# Meaning of different types of PR state
# Approved - SCM approval e.g. reviewer approving a PR ( must be in the list of codeowners, protected branch things)
# Mergeable - all checks must pass (defined in protected branch).
# UnDiverged - PR must be ahead of base branch!
# Requiring PR is approved before an applicable subcommand.
apply_requirements: [approved, mergeable, undiverged]
# Requiring PR is approved and mergeable (all checks are green e.g. CI) before an applicable subcommand.
import_requirements: [approved, mergeable, undiverged]
# workflow sets the workflow for all repos that match.
# This workflow must be defined in the workflows section.
# workflow: custom
# allowed_overrides specifies which keys can be overridden by this repo in
# its atlantis.yaml file.
allowed_overrides: [workflow]
# allowed_workflows specifies which workflows the repos that match
# are allowed to select.
allowed_workflows: [prod, preprod, dev]
# allow_custom_workflows defines whether this repo can define its own
# workflows. If false (default), the repo can only use server-side defined
# workflows.
# allow_custom_workflows: true
# delete_source_branch_on_merge defines whether the source branch would be deleted on merge
# If false (default), the source branch won't be deleted on merge
# delete_source_branch_on_merge: true
# repo_locking defines whether lock repository when planning.
# If true (default), atlantis try to get a lock.
# repo_locking: true
# custom_policy_check defines whether policy checking tools besides Conftest are enabled in checks
# If false (default), only Conftest JSON output is allowed
#custom_policy_check: false
# pre_workflow_hooks defines arbitrary list of scripts to execute before workflow execution.
#pre_workflow_hooks:
#- run: my-pre-workflow-hook-command arg1
# post_workflow_hooks defines arbitrary list of scripts to execute after workflow execution.
#post_workflow_hooks:
#- run: my-post-workflow-hook-command arg1
# policy_check defines if policy checking should be enable on this repository.
#policy_check: false
# workflows lists server-side custom workflows
workflows:
dev:
plan:
steps:
# needed otherwise TF will complain that backend changed between diff commands
- env:
name: ENVIRONMENT
command: echo ${PROJECT_NAME}
- run: rm -rf .terraform
- run: 'echo Checking if there is an attempt to push to prod && if [ "${ENVIRONMENT}" == "prod" ]; then exit 1; fi;'
- init:
extra_args:
- -backend-config=environment/dev/backend.tfvars
- plan:
extra_args:
- -var-file=environment/dev/dev.tfvars
preprod:
plan:
steps:
# needed otherwise TF will complain that backend changed between diff commands
- env:
name: ENVIRONMENT
command: echo ${PROJECT_NAME}
- run: rm -rf .terraform
- run: 'echo Checking if there is an attempt to push to prod && if [ "${ENVIRONMENT}" == "prod" ]; then exit 1; fi;'
- init:
extra_args:
- -backend-config=environment/preprod/backend.tfvars
- plan:
extra_args:
- -var-file=environment/preprod/preprod.tfvars
prod:
plan:
steps:
# needed otherwise TF will complain that backend changed between diff commands
- run: rm -rf .terraform
- init:
extra_args:
- -backend-config=environment/prod/backend.tfvars
- plan:
extra_args:
- -var-file=environment/prod/prod.tfvarsPePe Amengual
11/15/2023, 7:42 PMDeliush
11/15/2023, 7:42 PMPePe Amengual
11/15/2023, 7:42 PMDeliush
11/15/2023, 7:43 PMPePe Amengual
11/15/2023, 7:43 PMDeliush
11/15/2023, 7:44 PMDeliush
11/15/2023, 7:46 PMDeliush
11/15/2023, 7:47 PM# workflows lists server-side custom workflows
workflows:
dev:
plan:
steps:
# needed otherwise TF will complain that backend changed between diff commands
- env:
name: ENVIRONMENT
command: echo ${PROJECT_NAME}
- run: rm -rf .terraform
# - run: 'echo Checking if there is an attempt to push to prod && if [ "${ENVIRONMENT}" == "prod" ]; then exit 1; fi;'
- init:
extra_args:
- -backend-config=environment/dev/backend.tfvars
- plan:
extra_args:
- -var-file=environment/dev/dev.tfvars
preprod:
plan:
steps:
# needed otherwise TF will complain that backend changed between diff commands
- env:
name: ENVIRONMENT
command: echo ${PROJECT_NAME}
- run: rm -rf .terraform
# - run: 'echo Checking if there is an attempt to push to prod && if [ "${ENVIRONMENT}" == "prod" ]; then exit 1; fi;'
- init:
extra_args:
- -backend-config=environment/preprod/backend.tfvars
- plan:
extra_args:
- -var-file=environment/preprod/preprod.tfvarsDeliush
11/15/2023, 7:47 PMDeliush
11/15/2023, 7:51 PMDeliush
11/15/2023, 7:52 PMatlantis plan -p prod inside a PR to develop then I get:
no project with name "prod" is defined in ./atlantis.yaml
in atlantis.yaml I have prod project configured as such:
- name: prod
repo_locking: true
branch: /main/
dir: .
autoplan:
enabled: false
when_modified: ['*.tf*']
workflow: prod
as soon as I change from branch: /main/ to branch: /develop/ it worksDeliush
11/15/2023, 7:52 PMPePe Amengual
11/15/2023, 8:05 PMPePe Amengual
11/15/2023, 8:07 PM/branchname/ everything works?Deliush
11/15/2023, 8:11 PMDeliush
11/15/2023, 8:13 PM{"level":"error","ts":"2023-11-15T20:13:08.976Z","caller":"events/pull_updater.go:17","msg":"projects: (0: (branch: regex must begin and end with a slash '/'.); 1: (branch: regex must begin and end with a slash '/'.); 2: (branch: regex must begin and end with a slash '/'.).).",Deliush
11/15/2023, 8:16 PM- name: dev
repo_locking: true
branch: /develop/
- name: preprod
repo_locking: true
branch: /develop/
- name: prod
repo_locking: true
branch: /main/
as you can see dev is set to develop, and prod is set to main branch.
if I try to run atlantis plan -p prod inside a PR to develop, then I get this error: no project with name "prod" is defined in ./atlantis.yaml
If i change the combination of project name and branch name for prod to:
- name: prod
repo_locking: true
branch: /develop/
then atlantis plan -p prod doesn't fail.PePe Amengual
11/15/2023, 8:16 PM/ then it matches the branch?Deliush
11/15/2023, 8:16 PMPePe Amengual
11/15/2023, 8:16 PMDeliush
11/15/2023, 8:16 PMDeliush
11/15/2023, 8:17 PMDeliush
11/15/2023, 8:17 PMDeliush
11/15/2023, 8:17 PMPePe Amengual
11/15/2023, 8:18 PMDeliush
11/15/2023, 8:18 PMDeliush
11/15/2023, 8:18 PMPePe Amengual
11/15/2023, 8:18 PM/main/PePe Amengual
11/15/2023, 8:18 PMDeliush
11/15/2023, 8:19 PMRegex matching projects by the base branch of pull request (the branch the pull request is getting merged into). Only projects that match the PR's branch will be considered. By default, all branches are matched.Deliush
11/15/2023, 8:19 PMDeliush
11/15/2023, 8:20 PMPePe Amengual
11/15/2023, 8:20 PMPePe Amengual
11/15/2023, 8:20 PMPePe Amengual
11/15/2023, 8:21 PMbased on mainDeliush
11/15/2023, 8:21 PMPePe Amengual
11/15/2023, 8:21 PMPePe Amengual
11/15/2023, 8:21 PM- name: dev
repo_locking: true
branch: /develop/
- name: preprod
repo_locking: true
branch: /develop/
- name: prod
repo_locking: true
branch: /prod/Deliush
11/15/2023, 8:22 PMsource: feature/add_resource
destination: develop
would Atlantis set it to be feature/add_resource?PePe Amengual
11/15/2023, 8:22 PMDeliush
11/15/2023, 8:22 PMPePe Amengual
11/15/2023, 8:22 PMPePe Amengual
11/15/2023, 8:23 PMPePe Amengual
11/15/2023, 8:23 PMbranch: /branchname/ is a regexPePe Amengual
11/15/2023, 8:23 PMPePe Amengual
11/15/2023, 8:24 PMbranch: /dev|preprod|testing/Deliush
11/15/2023, 8:24 PMBASE_BRANCH_NAME - Name of the base branch of the pull request (the branch that the pull request is getting merged into)PePe Amengual
11/15/2023, 8:24 PMDeliush
11/15/2023, 8:24 PMDeliush
11/15/2023, 8:24 PMDeliush
11/15/2023, 8:25 PMPePe Amengual
11/15/2023, 8:25 PMDeliush
11/15/2023, 8:28 PMif [ "${ENVIRONMENT}" == "prod" ]; then exit 1; fi;
to
if [ "${ENVIRONMENT}" = "prod" -a "${BASE_BRANCH_NAME} = "develop" ]; then exit 1; fi;Deliush
11/15/2023, 8:28 PMDeliush
11/15/2023, 8:29 PMPePe Amengual
11/15/2023, 8:29 PMDeliush
11/15/2023, 8:30 PMDeliush
11/15/2023, 8:31 PMDeliush
11/15/2023, 8:31 PMPePe Amengual
11/15/2023, 8:31 PMPePe Amengual
11/15/2023, 8:31 PMDeliush
11/15/2023, 8:32 PMDeliush
11/15/2023, 8:32 PMDeliush
11/15/2023, 8:32 PMDeliush
11/15/2023, 8:33 PMPePe Amengual
11/15/2023, 8:34 PMDeliush
11/15/2023, 8:34 PMDeliush
11/15/2023, 8:34 PMDeliush
11/15/2023, 8:45 PMif [ "${ENVIRONMENT}" = "prod" -a "${BASE_BRANCH_NAME}" = "develop" ]; then exit 1; fi;'
but i will look other alternatives, I just tested and the above worked. Thank you for your help!PePe Amengual
11/15/2023, 8:48 PMDeliush
11/15/2023, 8:48 PM