-
Notifications
You must be signed in to change notification settings - Fork 2
/
.gitlab-ci.yml
168 lines (160 loc) · 5.12 KB
/
.gitlab-ci.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
variables:
DOCKER_HUB_PROXY: "artifactory.f5net.com/dockerhub-remote"
PKG_NAME: terraform-provider-f5os
default:
image: "${DOCKER_HUB_PROXY}/golang:latest"
stages:
- lint
- build
- unittest
- release
- publish
workflow:
rules:
- if: $CI_MERGE_REQUEST_IID
changes:
- .gitlab/ci/Dockerfile
- .go-version
variables:
IMAGE_TAG: ${CI_COMMIT_REF_SLUG}
- if: $CI_MERGE_REQUEST_IID
- if: $CI_COMMIT_TAG
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
cache:
key:
files:
- GNUMakefile
makelint:
needs: []
stage: lint
script:
- go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.51.2
- golangci-lint --version
- export GOFLAGS=-mod=vendor
- make ${TARGET}
parallel:
matrix:
- TARGET:
- lint
go:build:
rules:
- if: $CI_MERGE_REQUEST_IID
changes:
- "**/*.go"
- go.mod
- go.sum
- .gitlab-ci.yml
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
- if: $CI_COMMIT_TAG
needs: []
stage: build
script:
- export GOFLAGS=-mod=vendor
- make build
unit-tests:
rules:
- if: $CI_MERGE_REQUEST_IID
changes:
- "**/*.go"
- go.mod
- go.sum
- .gitlab-ci.yml
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
- if: $CI_COMMIT_TAG
stage: unittest
script:
- make test
coverage: '/coverage: \d+\.\d+/'
goreleaser:
stage: release
dependencies: []
only:
- tags
image:
name: "${DOCKER_HUB_PROXY}/goreleaser/goreleaser"
variables:
# Disable shallow cloning so that goreleaser can diff between tags to
# generate a changelog.
GIT_DEPTH: 0
GITLAB_TOKEN: $CI_JOB_TOKEN
before_script:
- apk add gpg-agent
- chmod +x setup_deps.bash
- ./setup_deps.bash
- gpg --batch --no-tty --import $GPG_PRIVATE_KEY
- "export GPG_FINGERPRINT=$(gpg --with-colons --list-keys | awk -F: '/^pub/ { print $5 }')"
script:
- echo $GPG_FINGERPRINT
- goreleaser release
- cp terraform-registry-manifest.json dist/${CI_PROJECT_NAME}_${CI_COMMIT_TAG:1}_manifest.json
artifacts:
paths:
- dist/$CI_PROJECT_NAME_*.zip
- dist/$CI_PROJECT_NAME_*_SHA256SUMS*
- dist/$CI_PROJECT_NAME_*_manifest.json
- docs/
github-for-terraform-registry:
stage: publish
only:
- tags
image: alpine:3.16
variables:
GIT_STRATEGY: none
GITHUB_ORG: RavinderReddyF5
GITHUB_REPO: $CI_PROJECT_NAME
GITHUB_USER: RavinderReddyF5
before_script:
- apk add --update curl jq file git
- ls dist/
script:
- |
git clone --depth 1 https://$GITHUB_USER:$GITHUB_TOKEN_FOR_SYNC@github.com/$GITHUB_ORG/$GITHUB_REPO.git /tmp/github-repo
cp -rf docs /tmp/github-repo
cd /tmp/github-repo
git config user.email $GITHUB_MAIL
git config user.name $GITHUB_USER
git add docs
git commit --message "Update docs for $CI_COMMIT_TAG release" --allow-empty
git push
cd -
rm -rf /tmp/github-repo
# create a pre-release Release on GitHub
# see https://docs.github.com/en/rest/releases/releases#create-a-release
- |
gh_release=$(curl --silent --show-error --fail-with-body -X POST -H "Accept: application/vnd.github.v3+json" -H "Authorization: token $GITHUB_TOKEN_FOR_SYNC" \
https://api.github.com/repos/$GITHUB_ORG/$GITHUB_REPO/releases \
-d '{
"tag_name":"'$CI_COMMIT_TAG'",
"target_commitish":"main",
"name":"'$CI_COMMIT_TAG'",
"body":"This release hosted on GitHub only exists because it is the only way to publish to the Terraform Provider Registry.\n\nSee the official release page on GitLab here: **'$CI_PROJECT_URL'/-/releases/'$CI_COMMIT_TAG'**",
"draft":false,
"prerelease":true,
"generate_release_notes":false
}'
)
- gh_release_id=$(echo $gh_release | jq -r '.id')
- gh_release_upload_url="https://uploads.github.com/repos/$GITHUB_ORG/$GITHUB_REPO/releases/$gh_release_id/assets"
- 'echo "GitHub Release URL: $gh_release_upload_url"'
# uploading release assets for GitHub release created in previous step
# see https://docs.github.com/en/rest/releases/assets#upload-a-release-asset
- |
echo "Uploading assets from dist/ produced by goreleaser"
for asset in dist/*; do
echo -n "Uploading $asset ..."
content_type=$(file -b --mime-type "$asset")
filename=$(basename "$asset")
curl --silent --output /dev/null --show-error --fail-with-body -X POST -H "Accept: application/vnd.github.v3+json" -H "Authorization: token $GITHUB_TOKEN_FOR_SYNC" \
"$gh_release_upload_url?name=$filename" \
-H "Content-Type: $content_type" \
--data-binary "@$asset"
echo " done."
done
# now that all assets are uploaded we can "release" the "pre-release" created earlier.
- |
curl --silent --show-error --fail-with-body \
-X PATCH \
-H "Accept: application/vnd.github+json" \
-H "Authorization: token $GITHUB_TOKEN_FOR_SYNC" \
https://api.github.com/repos/$GITHUB_ORG/$GITHUB_REPO/releases/$gh_release_id \
-d '{"prerelease":false}'