generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 4
132 lines (121 loc) · 3.97 KB
/
build-release-import-script.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
name: Build and Release Import Script
on:
push:
tags:
- 'import/v*'
pull_request:
branches:
- main
permissions:
contents: write
jobs:
build:
name: Build
runs-on: ubuntu-latest
timeout-minutes: 5
strategy:
matrix:
goos: [ linux, windows, darwin ]
goarch: [ amd64, arm64 ]
steps:
- uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
- uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5.0.1
with:
go-version-file: 'go.mod'
cache: true
- run: go mod download
- name: Build
env:
GOOS: ${{ matrix.goos }}
GOARCH: ${{ matrix.goarch }}
run: |
output_name="terraform-import-script_${{ matrix.goos }}_${{ matrix.goarch }}"
if [ "${{ matrix.goos }}" = "windows" ]; then
output_name="${output_name}.exe"
fi
go build -o "${output_name}" ./import/import_script.go
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: binaries
path: terraform-import-script_*
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
- uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5.0.1
with:
go-version-file: 'go.mod'
cache: true
- run: go fmt ./import/import_script.go
- run: go vet ./import/import_script.go
- run: make validate-fmt
release:
name: Create Release (or Dry Run)
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Download artifacts
uses: actions/download-artifact@v3
with:
name: binaries
- name: Get the version
id: get_version
run: |
if [[ $GITHUB_REF == refs/tags/* ]]; then
echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
else
echo "VERSION=pr-${{ github.event.pull_request.number }}-dry-run" >> $GITHUB_OUTPUT
fi
- name: Dry Run - Create Release
if: github.event_name == 'pull_request'
run: |
echo "Dry Run: Would create release with tag ${{ steps.get_version.outputs.VERSION }}"
echo "Release assets:"
ls -l terraform-import-script_*
- name: Create Release
if: github.event_name != 'pull_request'
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.get_version.outputs.VERSION }}
release_name: Release ${{ steps.get_version.outputs.VERSION }}
draft: false
prerelease: false
- name: Dry Run - Upload Release Assets
if: github.event_name == 'pull_request'
run: |
echo "Dry Run: Would upload the following assets:"
for file in terraform-import-script_*; do
echo " - $file"
done
- name: Upload Release Assets
if: github.event_name != 'pull_request'
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs').promises;
const { repo: { owner, repo }, sha } = context;
const release = await github.rest.repos.getReleaseByTag({
owner,
repo,
tag: '${{ steps.get_version.outputs.VERSION }}'
});
const files = await fs.readdir('.');
for (const file of files) {
if (file.startsWith('terraform-import-script_')) {
await github.rest.repos.uploadReleaseAsset({
owner,
repo,
release_id: release.data.id,
name: file,
data: await fs.readFile(file)
});
}
}