Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit f65886f

Browse files
committed
Add terraform-provider-infracost to the Dockerfile, README and GitHub action
1 parent 605ed95 commit f65886f

File tree

5 files changed

+100
-7
lines changed

5 files changed

+100
-7
lines changed

.github/workflows/go.yml

+11
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ jobs:
3030
dep ensure
3131
fi
3232
33+
- name: Check out terraform-provider-infracost@master
34+
uses: actions/checkout@v2
35+
with:
36+
repository: infracost/terraform-provider-infracost
37+
ref: refs/heads/master
38+
path: terraform-provider-infracost
39+
40+
- name: Install terraform-provider-infracost
41+
run: sudo make install
42+
working-directory: terraform-provider-infracost
43+
3344
- name: Run golangci-lint
3445
uses: golangci/[email protected]
3546
with:

Dockerfile

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@ FROM golang:1.15 as builder
22

33
ARG ARCH=linux
44
ARG TERRAFORM_VERSION=0.12.25
5+
ARG TERRAFORM_PROVIDER_INFRACOST_VERSION=latest
56

67
# Set Environment Variables
78
SHELL ["/bin/bash", "-c"]
89
ENV HOME /app
910
ENV CGO_ENABLED 0
10-
ENV GOOS linux
1111

1212
# Install Packages
1313
RUN wget -q https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_${ARCH}_amd64.zip
14-
RUN apt-get update && apt-get -y install zip -y
14+
RUN apt-get update && apt-get -y install zip jq -y
1515
RUN unzip terraform*.zip && \
1616
mv terraform /usr/local/bin && \
1717
chmod +x /usr/local/bin/terraform
1818

19-
# Build Application
2019
WORKDIR /app
20+
COPY scripts/install_provider.sh scripts/install_provider.sh
21+
RUN scripts/install_provider.sh ${TERRAFORM_PROVIDER_INFRACOST_VERSION} /usr/local/bin/
22+
23+
# Build Application
2124
COPY . .
2225
RUN make deps
2326
RUN make build
@@ -27,8 +30,8 @@ FROM alpine:3.12 as app
2730
ARG TERRAFORM_VERSION=0.12.25
2831
RUN apk --no-cache add ca-certificates terraform=${TERRAFORM_VERSION}-r0
2932
WORKDIR /root/
33+
COPY --from=builder /usr/local/bin/terraform-provider-infracost* /usr/bin/
3034
COPY --from=builder /app/build/infracost /usr/local/bin/infracost
3135
RUN chmod +x /usr/local/bin/infracost
32-
3336
ENTRYPOINT [ "infracost" ]
3437
CMD [ "--help" ]

Makefile

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
BINARY := infracost
22
ENTRYPOINT := cmd/infracost/main.go
3+
TERRAFORM_PROVIDER_INFRACOST_VERSION := latest
34

4-
.PHONY: deps run build windows linux darwin build_all clean test fmt lint
5+
ifndef $(GOOS)
6+
GOOS=$(shell go env GOOS)
7+
endif
8+
9+
ifndef $(GOARCH)
10+
GOARCH=$(shell go env GOARCH)
11+
endif
12+
13+
.PHONY: deps run build windows linux darwin build_all release install_provider clean test fmt lint
514

615
deps:
716
go mod download
@@ -28,10 +37,12 @@ release: build_all
2837
cd build; tar -czf $(BINARY)-linux-amd64.tar.gz $(BINARY)-linux-amd64
2938
cd build; tar -czf $(BINARY)-darwin-amd64.tar.gz $(BINARY)-darwin-amd64
3039

40+
install_provider:
41+
scripts/install_provider.sh $(TERRAFORM_PROVIDER_INFRACOST_VERSION)
42+
3143
clean:
3244
go clean
3345
rm -rf build/$(BINARY)*
34-
rm -rf release/$(BINARY)*
3546

3647
test:
3748
go test ./... $(or $(ARGS), -v -cover)

README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,16 @@ The [Infracost GitHub action](https://github.com/marketplace/actions/run-infraco
4242

4343
## Development
4444

45-
Install dependencies:
45+
Install Go dependencies:
4646
```sh
4747
make deps
4848
```
4949

50+
Install latest version of terraform-provider-infracost. If you want to use a local development version see [#using-a-local-version-of-terraform-provider-infracost](#using-a-local-version-of-terraform-provider-infracost)
51+
```sh
52+
make install_provider
53+
```
54+
5055
Run the code:
5156
```sh
5257
make run ARGS="--tfdir <Terraform Dir>"
@@ -67,6 +72,17 @@ Build:
6772
make build
6873
```
6974

75+
### Using a local version of terraform-provider-infracost
76+
77+
To use a local development version of terraform-provider-infracost
78+
79+
1. Fork/clone the [terraform-provider-infracost repository](https://github.com/infracost/terraform-provider-infracost)
80+
81+
2. Inside the directory that you cloned the repository run the following to install the local version in your `~/.terraform.d/plugins` directory:
82+
```sh
83+
make install
84+
```
85+
7086
## Contributing
7187

7288
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

scripts/install_provider.sh

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env bash
2+
3+
if ! command -v jq &> /dev/null
4+
then
5+
echo "This script requires jq"
6+
exit
7+
fi
8+
9+
version=${1:-latest}
10+
if [ ! -z ${2+x} ]; then
11+
install_path=${2}
12+
fi
13+
14+
if [ ! -z $GITHUB_ACCESS_TOKEN} ]; then
15+
github_headers="-H 'Authorization: token ${GITHUB_ACCESS_TOKEN}'"
16+
fi
17+
18+
goos=$(go env GOOS)
19+
goarch=$(go env GOARCH)
20+
21+
# Download the release assets from GitHub to a tmp directory
22+
tmp_dir=$(mktemp)
23+
if [ "$version" == "latest" ]; then
24+
resp=$(curl "${github_headers}" https://api.github.com/repos/infracost/terraform-provider-infracost/releases/latest)
25+
version=$(echo ${resp} | jq -r '.name')
26+
echo ${resp} | jq -r '.assets[] | select (.name | contains("'${goos}'_'${goarch}'")) | .browser_download_url' | wget -P ${tmp_dir} -i -
27+
else
28+
curl "${github_headers}" https://api.github.com/repos/infracost/terraform-provider-infracost/releases \
29+
| jq -r '.[] | select (.name == "'${version}'") | .assets[] | select (.name | contains("${goos}_${goarch}")) | .browser_download_url' | wget -P ${tmp_dir} -i -
30+
fi
31+
32+
# Unzip the release assets
33+
zip=$(ls ${tmp_dir} | grep "terraform-provider-infracost.*\.zip")
34+
unzip -od ${tmp_dir} ${tmp_dir}/${zip} && rm ${tmp_dir}/${zip}
35+
binary=$(ls ${tmp_dir} | grep "terraform-provider-infracost")
36+
37+
# If the install path isn't set then use the default Terraform plugin install paths
38+
if [ -z ${install_path+x} ]; then
39+
plugin_root_path=${HOME}/.terraform.d/plugins
40+
install_path=${plugin_root_path}/infracost.io/infracost/infracost/${version}/${goos}_${goarch}
41+
42+
mkdir -p ${install_path}
43+
mv ${tmp_dir}/${binary} ${install_path}/${binary}
44+
45+
# Add a link from the old-style plugin paths to maintain compatibility with older versions of Terraform
46+
mkdir -p ${plugin_root_path}/${goos}_${goarch}
47+
ln -sf ${install_path}/${binary} ${plugin_root_path}/${goos}_${goarch}/${binary}
48+
# Otherwise use the user-specified install path
49+
else
50+
mkdir -p ${install_path}
51+
mv ${tmp_dir}/${binary} ${install_path}/${binary}
52+
fi

0 commit comments

Comments
 (0)