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

Add support for partial cloud config block #268

Merged
merged 13 commits into from
Jun 18, 2023
Merged
6 changes: 3 additions & 3 deletions .github/workflows/test-apply.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ jobs:
fi

cat "${{ steps.apply.outputs.json_plan_path }}"
if [[ $(jq -r .format_version "${{ steps.apply.outputs.json_plan_path }}") != "1.1" ]]; then
if [[ $(jq -r .format_version "${{ steps.apply.outputs.json_plan_path }}") != "1.2" ]]; then
echo "::error:: json_plan_path not set correctly"
exit 1
fi
Expand Down Expand Up @@ -227,7 +227,7 @@ jobs:
exit 1
fi

if [[ $(jq -r .format_version "${{ steps.second-apply.outputs.json_plan_path }}") != "1.1" ]]; then
if [[ $(jq -r .format_version "${{ steps.second-apply.outputs.json_plan_path }}") != "1.2" ]]; then
echo "::error:: json_plan_path not set correctly"
exit 1
fi
Expand Down Expand Up @@ -625,7 +625,7 @@ jobs:
fi

cat "${{ steps.apply.outputs.json_plan_path }}"
if [[ $(jq -r .format_version "${{ steps.apply.outputs.json_plan_path }}") != "1.1" ]]; then
if [[ $(jq -r .format_version "${{ steps.apply.outputs.json_plan_path }}") != "1.2" ]]; then
echo "::error:: json_plan_path not set correctly"
exit 1
fi
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-plan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
fi

cat '${{ steps.plan.outputs.json_plan_path }}'
if [[ $(jq -r .format_version "${{ steps.plan.outputs.json_plan_path }}") != "1.1" ]]; then
if [[ $(jq -r .format_version "${{ steps.plan.outputs.json_plan_path }}") != "1.2" ]]; then
echo "::error:: json_plan_path not set correctly"
exit 1
fi
Expand Down Expand Up @@ -69,7 +69,7 @@ jobs:
- name: Verify outputs
run: |
cat '${{ steps.plan.outputs.json_plan_path }}'
if [[ $(jq -r .format_version "${{ steps.plan.outputs.json_plan_path }}") != "1.1" ]]; then
if [[ $(jq -r .format_version "${{ steps.plan.outputs.json_plan_path }}") != "1.2" ]]; then
echo "::error:: json_plan_path not set correctly"
exit 1
fi
Expand Down
47 changes: 46 additions & 1 deletion .github/workflows/test-version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,51 @@ jobs:
exit 1
fi

tfc_partial_cloud_workspace:
runs-on: ubuntu-latest
name: Partial TFC Cloud Configuration
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Create workspace
uses: ./terraform-new-workspace
env:
TERRAFORM_VERSION: 1.2.1
TERRAFORM_CLOUD_TOKENS: app.terraform.io=${{ secrets.TF_API_TOKEN }}
TF_CLOUD_ORGANIZATION: flooktech
with:
path: tests/workflows/test-version/partial-cloud
workspace: tfc_partial_cloud_workspace-1

- name: Test terraform-version
uses: ./terraform-version
id: terraform-version
env:
TERRAFORM_CLOUD_TOKENS: app.terraform.io=${{ secrets.TF_API_TOKEN }}
TF_CLOUD_ORGANIZATION: flooktech
with:
path: tests/workflows/test-version/partial-cloud
workspace: tfc_partial_cloud_workspace-1

- name: Destroy workspace
uses: ./terraform-destroy-workspace
env:
TERRAFORM_CLOUD_TOKENS: app.terraform.io=${{ secrets.TF_API_TOKEN }}
TF_CLOUD_ORGANIZATION: flooktech
with:
path: tests/workflows/test-version/partial-cloud
workspace: tfc_partial_cloud_workspace-1

- name: Print the version
run: |
echo "The terraform version was ${{ steps.terraform-version.outputs.terraform }}"

if [[ "${{ steps.terraform-version.outputs.terraform }}" != "1.2.1" ]]; then
echo "::error:: Terraform version not set from remote workspace"
exit 1
fi

local_state:
runs-on: ubuntu-latest
name: Local State file
Expand Down Expand Up @@ -398,7 +443,7 @@ jobs:

- name: Check the version
run: |
if [[ "${{ steps.terraform-version.outputs.terraform }}" != *"1.4"* ]]; then
if [[ "${{ steps.terraform-version.outputs.terraform }}" != *"1.5"* ]]; then
echo "::error:: Latest version was not used"
exit 1
fi
Expand Down
2 changes: 1 addition & 1 deletion image/entrypoints/destroy-workspace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ if [[ $DESTROY_EXIT -eq 1 ]]; then
exit 1
fi

if [[ "$TERRAFORM_BACKEND_TYPE" == "remote" ]]; then
if [[ "$TERRAFORM_BACKEND_TYPE" == "remote" || "$TERRAFORM_BACKEND_TYPE" == "cloud" ]]; then
terraform-cloud-workspace delete "$INPUT_WORKSPACE"
else
# We can't delete an active workspace, so re-initialize with a 'default' workspace (which may not exist)
Expand Down
2 changes: 1 addition & 1 deletion image/entrypoints/new-workspace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ source /usr/local/actions.sh
debug
setup

if [[ "$TERRAFORM_BACKEND_TYPE" == "remote" ]]; then
if [[ "$TERRAFORM_BACKEND_TYPE" == "remote" || "$TERRAFORM_BACKEND_TYPE" == "cloud" ]]; then
TERRAFORM_VERSION="$TERRAFORM_VER_MAJOR.$TERRAFORM_VER_MINOR.$TERRAFORM_VER_PATCH" terraform-cloud-workspace new "$INPUT_WORKSPACE"
exit 0
fi
Expand Down
2 changes: 1 addition & 1 deletion image/entrypoints/validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ setup
# terraform.workspace will be evaluated during a validate, but it is not initialized properly.
# Pass through the workspace input, except for remote backend where it should be 'default'

if [[ "$TERRAFORM_BACKEND_TYPE" == "remote" ]]; then
if [[ "$TERRAFORM_BACKEND_TYPE" == "remote" || "$TERRAFORM_BACKEND_TYPE" == "cloud" ]]; then
TF_WORKSPACE="default"
else
TF_WORKSPACE="$INPUT_WORKSPACE"
Expand Down
2 changes: 2 additions & 0 deletions image/src/github_pr_comment/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ def get_comment(action_inputs: PlanPrInputs, backend_fingerprint: bytes, backup_
}

if backend_type := os.environ.get('TERRAFORM_BACKEND_TYPE'):
if backend_type == 'cloud':
backend_type = 'remote'
headers['backend_type'] = backend_type

headers['label'] = os.environ.get('INPUT_LABEL') or None
Expand Down
10 changes: 7 additions & 3 deletions image/src/terraform/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,16 @@ def get_cloud_config(module: TerraformModule, cli_config_path: Path) -> Optional

if 'hostname' in cloud:
backend_config['hostname'] = cloud['hostname']
elif 'TF_CLOUD_HOSTNAME' in os.environ:
backend_config['hostname'] = os.environ['TF_CLOUD_HOSTNAME']

backend_config['organization'] = cloud.get('organization')
backend_config['organization'] = cloud.get('organization', os.environ.get('TF_CLOUD_ORGANIZATION'))
backend_config['token'] = cloud.get('token')

if cloud.get('workspaces', []):
if 'workspaces' in cloud:
backend_config['workspaces'] = cloud['workspaces'][0]
elif 'INPUT_WORKSPACE' in os.environ:
backend_config['workspaces'] = BackendConfigWorkspaces(name=os.environ['INPUT_WORKSPACE'])

if not found:
return None
Expand All @@ -248,7 +252,7 @@ def get_backend_type(module: TerraformModule) -> Optional[str]:

for terraform in module.get('terraform', []):
if 'cloud' in terraform:
return 'remote'
return 'cloud'

return 'local'

Expand Down
2 changes: 1 addition & 1 deletion image/src/terraform_version/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def determine_version(inputs: InitInputs, cli_config_path: Path, actions_env: Ac
# arm64 support was introduced in 0.13.5
versions = list(apply_constraints(versions, [Constraint('>=0.13.5')]))

if backend_type not in ['remote', 'local']:
if backend_type not in ['remote', 'cloud', 'local']:
if version := try_guess_state_version(inputs, module, versions):
sys.stdout.write('Using the same terraform version that wrote the existing remote state file\n')
return version
Expand Down
4 changes: 4 additions & 0 deletions tests/workflows/test-version/partial-cloud/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
terraform {
cloud {
}
}