Skip to content

Commit

Permalink
create workspace directly via api to ensure it's in the right project
Browse files Browse the repository at this point in the history
debugging
  • Loading branch information
jeremywmoore committed Jul 11, 2024
1 parent 7c534c6 commit 9544188
Showing 1 changed file with 49 additions and 23 deletions.
72 changes: 49 additions & 23 deletions .github/workflows/dev_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
types: [labeled, closed]

env:
TERRAFORM_CLOUD_TOKENS: app.terraform.io=${{ secrets.TERRAFORM_API_TOKEN }}
TERRAFORM_CLOUD_TOKENS: app.terraform.io=${{ secrets.HUSHLINE_DEV_TF_TOKEN }}
GITHUB_TOKEN: ${{ github.token }}
WORKSPACE_NAME: hushline-dev-${{ github.head_ref }}
TF_PROJECT_HUSH_LINE_DEV: prj-iEruEQFmaNTCRAtA
Expand All @@ -26,33 +26,59 @@ jobs:
repository: ${{ env.HUSHLINE_INFRA_REPO }}
ref: ${{ env.HUSHLINE_INFRA_REF }}
token: ${{ secrets.HUSHLINE_INFRA_TOKEN }}

- name: Use branch workspace
uses: dflook/terraform-new-workspace@v1.43.0
with:
path: ${{ env.DEV_TF_PATH }}
workspace: ${{ env.WORKSPACE_NAME }}

- name: Update workspace attributes

- name: Create workspace
run: |
curl -X PATCH https://app.terraform.io/api/v2/organizations/science-and-design/workspaces/${{ env.WORKSPACE_NAME }} \
set -e
resp=$(curl -s -o /dev/null -w "%{http_code}" \
-X GET https://app.terraform.io/api/v2/organizations/science-and-design/workspaces/${{ env.WORKSPACE_NAME }} \
-H 'Content-Type: application/vnd.api+json' \
-H 'Authorization: Bearer ${{ secrets.TERRAFORM_API_TOKEN }}' \
-d '{
"data": {
"type": "workspaces",
"attributes": {
"working-directory": "${{ env.DEV_TF_PATH }}"
},
"relationships": {
"project": {
"data": {
"id": "${{ env.TF_PROJECT_HUSH_LINE_DEV }}"
}
-H 'Authorization: Bearer ${{ secrets.HUSHLINE_DEV_TF_TOKEN }}')
echo "Got $resp when looking up workspace"
if [[ "$resp" == '404' ]]; then
echo "Attempting to create workspace..."
resp=$(curl -X POST https://app.terraform.io/api/v2/organizations/science-and-design/workspaces \
-H 'Content-Type: application/vnd.api+json' \
-H 'Authorization: Bearer ${{ secrets.HUSHLINE_DEV_TF_TOKEN }}' \
-d '{
"data": {
"type": "workspaces",
"attributes": {
"working-directory": "${{ env.DEV_TF_PATH }}",
"name": "${{ env.WORKSPACE_NAME }}",
"auto-destroy-activity-duration": "14d",
"execution-mode": "remote",
"source-name": "scidsg/hushline dev deploy"
},
"relationships": {
"project": {
"data": {
"id": "${{ env.TF_PROJECT_HUSH_LINE_DEV }}"
}
}
}
}'
}
}')
workspace_id=$(echo $resp | jq -r '.data.id')
echo "Created workspace '${{ env.WORKSPACE_NAME }}' with id '$workspace_id'"
curl -X GET https://app.terraform.io/api/v2/workspaces/${workspace_id}/relationships/tags \
-H 'Content-Type: application/vnd.api+json' \
-H 'Authorization: Bearer ${{ secrets.HUSHLINE_DEV_TF_TOKEN }}'
echo "Tagging workspace with the 'dev' tag... https://app.terraform.io/api/v2/workspaces/${workspace_id}/relationships/tags"
curl -X POST https://app.terraform.io/api/v2/workspaces/${workspace_id}/relationships/tags \
-H 'Content-Type: application/vnd.api+json' \
-H 'Authorization: Bearer ${{ secrets.HUSHLINE_DEV_TF_TOKEN }}' \
-d '{
"data": [
{
"type": "tags",
"attributes": {
"name": "dev"
}
}
]
}'
fi
- name: Plan test infrastrucutre
uses: dflook/terraform-plan@v1.43.0
Expand Down

0 comments on commit 9544188

Please sign in to comment.