Skip to content

Commit 11a9227

Browse files
committed
Adding README
1 parent 61ef99e commit 11a9227

File tree

2 files changed

+129
-7
lines changed

2 files changed

+129
-7
lines changed

README.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# RockSteady CLI
2+
3+
A command-line interface for building and deploying Docker images to AWS ECR and notifying the RockSteady deployment service. This tool is designed to be used in CI/CD environments.
4+
5+
## Features
6+
7+
* Builds Docker images from a local `Dockerfile`.
8+
* Tags images with git branch, commit SHA, and build number for easy tracking.
9+
* Pushes images to a specified AWS ECR repository.
10+
* Notifies a RockSteady server of a new successful build, triggering deployments.
11+
* Leverages Docker layer caching to speed up builds by reusing layers from previous images on the same branch.
12+
13+
## How it works
14+
15+
The `rocksteady` script has two main subcommands: `build` and `deploy`.
16+
17+
### Build and Push an Image
18+
19+
This command builds the Docker image, tags it, and pushes it to your ECR repository.
20+
21+
```bash
22+
rocksteady build
23+
```
24+
25+
This command will:
26+
27+
1. Authenticate with AWS ECR using the provided credentials.
28+
2. Attempt to pull the latest image for the current branch to use as a build cache (`--cache-from`). This makes builds faster by reusing unchanged layers.
29+
3. Build the Docker image from the `Dockerfile` in the current directory.
30+
4. Tag the image with multiple tags:
31+
* `:build-<build_num>`
32+
* `:<branch>-<build_num>`
33+
* `:<branch>-latest`
34+
* `:<commit_sha>`
35+
* `:latest` (if the branch is `master`)
36+
5. Push all generated tags to the ECR repository.
37+
38+
### Notify RockSteady of a New Build
39+
40+
This command sends a webhook to the RockSteady server to notify it that a new build is available for deployment.
41+
42+
```bash
43+
rocksteady deploy ROCKSTEADY_URL
44+
```
45+
46+
* `ROCKSTEADY_URL`: The URL of the RockSteady server's webhook endpoint. This can also be provided via the `ROCKSTEADY_SERVER` environment variable.
47+
48+
## Configuration
49+
50+
The `rocksteady-cli` is configured entirely through environment variables. The script checks for multiple environment variables for the same configuration. Right now it's being invoked and configured from Altmetric's CircleCI account.
51+
52+
### Required Environment Variables
53+
54+
| Variable | Fallback Variable | Description |
55+
| ----------------------------- | ----------------------------- | ------------------------------------------------------------------------ |
56+
| `ROCKSTEADY_PROJECT` | `CIRCLE_PROJECT_REPONAME` | The name of the project in RockSteady. |
57+
| `ECR_BASE` | | The base URI of your ECR repository (e.g., `123456789012.dkr.ecr.us-east-1.amazonaws.com`). |
58+
| `ECR_AWS_ACCESS_KEY_ID` | `AWS_ACCESS_KEY_ID` | The AWS access key ID for ECR authentication. |
59+
| `ECR_AWS_SECRET_ACCESS_KEY` | `AWS_SECRET_ACCESS_KEY` | The AWS secret access key for ECR authentication. |
60+
| `ECR_AWS_REGION` | | The AWS region where your ECR repository is located. |
61+
| `CIRCLE_BUILD_NUM` | | The build number, used for tagging. |
62+
| `CIRCLE_BRANCH` | | The git branch name, used for tagging. |
63+
| `CIRCLE_SHA1` | | The git commit SHA, used for tagging. |
64+
65+
### Optional Environment Variables
66+
67+
| Variable | Fallback Variable | Description |
68+
| ----------------------------- | ----------------------------- | ------------------------------------------------------------------------ |
69+
| `ECR_REPO` | `ROCKSTEADY_PROJECT` | The name of the ECR repository. Defaults to the project name. |
70+
| `ROCKSTEADY_SERVER` | | The URL for the RockSteady server. Can be overridden by the command line argument in `deploy`. |
71+
| `CF_ACCESSC_ID` | | Cloudflare Access Client ID for authenticating with the RockSteady webhook. |
72+
| `CF_ACCESS_SECRET` | | Cloudflare Access Client Secret for authenticating with the RockSteady webhook. |
73+
| `SIDEKIQ_PRO_TOKEN` | | Build-time variable passed to `docker build`. |
74+
| `DSUI_GITHUB_TOKEN` | | Build-time variable passed to `docker build`. |
75+
76+
## Usage
77+
78+
Add the `rocksteady` script to your CI/CD pipeline. Here’s an example of how to use it in a CircleCI configuration:
79+
80+
```yaml
81+
deploy:
82+
docker:
83+
- image: altmetric/ci:rocksteady-deploy-latest
84+
85+
auth:
86+
username: $DOCKERHUB_USERNAME
87+
password: $DOCKERHUB_PASSWORD
88+
89+
steps:
90+
- setup_remote_docker:
91+
version: default
92+
93+
- checkout
94+
95+
- run:
96+
command: deploy
97+
```

rocksteady

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ get_build_env() {
139139

140140
unslashedbranch=${branch//\//-}
141141
tags=("$ecr_base/$name:build-$build_num" "$ecr_base/$name:$unslashedbranch-$build_num" "$ecr_base/$name:$unslashedbranch-latest" "$ecr_base/$name:$sha1")
142-
if [ "$branch" = "master" ]; then
142+
143+
if [ "$branch" = "main" ] || [ "$branch" = "master" ]; then
143144
tags+=("$ecr_base/$name:latest")
144145
fi
145146
}
@@ -172,14 +173,38 @@ sub_build() {
172173

173174
`AWS_ACCESS_KEY_ID=$aws_access_key_id AWS_SECRET_ACCESS_KEY=$aws_secret_access_key aws ecr get-login --no-include-email --region $aws_region`
174175

175-
cache_from_tag="$ecr_base/$name:$unslashedbranch-latest"
176-
docker pull "$cache_from_tag" || true
176+
# Define cache references
177+
cache_ref_main="$ecr_base/$name:main-cache"
178+
cache_ref_branch="$ecr_base/$name:$unslashedbranch-branch-cache"
179+
180+
docker buildx create --name rocksteady-builder --use
181+
buildx_cmd=(docker buildx build .)
177182

178-
docker build --cache-from "$cache_from_tag" `printf " -t %s" "${tags[@]}"` --build-arg SIDEKIQ_PRO_TOKEN=$SIDEKIQ_PRO_TOKEN --build-arg DSUI_GITHUB_TOKEN=$DSUI_GITHUB_TOKEN .
179-
for tag in "${tags[@]}"
180-
do
181-
docker push $tag
183+
for tag in "${tags[@]}"; do
184+
buildx_cmd+=( -t "$tag" )
182185
done
186+
187+
buildx_cmd+=( \
188+
--build-arg SIDEKIQ_PRO_TOKEN="$SIDEKIQ_PRO_TOKEN" \
189+
--build-arg DSUI_GITHUB_TOKEN="$DSUI_GITHUB_TOKEN" )
190+
191+
# Caching strategy:
192+
# main/master: import main cache only; export main cache only
193+
# other branches: import main (if exists) + branch cache; export branch cache only
194+
if [ "$branch" = "main" ] || [ "$branch" = "master" ]; then
195+
buildx_cmd+=( \
196+
--cache-from type=registry,ref="$cache_ref_main" \
197+
--cache-to type=registry,ref="$cache_ref_main",mode=max )
198+
else
199+
buildx_cmd+=( \
200+
--cache-from type=registry,ref="$cache_ref_main" \
201+
--cache-from type=registry,ref="$cache_ref_branch" \
202+
--cache-to type=registry,ref="$cache_ref_branch",mode=max )
203+
fi
204+
205+
buildx_cmd+=( --push --progress=plain )
206+
207+
"${buildx_cmd[@]}"
183208
}
184209

185210
sub_deploy() {

0 commit comments

Comments
 (0)