I have tried initializing terraform code in Travis CI but an error keeps coming up;
Failed to load state:
Failed to open state file at
gs://{{project_id}}/terraform/state/default.tfstate:
Get
https://storage.googleapis.com/{{project_id}}/terraform/state/default.tfstate:
metadata: GCE metadata "instance/service-accounts/default/token" not defined
I have initialized gcloud(Google Cloud SDK), with a service account ie
gcloud auth activate-service-account --key-file {{service-account-path}}
and set:
export TF_CREDS={{service-account-path}}
but the error still comes up.
My terraform provider and backend are in the following format respectively:
provider "google" {
region = "${var.region}"
zone = "${var.zone}"
project = "${var.project_id}"
}
terraform {
backend "gcs" {
bucket = "{{project_id}}"
prefix = "terraform/state"
}
}
I removed the default Google Cloud SDK thinking that it was causing a conflict when another which is specific to the usage in the CI/CD process is installed. This, however, did not help.
The .travis.yml looks like:
language: node_js
sudo: true
node_js:
- "12.2.0"
- "12.0"
- "11.0"
- "10.0"
- "9.10.0"
services:
- docker
stages:
- name: test
- name: test-build
- name: build
if: |
tag =~ /^v\d+\.\d+(\.\d+)?(-\S*)?$/ || \
tag =~ /^dev\/v\d+\.\d+(\.\d+)?(-\S*)?$/ || \
tag =~ /^release\/v\d+\.\d+(\.\d+)?(-\S*)?$/ || \
tag =~ /^prod\/v\d+\.\d+(\.\d+)?(-\S*)?$/
- name: deploy
if: |
tag =~ /^v\d+\.\d+(\.\d+)?(-\S*)?$/ || \
tag =~ /^dev\/v\d+\.\d+(\.\d+)?(-\S*)?$/ || \
tag =~ /^release\/v\d+\.\d+(\.\d+)?(-\S*)?$/ || \
tag =~ /^prod\/v\d+\.\d+(\.\d+)?(-\S*)?$/
install:
- yarn install
script:
- yarn list
# - yarn check
# - yarn test
jobs:
include:
- stage: test-build
install:
- yarn install
script:
- yarn run build
- stage: build
install:
- docker -v
before_script:
- git clone https://[email protected]/{{github-user}}/devops_configurations.git ../devops_configurations/
- cp ../devops_configurations/React/* .
script:
- docker login -u $DOCKER_HUB_USER -p $DOCKER_HUB_PASSWORD
- docker build -t $DOCKER_HUB_USER/test_react:$(echo $TRAVIS_TAG | tr "/" .) -f Dockerfile-multibuild .
- docker push $DOCKER_HUB_USER/test_react:$(echo $TRAVIS_TAG | tr "/" .)
- docker rmi $DOCKER_HUB_USER/test_react:$(echo $TRAVIS_TAG | tr "/" .)
- docker logout
- stage: deploy
install:
# Install Google Cloud SDK
- if [ ! -d "$HOME/google-cloud-sdk/bin" ]; then
rm -rf $HOME/google-cloud-sdk;
sudo rm -rf /usr/lib/google-cloud-sdk/;
export CLOUDSDK_CORE_DISABLE_PROMPTS=1;
curl https://sdk.cloud.google.com | bash > /dev/null;
fi
# Add gcloud to $PATH
- source /home/travis/google-cloud-sdk/path.bash.inc
# Install Kubectl
- if ! [ -x "$(command -v kubectl)" ]; then
gcloud components install kubectl --quiet;
fi
# Install Terraform
- if ! [ -x "$(command -v terraform)" ]; then
TERRAFORM_VERSION=0.12.0;
URL="https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip";
wget $URL;
unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip -d executables/;
sudo mv executables/terraform /usr/local/bin/terraform;
sudo chmod +x /usr/local/bin/terraform;
rm terraform_${TERRAFORM_VERSION}_linux_amd64.zip;
rm -rf executables/;
fi
before_script:
- git clone https://[email protected]/{{github-user}}/devops_configurations.git ../devops_configurations/
- cd ../devops_configurations/terraform/
- mkdir {{service-account-folder}}
- echo $GCP_MAIN_SERVICE_ACCOUNT | base64 --decode > {{service-account-path}}
- gcloud auth activate-service-account --key-file {{service-account-path}}
- export TF_CREDS={{service-account-path}}
script:
- terraform init
- terraform plan -var-file variables.tfvars
-var="react_docker_image=$DOCKER_HUB_USER/test_react:$(echo $TRAVIS_TAG | tr "/" .)"
-var="vue_docker_image=$(terraform output terraform_gcp_gke_project_id || echo '$DOCKER_HUB_USER/test_vue:dev.v0.0.1-test')"
-var="angular_docker_image=$(terraform output terraform_gcp_gke_project_id || echo '$DOCKER_HUB_USER/test_angular:dev.v0.0.1-test')"
-target kubenetes_deployment.react-deployment
- terraform apply -var-file variables.tfvars
-var="react_docker_image=$DOCKER_HUB_USER/test_react:$(echo $TRAVIS_TAG | tr "/" .)"
-var="vue_docker_image=$(terraform output terraform_gcp_gke_project_id || echo '$DOCKER_HUB_USER/test_vue:dev.v0.0.1-test')"
-var="angular_docker_image=$(terraform output terraform_gcp_gke_project_id || echo '$DOCKER_HUB_USER/test_angular:dev.v0.0.1-test')"
-target kubenetes_deployment.react-deployment
after_script:
- rm -rf {{service-account-folder}}
- unset TF_CREDS
Any help would be appreciated.