Skip to content

Commit

Permalink
Merge pull request #475 from hashicorp/instance-workflows
Browse files Browse the repository at this point in the history
Workflow job that rebuilds tflocal
  • Loading branch information
sebasslash committed Aug 3, 2022
2 parents 66ef8bc + 2a85ccc commit b6a0fe9
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .github/scripts/build-tflocal/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module build-tflocal

go 1.18

require (
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.1 // indirect
github.com/hashicorp/go-slug v0.9.1 // indirect
github.com/hashicorp/go-tfe v1.5.0 // indirect
github.com/hashicorp/jsonapi v0.0.0-20210826224640-ee7dae0fb22d // indirect
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect
)
21 changes: 21 additions & 0 deletions .github/scripts/build-tflocal/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-retryablehttp v0.7.1 h1:sUiuQAnLlbvmExtFQs72iFW/HXeUn8Z1aJLQ4LJJbTQ=
github.com/hashicorp/go-retryablehttp v0.7.1/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/go-slug v0.9.1 h1:gYNVJ3t0jAWx8AT2eYZci3Xd7NBHyjayW9AR1DU4ki0=
github.com/hashicorp/go-slug v0.9.1/go.mod h1:Ib+IWBYfEfJGI1ZyXMGNbu2BU+aa3Dzu41RKLH301v4=
github.com/hashicorp/go-tfe v1.5.0 h1:MtABkqH2s6lRFl8HaGt0qESLGAyrmMAFfecsEm+13K8=
github.com/hashicorp/go-tfe v1.5.0/go.mod h1:E8a90lC4kjU5Lc2c0D+SnWhUuyuoCIVm4Ewzv3jCD3A=
github.com/hashicorp/jsonapi v0.0.0-20210826224640-ee7dae0fb22d h1:9ARUJJ1VVynB176G1HCwleORqCaXm/Vx0uUi0dL26I0=
github.com/hashicorp/jsonapi v0.0.0-20210826224640-ee7dae0fb22d/go.mod h1:Yog5+CPEM3c99L1CL2CFCYoSzgWm5vTU58idbRUaLik=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 h1:Dpdu/EMxGMFgq0CeYMh4fazTD2vtlZRYE7wyynxJb9U=
golang.org/x/time v0.0.0-20220609170525-579cf78fd858/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
56 changes: 56 additions & 0 deletions .github/scripts/build-tflocal/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"context"
"fmt"
"log"
"os"
"os/signal"

tfe "github.com/hashicorp/go-tfe"
)

// replaceInstance contains the state target that will be forcibly replaced every run
const replaceInstance = "module.tflocal.module.tfbox.aws_instance.tfbox"

func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

if len(os.Args) < 3 {
log.Fatal("usage: <organization-name> <workspace-name>")
}
organizationName := os.Args[1]
workspaceName := os.Args[2]

if err := triggerRun(ctx, organizationName, workspaceName); err != nil {
log.Fatal(err)
}
}

func triggerRun(ctx context.Context, organizationName, workspaceName string) error {
config := &tfe.Config{Token: os.Getenv("TFE_TOKEN")}

client, err := tfe.NewClient(config)
if err != nil {
return fmt.Errorf("client initialization error: %w", err)
}

wk, err := client.Workspaces.Read(ctx, organizationName, workspaceName)
if err != nil {
return fmt.Errorf("failed to read workspace: %w", err)
}

run, err := client.Runs.Create(ctx, tfe.RunCreateOptions{
IsDestroy: tfe.Bool(false),
Message: tfe.String("Queued nightly from tflocal-cloud GH Actions via go-tfe"),
Workspace: wk,
ReplaceAddrs: []string{replaceInstance},
})
if err != nil {
return fmt.Errorf("failed to trigger run: %w", err)
}

fmt.Println("Created run: " + run.ID)
return nil
}
23 changes: 23 additions & 0 deletions .github/workflows/tflocal-instance.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: instance
on:
schedule:
- cron: 0 0 * * *
jobs:
terraform:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: go.mod
check-latest: true
cache: true

- name: Rebuild tflocal
env:
TFE_TOKEN: ${{ secrets.TF_WORKFLOW_TFLOCAL_CLOUD_TFC_TOKEN }}
run: |
cd .github/scripts/rebuild-tflocal
go run main.go hashicorp-v2 tflocal-go-tfe

0 comments on commit b6a0fe9

Please sign in to comment.