Deploy to space #33
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# To manually trigger: | |
# gh workflow run --ref jadudm/tf-0103 --field environment=dev deploy-to-dev.yaml | |
# | |
name: Deploy to space | |
on: | |
# push: | |
# branches: | |
# - jadudm/tf-0103 | |
workflow_dispatch: | |
inputs: | |
environment: | |
required: true | |
type: string | |
workflow_call: | |
inputs: | |
environment: | |
required: true | |
type: string | |
jobs: | |
deploy: | |
name: apply ( ${{ inputs.environment }} ) | |
runs-on: ubuntu-latest | |
environment: ${{ inputs.environment }} | |
env: | |
KEY: "terraform.tfstate.${{ inputs.environment }}" | |
TF_VAR_cf_user: ${{ secrets.CF_USERNAME }} | |
TF_VAR_cf_password: ${{ secrets.CF_PASSWORD }} | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
AWS_CONFIG_FILE: "${GITHUB_WORKSPACE}/aws-config" | |
AWS_SHARED_CREDENTIALS_FILE: "${GITHUB_WORKSPACE}/aws-credentials" | |
steps: | |
# This leaves the repository checked out in ${GITHUB_WORKSPACE}. | |
# So, ${GITHUB_WORKSPACE}/terraform is where the TF lives. | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: what is there | |
run: | | |
ls -alh "${GITHUB_WORKSPACE}" | |
ls -alh "${GITHUB_WORKSPACE}/terraform" | |
- name: Install CloudFoundry CLI v8 | |
run: | | |
curl -k -O -L https://github.com/cloudfoundry/cli/releases/download/v8.8.0/cf8-cli-installer_8.8.0_x86-64.deb | |
sudo apt-get install --assume-yes ./cf8-cli-installer_8.8.0_x86-64.deb | |
# This may want to become TF actions | |
- name: Install Terraform | |
uses: hashicorp/setup-terraform@v3 | |
with: | |
terraform_version: "1.10.3" | |
# We use ${{ secret.X }} to reference secrets, and | |
# we use ${{ vars.X }} to access environment variables | |
- name: Authenticate against Cloud.gov | |
run: | | |
cf api api.fr.cloud.gov | |
cf auth ${{ secrets.CF_USERNAME }} ${{ secrets.CF_PASSWORD }} | |
cf target -o ${{ vars.CF_ORG }} -s ${{ vars.CF_SPACE }} | |
# https://stackoverflow.com/questions/14268097/do-i-need-cat-to-write-a-heredoc-to-a-file | |
# https://stackoverflow.com/questions/63048738/how-to-declare-variables-for-s3-backend-in-terraform | |
- name: Setup TF creds for state bucket | |
run: | | |
tee "${GITHUB_WORKSPACE}/terraform/${{ inputs.environment }}/backend_${{ inputs.environment }}.config" <<EOF | |
bucket="${{ secrets.TF_VAR_BUCKET_NAME }}" | |
region="${{ secrets.TF_VAR_AWS_DEFAULT_REGION}}" | |
access_key="${{ secrets.TF_VAR_AWS_ACCESS_KEY_ID }}" | |
secret_key="${{ secrets.TF_VAR_SECRET_ACCESS_KEY }}" | |
endpoints={ s3 = "https://s3-${{ secrets.TF_VAR_AWS_DEFAULT_REGION }}.amazonaws.com/" } | |
EOF | |
- name: Setup TF vars for execution | |
run: | | |
tee "${GITHUB_WORKSPACE}/terraform/${{ inputs.environment }}/terraform.tfvars" <<EOF | |
cf_username = "${{ secrets.CF_USERNAME }}" | |
cf_password = "${{ secrets.CF_PASSWORD }}" | |
api_key = "FIXME" | |
cf_space_guid = "${{ secrets.CF_SPACE_GUID }}" | |
cf_org_guid = "${{ secrets.CF_ORG_GUID }}" | |
EOF | |
# - name: Setup AWS | |
# run: | | |
# tee "${GITHUB_WORKSPACE}/aws-credentials" <<EOF | |
# [default] | |
# aws_access_key_id="${{ secrets.TF_VAR_AWS_ACCESS_KEY_ID }}" | |
# aws_secret_access_key="${{ secrets.TF_VAR_SECRET_ACCESS_KEY }}" | |
# EOF | |
# chmod 600 "${GITHUB_WORKSPACE}/aws-credentials" | |
# tee "${GITHUB_WORKSPACE}/aws-config" <<EOF | |
# [default] | |
# region="${{ secrets.TF_VAR_AWS_DEFAULT_REGION }}" | |
# EOF | |
- name: TF init | |
run: | | |
pushd ${GITHUB_WORKSPACE}/terraform/${{ inputs.environment }} | |
terraform init -backend-config="./backend_${{ inputs.environment }}.config" || echo "Failed" && exit | |
popd | |
- name: TF plan | |
uses: dflook/terraform-plan@v1 | |
with: | |
path: ${GITHUB_WORKSPACE}/terraform/${{ inputs.environment }} | |
backend_config_file: "${GITHUB_WORKSPACE}/terraform/${{ inputs.environment }}/backend_${{ inputs.environment}}.config" | |
# - name: TF apply | |
# run: | | |
# pushd ${GITHUB_WORKSPACE}/terraform/${{ inputs.environment }} | |
# make apply | |
# popd |