Skip to content

Commit

Permalink
Build preview images from preview/* branches (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasgarciaisaia committed Aug 7, 2020
1 parent e317973 commit bdf32a7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ci-docker-builder

This repository hosts a script that can be used to build Docker images during the execution on **Travis** or **CircleCI**
and push the result to Docker Hub.
This repository hosts a script that can be used to build Docker images during the execution on **Travis**, **CircleCI**
or **Github Actions** and push the result to Docker Hub.

The **name** of the image and the **repository** are set through environment variables that must be set on each environment.

Expand All @@ -21,6 +21,7 @@ application (for example in the footer).
||`release/x.y`|`x.y-dev-rrrrrrr (build nnn)`|`x.y-dev`||
||`master`|`dev-rrrrrrr (build nnn)`|`dev`||
||`master` (called with **latest** flag)|`rrrrrrr (build nnn)`|`latest`||
||`preview/some-description`|`some-description-rrrrrrr (build nnn)`|`some-description`||

## Usage

Expand Down Expand Up @@ -89,6 +90,29 @@ workflows:
only: /.*/
```

### Github Actions

Add a workflow like this in a `.github/workflows/build.yml` file to let the script decide when to build or not:

```yaml
name: Build & Push Docker Image

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
# needs: test # you can declare a `test` job and uncomment this to test the app before building
env:
DOCKER_REPOSITORY: 'dockerhub_org/dockerhub_repo'
DOCKER_USER: ${{ secrets.DOCKER_USER }}
DOCKER_PASS: ${{ secrets.DOCKER_PASS }}
steps:
- uses: actions/checkout@v2
- name: Build image & push to Docker Hub
run: ./build.sh
```

## Functions

### `dockerSetup [latest]`
Expand Down
5 changes: 5 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ dockerSetup() {
VERSION="${BRANCH##release/}-dev-${COMMIT::7} (build $BUILD_NUMBER)"
DOCKER_TAG="${BRANCH##release/}-dev"
;;

preview/*)
VERSION="${BRANCH##preview/}-${COMMIT::7} (build $BUILD_NUMBER)"
DOCKER_TAG="${BRANCH##preview/}"
;;
esac
fi

Expand Down
25 changes: 25 additions & 0 deletions test/test-suite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,30 @@ testStack() {
assertNull "${DOCKER_CALLS[5]}"
}

testPreviewBranch() {
branch "preview/my-feature"
dockerSetup > /dev/null

assertEquals "my-feature-0b5a2c5 (build 123)" "$VERSION"
assertEquals "my-feature" "$DOCKER_TAG"
assertNull "$EXTRA_DOCKER_TAG"
assertEquals "login -u user -p pass registry" "${DOCKER_CALLS[0]}"
assertNull "${DOCKER_CALLS[1]}"
}

testBuildAndPushPreviewBranch() {
branch "preview/my-feature"
dockerSetup > /dev/null
dockerBuildAndPush > /dev/null

assertEquals "my-feature-0b5a2c5 (build 123)" "$VERSION"
assertEquals "my-feature" "$DOCKER_TAG"
assertNull "$EXTRA_DOCKER_TAG"
assertEquals "login -u user -p pass registry" "${DOCKER_CALLS[0]}"
assertEquals "build -t repository:my-feature ." "${DOCKER_CALLS[1]}"
assertEquals "push repository:my-feature" "${DOCKER_CALLS[2]}"
assertNull "${DOCKER_CALLS[3]}"
}

SHUNIT_PARENT="test-suite.sh"
. ./shunit2

0 comments on commit bdf32a7

Please sign in to comment.