Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix import script github action #136

Merged
merged 10 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 88 additions & 25 deletions .github/workflows/build-release-import-script.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
push:
tags:
- 'import/v*'
pull_request:
branches:
- main

permissions:
contents: write
Expand All @@ -13,19 +16,32 @@ jobs:
name: Build
runs-on: ubuntu-latest
timeout-minutes: 5
strategy:
matrix:
goos: [ linux, windows, darwin ]
goarch: [ amd64, arm64 ]
Comment on lines +16 to +19
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generate binary for each combination of os and arch

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
- run: go build -v -o terraform-import-script ./import/import_script.go
- name: Upload Terraform import script artifact
- 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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also add the version to the binary names

fi
go build -o "${output_name}" ./import/import_script.go
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: terraform-import-script
path: terraform-import-script
name: binaries
path: terraform-import-script_*

lint:
name: Lint
Expand All @@ -40,30 +56,77 @@ jobs:
- run: go vet ./import/import_script.go
- run: make validate-fmt

goreleaser:
needs: [build, lint]
release:
name: Create Release (or Dry Run)
needs: build
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
- name: Import GPG key
uses: crazy-max/ghaction-import-gpg@v5
id: import_gpg
with:
gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
passphrase: ${{ secrets.PASSPHRASE }}
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@5742e2a039330cbb23ebf35f046f814d4c6ff811 # v5.1.0
- name: Checkout code
uses: actions/checkout@v3

- name: Download artifacts
uses: actions/download-artifact@v3
with:
args: release --clean --config .goreleaser.import.yml
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 }}
GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }}
- name: Upload GoReleaser artifacts
uses: actions/upload-artifact@v3
with:
name: goreleaser-artifacts
path: dist/*
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)
});
}
}
16 changes: 13 additions & 3 deletions .goreleaser.import.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# .goreleaser.import.yml
version: 2

project_name: terraform-import-script

builds:
Expand All @@ -18,7 +21,7 @@ builds:
archives:
- format: tar.gz
name_template: >-
{{ .ProjectName }}_{{ .Version }}_
{{ .ProjectName }}_{{ .Tag }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
Expand All @@ -28,7 +31,7 @@ archives:
format: zip

checksum:
name_template: '{{ .ProjectName }}_{{ .Version }}_SHA256SUMS'
name_template: '{{ .ProjectName }}_{{ .Tag }}_SHA256SUMS'
algorithm: sha256

signs:
Expand All @@ -55,4 +58,11 @@ release:
# Terraform Import Script Release {{.Tag}}

This release contains the Terraform import script, a standalone tool for importing resources into Terraform.
prerelease: auto
prerelease: auto

git:
prerelease_suffix: "-rc"

# This will ensure the full tag (including 'import/') is used
tag_names:
use_fully_qualified: true
Loading