Thank you very much for clarification details.
When terraform runs plan or apply, it tries to find a difference between 3 sources of information - "source code", "state data", and "real life resources". But, the "source code" - is terraform code, not the python, go or node.js, etc. code.
In your example, a "movable alias (branch)" is used in the terraform source code in the source_repository
- url
attribute. When you modify the cloud function source code, the value of this url
attribute is not changed. Thus, from the terraform's point of view the cloud function has not been changed, therefore nothing to be redeployed.
There may be a few ways to solve the issue. One of them is described below.
Instead of a "movable alias (branch)", use a specific "git commit hash". In this case, the value of the url
attribute is changed, so terraform sees that the cloud function is to be redeployed.
Therefore, the url
attribute should look:
url = https://source.developers.google.com/projects/MY_PROJECT_NAME/repos/MY_REPOSITORY_NAME/revisions/GIT_COMMIT_HASH/paths/RELATIVE_PATH
The RELATIVE_PATH
may be omitted if you deploy from the "local root" (the cloud function code is not in any subdirectories).
Now, it is necessary to find out the actual (or the most recent) GIT_COMMIT_HASH
. As you deploy from your local machine, you probably can either find the commit hash on your local machine and use it, or find the commit hash from the remote git repository.
The later case is more complex (I guess), but safer. The former case is more simple, but if you forget to git push
, the terraform plan/apply
will be finished with an error (as the 'commit hash' won't be found in the remote repository from which you deploy the cloud function code). Let's consider the simpler (local) case.
How to find the most recent git commit hash => use a command like: git rev-list -1 HEAD ./
or git rev-list -1 HEAD ./some_path
So that command is to be run (during plan/apply) and the result to be propagated into a terraform file, so that the difference can be revealed by terraform.
In order to implement that, we can use an "external" terraform provider (I guess the latest version is 2.0.0 at the present moment):
provider "external" {
version = "~> 1.2"
}
data "external" "git_commit_hash" {
program = ["sh", "get-commit-hash.sh"]
}
and we need a bash script with the name "get-commit-hash.sh" (in the same directory, where the terraform is running):
#!/bin/sh
{
hash_value=`git rev-list -1 HEAD ./`
} &> /dev/null
echo {\"hash\":\""$hash_value"\"}
Bear in mind to apply correct chmod
, so it is an executable file.
The last step - to configure the url
attribute in the terraform cloud function, and it should be something like this one:
...revisions/${data.external.git_commit_hash.result.hash}/paths...