is there way to get a current branch name using this plugin?
daroga0002 opened this issue · comments
Does there is a way to get current branch name?
This is because I want have such example code in packer:
locals {
......
### data source gathering branch name
version = local.branch == "master" ? "${var.version}" : "${var.version}-${local.sha}"
}
source "amazon-ebs" "ami1" {
ami_description = "AMI1"
ami_name = "ami1-${local.version}"
}
This will allow use CI/CD pipeline approach:
- build artifacts on each branch with name including git sha
- on master branch dont use SHA in version name
No, there isn't currently a datasource which returns the branch. However, it should be pretty easy to build. I might add it myself if I have some free time, but PRs are also welcome :)
I am not a master of go
but I have created PR to address it. Please take a look when you will have a time
Thank you so much for the PR! As you could probably see from the code, I am no master of Go either. I will try to review it this weekend.
Ca we have an example on how to use this? It would add great detail to the documentation!
it is documented here:
https://github.com/ethanmdavidson/packer-plugin-git/blob/main/docs/datasources/commit.mdx
In my project I am using this as:
variable "ubuntu_version" {
description = "Variable with version of AMI"
type = string
}
locals {
truncated_sha = substr(data.git-commit.cwd-head.hash, 0, 8)
version = data.git-repository.cwd.head == "master" && data.git-repository.cwd.is_clean ? var.ubuntu_version : "${var.ubuntu_version}-${local.truncated_sha}"
}
data "git-repository" "cwd" {}
data "git-commit" "cwd-head" {}
so on the end local.version
is variable which I am using:
source "amazon-ebs" "ubuntu" {
ami_name = "ubuntu-${local.version}"
.......................
Thanks for the example @daroga0002 ! I did try to use this as an example in my packer template, but it throws me an error saying the AMI name is invalid when i run packer validate .
command:
Error: 1 error(s) occurred:
* AMIName should only contain alphanumeric characters, parentheses (()), square brackets ([]), spaces ( ), periods (.), slashes (/), dashes (-), single quotes ('), at-signs (@), or underscores(_). You can use the `clean_resource_name` template filter to automatically clean your ami name.
on ami.pkr.hcl line 48:
(source code not available)
Here's a snippet of my ami.pkr.hcl
file where it fails:
.....
variable "ubuntu_version" {
type = string
description = "Version of the custom AMI"
}
locals {
truncated_sha = substr(data.git-commit.cwd-head.hash, 0, 8)
version = data.git-repository.cwd.head == "master" && data.git-repository.cwd.is_clean ? var.ubuntu_version : "${var.ubuntu_version}-${local.truncated_sha}"
}
data "git-repository" "cwd" {}
data "git-commit" "cwd-head" {}
source "amazon-ebs" "ubuntu" {
region = "${var.aws_region}"
ami_name = "ubuntu-${local.version}"
.....
Can't seem to find what I'm doing wrong here. I also didn't find any specific rules as to what the AMI name should follow, so it's a bit vague to me what not to name my AMI, because I wanted to have it specific to the PR during CI/CD workflows on GitHub actions.
this is packer bug, if you run packer build it should go without issues.
In general looks that validate doesnt trying to predict a values for variables.
@sydrawat01 also you can do just packer validate -syntax-only .
which will be checking does code is correctly written (all HCL references and etc.)