Skip to content

Commit

Permalink
DAT-16244 DevOps :: Spacelift replacement of Terraform Cloud (#104)
Browse files Browse the repository at this point in the history
* chore(generate.yml): add environment variables for Spacelift API key to create-action-repo job
chore(generate.yml): remove TF_VAR_BOT_TOKEN environment variable from plan and apply steps
chore(main.tf): update terraform backend configuration to use remote backend on spacelift.io for the liquibase organization and workspace "liquibase-github-actions"

* chore(main.tf): remove unused backend configuration in terraform block

* chore(main.tf): add remote backend configuration for Terraform to use Spacelift.io as the backend
feat(main.tf): configure Terraform to use the "liquibase-github-actions" workspace in the "liquibase" organization on Spacelift.io as the backend

* chore(generate.yml): remove unnecessary Terraform setup step

The Terraform setup step was removed because it is not needed for the current workflow.

* chore(generate.yml): update environment variable names for Spacelift API key
fix(generate.yml): add -lock=false flag to terraform plan and terraform apply commands to prevent locking the state file during execution

* chore(generate.yml): add environment variable TF_VAR_BOT_TOKEN to provide the admin token for the generator action repository
chore(generate.yml): add environment variable TF_TOKEN_spacelift_io to provide the Spacelift API key for the generator action repository

* chore(main.tf): remove unused backend configuration in terraform file

* chore(main.tf): add remote backend configuration for Terraform to use Spacelift.io as the backend
feat(main.tf): configure Terraform to use the "liquibase-github-actions" workspace in the "liquibase" organization on Spacelift.io as the backend

* fix(generate.yml): remove unnecessary -lock=false flag from terraform apply command

The -lock=false flag is not necessary for the terraform apply command. Removing it improves the readability of the command and avoids confusion.

* fix(generate.yml): add -lock=false flag to terraform apply command to prevent locking the state file during apply
feat(generate.yml): add Terraform State Push step to push errored.tfstate file to remote state backend, even if previous steps failed

* fix(generate.yml): update Terraform State Push step to disable locking to prevent errors during state push

* fix(generate.yml): add -force flag to terraform state push command to force push the errored.tfstate file

* fix(generate.yml): add cat command to print contents of errored.tfstate before pushing state to Terraform backend

The `generate.yml` workflow file was modified to add a `cat` command before pushing the state to the Terraform backend. This change was made to print the contents of the `errored.tfstate` file for debugging purposes.

* fix(generate.yml): increment serial number and update terraform version in errored.tfstate file

The `generate.yml` workflow file was updated to fix an issue with the `Terraform State Push` step. The following changes were made:

- Added a command to display the contents of the `errored.tfstate` file.
- Added variables `STATE_FILE`, `current_serial`, and `new_serial` to store the current and new serial numbers.
- Incremented the current serial number by 1 and stored it in `new_serial`.
- Updated the `errored.tfstate` file by using `jq` to add the new serial number and update the terraform version.
- Added a command to display the contents of the updated `errored.tfstate` file.
- Updated the `terraform state push` command to use the updated `errored.tfstate` file with the lock disabled and force enabled.

* chore(main.tf): add required_version to terraform block to enforce a specific version of Terraform (1.5.7)
chore(main.tf): update backend hostname and organization to match new spacelift.io configuration

* downgrade terraform version for spacelift

* chore(generate.yml): comment out unused steps in the workflow file

The unused steps in the generate.yml workflow file have been commented out to improve readability and prevent unnecessary execution of those steps.

* chore(generate.yml): update hashicorp/setup-terraform action to version 3 to ensure compatibility with the latest features and improvements
chore(generate.yml): remove unused cli_config_credentials_token parameter from hashicorp/setup-terraform action

* chore(main.tf): remove unused backend configuration for remote state management
chore(main.tf): remove unnecessary workspace configuration for remote state management

* chore(main.tf): add remote backend configuration for Terraform state management to store state in spacelift.io organization and workspace "liquibase-github-actions"
feat(main.tf): update github_repository resource to use for_each to create multiple repositories based on local.commands, and improve resource configuration formatting for better readability

* chore(main.tf): remove unused backend configuration to simplify the Terraform configuration
fix(main.tf): update the owner of the GitHub provider to "liquibase" to match the correct organization
refactor(main.tf): remove unused resource "github_repository" to clean up the Terraform configuration

* chore(main.tf): update provider owner from "liquibase" to "liquibase-github-actions" for better clarity and accuracy
feat(main.tf): add resource block to create GitHub repositories based on commands defined in commands.json file, with appropriate naming and descriptions

* chore(.gitignore): remove unused 'commands.json' file from being tracked by git

feat(commands.json): add 'commands.json' file containing a list of available commands for the application

* chore(main.tf): update commands list in locals to include all available commands

The commands list in locals was updated to include all available commands for the Liquibase GitHub Actions. This ensures that all commands are accounted for and can be used in the Terraform configuration.

* chore(main.tf): refactor commands variable to use double quotes for consistency and readability

* chore(.gitignore): add commands.json to the list of ignored files
feat(main.tf): add remote backend configuration for Terraform to store state in spacelift.io organization workspace "liquibase-github-actions"
refactor(main.tf): replace hardcoded commands list with reading commands from commands.json file

* fix(generate.yml): add -lock=false flag to the terraform plan command to disable locking and prevent potential issues with concurrent runs

* chore(generate.yml): uncomment Terraform Apply and Terraform State Push steps

chore(generate.yml): uncomment generate-action and output-action steps

The Terraform Apply and Terraform State Push steps were uncommented to enable the execution of Terraform apply and state push commands. This is necessary for the deployment and management of infrastructure resources.

The generate-action and output-action steps were also uncommented to enable the generation of commands and the output of the action edit link. These steps are crucial for the overall workflow and automation process.

* chore(generate.yml): remove Terraform State Push step from the workflow

The Terraform State Push step was removed from the workflow as it was causing errors and is no longer needed.

* chore(generate.yml): remove trailing whitespace to improve code readability

* chore(terraform): add .terraform.lock.hcl file to track provider version and constraints

A new file `.terraform.lock.hcl` has been added to the repository. This file is automatically maintained by `terraform init` command and should not be manually edited. It tracks the version and constraints of the provider `registry.terraform.io/integrations/github`. The current version is `4.31.0` and the constraints are set to `~> 4.0`. The file includes a list of hashes for the provider's artifacts to ensure integrity and security.

* chore(.terraform.lock.hcl): add newline at the end of the file for consistency and to adhere to best practices

* fix(generate.yml): add `-reconfigure` flag to `terraform init` command to ensure proper initialization of the Terraform workspace
feat(generate.yml): add steps to pull and push the current state file to/from the Terraform backend to ensure consistency and synchronization

* fix(generate.yml): add -lock=false flag to the terraform state push command to disable locking when pushing state

* fix(generate.yml): change output file name from current_state.tfstate to current.tfstate in the Terraform Pull state step
fix(generate.yml): add terraform init -force-copy -backend=false command in the Terraform Pull state step to ensure a clean initialization
fix(generate.yml): add terraform init -reconfigure command in the Terraform Push state step to reconfigure the backend before pushing the state
fix(generate.yml): change output file name from current_state.tfstate to current.tfstate in the Terraform Push state step

* chore(generate.yml): update workflow to install spacectl instead of terraform and add support for local preview of infrastructure changes

chore(main.tf): remove backend configuration for remote state as it is no longer needed with the switch to spacectl

* fix(generate.yml): update spacectl command to use 'preview' instead of 'local-preview' for consistency with other commands

* chore(generate.yml): comment out unused workflow steps to improve readability and reduce noise in the file

* fix(generate.yml): change 'spacectl stack preview' command to 'spacectl stack local-preview' to run the stack locally instead of in the cloud

* fix(main.tf): fix file path for commands.json to be relative to the current directory instead of the module directory

* chore(generate.yml): add ls -ltr command before running spacectl stack local-preview to debug the issue
fix(main.tf): fix the path to commands.json file to use the module path for better reliability

* chore(.gitignore): remove unused "commands.json" file from git tracking

* chore(generate.yml): remove commands.json from .gitignore to allow uploading the file to Spacelift
chore(generate.yml): update comment to explain the reason for removing commands.json from .gitignore
chore(gitignore): remove newline at end of file

* chore(generate.yml): uncomment deploy infrastructure step to enable deployment of infrastructure
chore(generate.yml): uncomment generate-action and output-action steps to enable generation and output of action

* chore(generate.yml): remove commands.json from .gitignore to allow uploading it to Spacelift
fix(generate.yml): update spacectl commands to include necessary flags for local preview and deployment

* fix(generate.yml): add condition to deploy infrastructure and generate action jobs to only run on the main branch

* fix(generate.yml): update spacectl stack deploy command to include setting current commit before deployment

The previous command only deployed the stack without setting the current commit. This caused issues with tracking the deployed version. The updated command now sets the current commit to the latest commit SHA before deploying the stack.
  • Loading branch information
jandroav authored Jan 5, 2024
1 parent f6c751d commit 48d2613
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 55 deletions.
40 changes: 23 additions & 17 deletions .github/workflows/generate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ jobs:
path: commands.json

create-action-repo:
env:
TF_VAR_BOT_TOKEN: ${{ secrets.GHA_GENERATOR_ADMIN_TOKEN }}
TF_TOKEN_spacelift_io: ${{ secrets.SPACELIFT_API_KEY }}

runs-on: ubuntu-latest
needs: [ create-command-list ]
steps:
- uses: actions/checkout@v3

- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
with:
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
- name: Install spacectl
uses: spacelift-io/setup-spacectl@main
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- uses: actions/download-artifact@v3
with:
Expand All @@ -53,25 +57,27 @@ jobs:
id: validate
run: terraform validate -no-color

- name: Terraform Plan
id: plan
run: terraform plan -no-color
continue-on-error: true
- name: Preview infrastructure
env:
TF_VAR_BOT_TOKEN: ${{ secrets.GHA_GENERATOR_ADMIN_TOKEN }}
SPACELIFT_API_KEY_ENDPOINT: ${{ secrets.SPACELIFT_API_KEY_ENDPOINT }}
SPACELIFT_API_KEY_ID: ${{ secrets.SPACELIFT_API_KEY_ID }}
SPACELIFT_API_KEY_SECRET: ${{ secrets.SPACELIFT_API_KEY_SECRET }}
run: spacectl stack local-preview --id liquibase-github-actions --disregard-gitignore=true

- name: Terraform Plan Status
if: steps.plan.outcome == 'failure'
run: exit 1

- name: Terraform Apply
run: terraform apply -auto-approve
- name: Deploy infrastructure
if: github.ref == 'refs/heads/main'
env:
TF_VAR_BOT_TOKEN: ${{ secrets.GHA_GENERATOR_ADMIN_TOKEN }}
SPACELIFT_API_KEY_ENDPOINT: ${{ secrets.SPACELIFT_API_KEY_ENDPOINT }}
SPACELIFT_API_KEY_ID: ${{ secrets.SPACELIFT_API_KEY_ID }}
SPACELIFT_API_KEY_SECRET: ${{ secrets.SPACELIFT_API_KEY_SECRET }}
run: |
spacectl stack set-current-commit --id liquibase-github-actions --commit ${{ github.sha }}
spacectl stack deploy --id liquibase-github-actions --auto-confirm
generate-action:
runs-on: ubuntu-latest
needs: [ create-command-list, create-action-repo ]
if: github.ref == 'refs/heads/main'
strategy:
matrix:
commands: ${{ fromJSON(needs.create-command-list.outputs.matrix) }}
Expand All @@ -98,7 +104,7 @@ jobs:
run: ./scripts/push-to-repository.sh "${{ matrix.commands }}" $LIQUIBASE_VERSION
env:
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}

output-action:
runs-on: ubuntu-latest
needs: [ generate-action ]
Expand Down
24 changes: 0 additions & 24 deletions .terraform.lock.hcl

This file was deleted.

22 changes: 8 additions & 14 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
terraform {
cloud {
organization = "liquibase"
workspaces {
name = "liquibase-github-actions"
}
}
required_providers {
github = {
source = "integrations/github"
Expand All @@ -23,12 +17,12 @@ locals {
}

resource "github_repository" "liquibase-github-actions" {
for_each = toset(local.commands)
name = replace(each.key, " ", "-")
description = "Official GitHub Action to run Liquibase ${title(replace(each.key, "-", " "))}"
visibility = "public"
has_downloads = false
has_issues = false
has_projects = false
has_wiki = false
for_each = toset(local.commands)
name = replace(each.key, " ", "-")
description = "Official GitHub Action to run Liquibase ${title(replace(each.key, "-", " "))}"
visibility = "public"
has_downloads = false
has_issues = false
has_projects = false
has_wiki = false
}

0 comments on commit 48d2613

Please sign in to comment.