Skip to content

Commit 56bef22

Browse files
author
Rui Yang
authored
Merge pull request #136 from evanchaoli/get_latest
Add get_latest param.
2 parents d498338 + f596b78 commit 56bef22

File tree

6 files changed

+117
-30
lines changed

6 files changed

+117
-30
lines changed

README.md

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ plan:
154154

155155
### `check`: Report the current version number.
156156

157-
Detects new versions by reading the file from the specified source. If the file is empty, it returns the `initial_version`. If the file is not empty, it returns the version specified in the file.
157+
Detects new versions by reading the file from the specified source. If the
158+
file is empty, it returns the `initial_version`. If the file is not empty, it
159+
returns the version specified in the file.
158160

159161
### `in`: Provide the version as a file, optionally bumping it.
160162

@@ -205,6 +207,7 @@ because there's some new version `M`, the driver will re-apply the bump to get
205207
* `pre_without_version`: *Optional.* By default `false`, once it's set to `true`
206208
then PreRelease will be bumped without a version number.
207209

210+
* `get_latest`: *Optional.* See [Check-less Usage](#check-less-usage).
208211

209212
## Version Bumping Semantics
210213

@@ -229,7 +232,10 @@ be one of:
229232
version is reset to `1`. If the version is *not* already a pre-release, then
230233
`pre` is added, starting at `1`.
231234

232-
The value of `pre` can be anything you like; the value will be `pre`-pended (_hah_) to a numeric value. For example, `pre: foo` will result in a semver of `x.y.z-foo.<number>`, `pre: alpha` becomes `x.y.z-alpha.<number>`, and `pre: my-preferred-naming-convention` becomes `x.y.z-my-preferred-naming-convention.<number>`
235+
The value of `pre` can be anything you like; the value will be `pre`-pended (_hah_)
236+
to a numeric value. For example, `pre: foo` will result in a semver of
237+
`x.y.z-foo.<number>`, `pre: alpha` becomes `x.y.z-alpha.<number>`, and
238+
`pre: my-preferred-naming-convention` becomes `x.y.z-my-preferred-naming-convention.<number>`
233239

234240
* `build`: *Optional.* Same as `pre` but for build labels (e.g. `build: foo`
235241
will result in a semver of `x.y.z+foo.<number>`, `build: alpha` becomes
@@ -246,7 +252,76 @@ be one of:
246252
* `build_without_version`: *Optional.* Same as `pre_without_version` but for
247253
build labels.
248254

249-
### Running the tests
255+
## Check-less Usage
256+
257+
A classic usage of semver resource is like:
258+
259+
```yaml
260+
resources:
261+
- name: version
262+
type: semver
263+
source:
264+
driver: git
265+
uri: git@github.com:concourse/concourse.git
266+
branch: version
267+
file: version
268+
private_key: {{concourse-repo-private-key}}
269+
270+
jobs:
271+
- name: some-job
272+
plan:
273+
- get: trigger-resource
274+
trigger: true
275+
- get: version
276+
param: {bump: major}
277+
- task: a-thing-that-needs-a-version
278+
- put: version
279+
params: {file: version/version}
280+
```
281+
282+
In above classic mode, Concourse will run periodic checks against the `semver`
283+
resource `version`. Each check will do a `git clone` as the driver is `git`.
284+
When there are a lot of `semver` resources, checks on `semver` resources may
285+
also bring burden to the git system as each check will invoke a `git clone`.
286+
287+
Given each `semver` resource requires a parameter `file` in `source`, `semver`
288+
resources are hard to enjoy [benefits of global resources](https://concourse-ci.org/global-resources.html#benefits-of-global-resources).
289+
290+
To mitigate the burden of checks, if a `semver` resource is not a job trigger,
291+
check-less mode can be used. The above sample then can be rewritten as:
292+
293+
```yaml
294+
jobs:
295+
- name: some-job
296+
plan:
297+
- get: trigger-resource
298+
trigger: true
299+
- put: version # change `get` to `put`
300+
param:
301+
get_latest: true # and set `get_latest: true`
302+
get_params:
303+
bump: major
304+
- task: a-thing-that-needs-a-version
305+
- put: version
306+
params: {file: version/version}
307+
```
308+
309+
You may have noticed that, original `get: version` is changed to `put: version`.
310+
Now resource `version` is put-only, then Concourse will no longer run check on
311+
it. Param `get_latest: true` tells the `put` step to only fetch the latest version
312+
without bumping anything. Then the implied `get` will fetch a version as a typical
313+
`get` step.
314+
315+
If your Concourse or Git (e.g. Gitlab) systems are exhausted by `semver` resources'
316+
checks, you may consider reforming pipelines to use this check-less usage.
317+
318+
The cons of check-less usage are:
319+
320+
* you cannot use `put` step as a job trigger.
321+
* `put` step with `get_latest: true` will always fetch the latest version, thus
322+
you are not able to pin an old version.
323+
324+
## Running the tests
250325

251326
The tests have been embedded with the `Dockerfile`; ensuring that the testing
252327
environment is consistent across any `docker` enabled platform. When the docker
@@ -260,7 +335,7 @@ docker build -t semver-resource --target tests -f dockerfiles/alpine/Dockerfile
260335
docker build -t semver-resource --target tests -f dockerfiles/ubuntu/Dockerfile .
261336
```
262337

263-
#### Integration tests
338+
### Integration tests
264339

265340
The integration requires two AWS S3 buckets, one without versioning and another
266341
with. The `docker build` step requires setting `--build-args` so the
@@ -287,7 +362,7 @@ docker build . -t semver-resource --target tests -f dockerfiles/ubuntu/Dockerfil
287362
--build-arg SEMVER_TESTING_REGION="some-region"
288363
```
289364

290-
### Contributing
365+
## Contributing
291366

292367
Please make all pull requests to the `master` branch and ensure tests pass
293368
locally.

dockerfiles/alpine/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ FROM ${builder_image} as builder
55
COPY . /src
66
WORKDIR /src
77
ENV CGO_ENABLED 0
8+
9+
ARG goproxy
10+
ENV GOPROXY=$goproxy
11+
812
RUN go get -d ./...
913
RUN go build -o /assets/in ./in
1014
RUN go build -o /assets/out ./out

dockerfiles/ubuntu/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ FROM ${builder_image} as builder
55
COPY . /src
66
WORKDIR /src
77
ENV CGO_ENABLED 0
8+
9+
ARG goproxy
10+
ENV GOPROXY=$goproxy
11+
812
RUN go get -d ./...
913
RUN go build -o /assets/in ./in
1014
RUN go build -o /assets/out ./out

driver/git.go

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package driver
33
import (
44
"errors"
55
"fmt"
6-
"io/ioutil"
76
"net/mail"
87
"os"
98
"os/exec"
@@ -72,10 +71,11 @@ func (driver *GitDriver) Bump(bump version.Bump) (semver.Version, error) {
7271

7372
newVersion = bump.Apply(currentVersion)
7473

75-
wrote, err := driver.writeVersion(newVersion)
74+
var wrote bool
75+
wrote, err = driver.writeVersion(newVersion)
7676
if wrote {
7777
break
78-
}
78+
}
7979
}
8080
if err != nil {
8181
return semver.Version{}, err
@@ -101,15 +101,15 @@ func (driver *GitDriver) Set(newVersion semver.Version) error {
101101
return err
102102
}
103103

104-
wrote, err := driver.writeVersion(newVersion)
105-
if err != nil {
106-
return err
107-
}
108-
104+
var wrote bool
105+
wrote, err = driver.writeVersion(newVersion)
109106
if wrote {
110107
break
111108
}
112109
}
110+
if err != nil {
111+
return err
112+
}
113113

114114
return nil
115115
}
@@ -224,7 +224,7 @@ func (driver *GitDriver) setUpKey() error {
224224
if err != nil {
225225
if os.IsNotExist(err) {
226226
privateKey := strings.TrimSuffix(driver.PrivateKey, "\n")
227-
err := ioutil.WriteFile(privateKeyPath, []byte(privateKey+"\n"), 0600)
227+
err := os.WriteFile(privateKeyPath, []byte(privateKey+"\n"), 0600)
228228
if err != nil {
229229
return err
230230
}
@@ -253,7 +253,7 @@ func (driver *GitDriver) setUpUsernamePassword() error {
253253
if err != nil {
254254
if os.IsNotExist(err) {
255255
content := fmt.Sprintf("default login %s password %s", driver.Username, driver.Password)
256-
err := ioutil.WriteFile(netRcPath, []byte(content), 0600)
256+
err := os.WriteFile(netRcPath, []byte(content), 0600)
257257
if err != nil {
258258
return err
259259
}
@@ -321,20 +321,18 @@ func (driver *GitDriver) readVersion() (semver.Version, bool, error) {
321321

322322
const nothingToCommitString = "nothing to commit"
323323
const falsePushString = "Everything up-to-date"
324-
const pushRejectedString = "[rejected]"
325-
const pushRemoteRejectedString = "[remote rejected]"
326324

327325
func (driver *GitDriver) writeVersion(newVersion semver.Version) (bool, error) {
328326

329-
path := filepath.Dir(driver.File)
330-
if path != "/" && path != "." {
331-
err := os.MkdirAll(filepath.Join(gitRepoDir, path), 0755)
332-
if err != nil {
333-
return false, err
334-
}
335-
}
327+
path := filepath.Dir(driver.File)
328+
if path != "/" && path != "." {
329+
err := os.MkdirAll(filepath.Join(gitRepoDir, path), 0755)
330+
if err != nil {
331+
return false, err
332+
}
333+
}
336334

337-
err := ioutil.WriteFile(filepath.Join(gitRepoDir, driver.File), []byte(newVersion.String()+"\n"), 0644)
335+
err := os.WriteFile(filepath.Join(gitRepoDir, driver.File), []byte(newVersion.String()+"\n"), 0644)
338336
if err != nil {
339337
return false, err
340338
}
@@ -374,11 +372,9 @@ func (driver *GitDriver) writeVersion(newVersion semver.Version) (bool, error) {
374372

375373
pushOutput, err := gitPush.CombinedOutput()
376374

377-
if strings.Contains(string(pushOutput), falsePushString) ||
378-
strings.Contains(string(pushOutput), pushRejectedString) ||
379-
strings.Contains(string(pushOutput), pushRemoteRejectedString) {
375+
if strings.Contains(string(pushOutput), falsePushString) {
380376
os.Stderr.Write(pushOutput)
381-
return false, nil
377+
return true, nil
382378
}
383379

384380
if err != nil {

models/models.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ type OutParams struct {
4242
Build string `json:"build"`
4343
PreWithoutVersion bool `json:"pre_without_version"`
4444
BuildWithoutVersion bool `json:"build_without_version"`
45+
46+
GetLatest bool `json:"get_latest,omitempty"`
4547
}
4648

4749
type CheckRequest struct {

out/main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ func main() {
3333
}
3434

3535
var newVersion semver.Version
36-
if request.Params.File != "" {
36+
if request.Params.GetLatest {
37+
versions, err := driver.Check(nil)
38+
if err != nil {
39+
fatal("checking latest version", err)
40+
}
41+
newVersion = versions[0]
42+
} else if request.Params.File != "" {
3743
versionFile, err := os.Open(filepath.Join(sources, request.Params.File))
3844
if err != nil {
3945
fatal("opening version file", err)

0 commit comments

Comments
 (0)