From 63bac705ffa5ddd5523a734a323149ffe56b9f6f Mon Sep 17 00:00:00 2001 From: Afonso Costa Date: Mon, 23 Oct 2023 00:51:56 +0100 Subject: [PATCH] 351/automated docker images (#354) --- .github/workflows/docker.yml | 61 ++++++++++++++++++ .github/workflows/go.yml | 9 +++ .github/workflows/goreleaser.yml | 11 +++- README.md | 102 +++++++++++++++++++++---------- 4 files changed, 148 insertions(+), 35 deletions(-) create mode 100644 .github/workflows/docker.yml diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..79ee25c --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,61 @@ +name: docker + +on: + workflow_dispatch: + inputs: + is_release: + required: false + type: boolean + default: false + workflow_call: + inputs: + is_release: + required: false + type: boolean + default: false + +jobs: + docker: + name: Build and deploy to ghcr.io + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Compute Docker tags + run: | + echo "github.ref_name=${{ github.ref_name }}" + echo "github.sha=${{ github.sha }}" + ref_name=$(echo "${{ github.ref_name }}" | tr -c 'a-zA-Z0-9.' '-' | sed 's/^-//;s/-$//') + commit_sha=$(git rev-parse --short HEAD) + echo "ref_name=$ref_name" + echo "commit_sha=$commit_sha" + + if [ "${{ inputs.is_release }}" = "true" ]; then + echo "Setting Docker tag for a release" + echo "docker_latest_tag=latest" >> $GITHUB_ENV + echo "docker_tag=$ref_name" >> $GITHUB_ENV + else + echo "Setting Docker tag for a regular push" + echo "docker_latest_tag=$ref_name" >> $GITHUB_ENV + echo "docker_tag=$ref_name-$commit_sha" >> $GITHUB_ENV + fi + echo "Tag docker_latest_tag=${{ env.docker_latest_tag }}" + echo "Tag docker_tag=${{ env.docker_tag }}" + - name: Build and push + uses: docker/build-push-action@v5 + with: + platforms: linux/arm64,linux/amd64 + push: true + tags: | + ghcr.io/${{ github.repository }}:${{ env.docker_latest_tag }} + ghcr.io/${{ github.repository }}:${{ env.docker_tag }} diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 3ab41e2..5456c62 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -34,6 +34,7 @@ jobs: run: scripts/gitlab_cloud_integration_tests.sh env: GITLAB_TOKEN: ${{ secrets.GHORG_GITLAB_TOKEN }} + build_and_test_windows: environment: actions name: Build and Test Windows @@ -67,3 +68,11 @@ jobs: run: scripts/gitlab_cloud_integration_tests.sh env: GITLAB_TOKEN: ${{ secrets.GHORG_GITLAB_TOKEN }} + + docker: + needs: [build_and_test, build_and_test_windows] + uses: ./.github/workflows/docker.yml + permissions: + packages: write + with: + is_release: false diff --git a/.github/workflows/goreleaser.yml b/.github/workflows/goreleaser.yml index db3f77e..dee5a53 100644 --- a/.github/workflows/goreleaser.yml +++ b/.github/workflows/goreleaser.yml @@ -6,13 +6,12 @@ on: - 'v*' workflow_dispatch: -permissions: - contents: write - jobs: goreleaser: environment: actions runs-on: ubuntu-latest + permissions: + contents: write steps: - name: Checkout @@ -32,3 +31,9 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GHORG_GITHUB_TOKEN }} + docker: + uses: ./.github/workflows/docker.yml + permissions: + packages: write + with: + is_release: true diff --git a/README.md b/README.md index 8df39aa..6710c41 100644 --- a/README.md +++ b/README.md @@ -59,20 +59,14 @@ Alternatively, Windows users can also install ghorg using [scoop](https://scoop. ## Installation -### Prebuilt Binaries - -See [latest release](https://github.com/gabrie30/ghorg/releases/latest) to download directly for - -- Mac (Darwin) -- Windows -- Linux - -If you don't know which to choose its likely going to be the x86_64 version for your operating system +There are 4 installation methods available, please choose the one that suits your fancy: +- [Homebrew](#homebrew) +- [Golang](#golang) +- [Docker](#docker) +- [Prebuilt Binaries](#prebuilt-binaries) ### Homebrew -> optional but recommended - ```bash mkdir -p $HOME/.config/ghorg curl https://raw.githubusercontent.com/gabrie30/ghorg/master/sample-conf.yaml > $HOME/.config/ghorg/conf.yaml @@ -86,8 +80,6 @@ brew install gabrie30/utils/ghorg ### Golang -> optional but recommended - ```bash mkdir -p $HOME/.config/ghorg curl https://raw.githubusercontent.com/gabrie30/ghorg/master/sample-conf.yaml > $HOME/.config/ghorg/conf.yaml @@ -106,6 +98,71 @@ go install github.com/gabrie30/ghorg@latest go get github.com/gabrie30/ghorg ``` +### Docker + +The provided images are built for both `amd64` and `arm64` architectures and are available solely on Github Container Registry [ghcr.io](https://github.com/gabrie30/ghorg/pkgs/container/ghorg). + +```shell +# Should print help message +# You can also specify a version as the tag, such as ghcr.io/gabrie30/ghorg:v1.9.9 +docker run --rm ghcr.io/gabrie30/ghorg:latest +``` + +> Note: There are also tags available for the latest on trunk, such as `master` or `master-`, but these **are not recommended**. + +The commands for ghorg are parsed as docker commands. The entrypoint is the `ghorg` binary, hence you only need to enter remaining arguments as follows: + +```shell +docker run --rm ghcr.io/gabrie30/ghorg \ + clone kubernetes --token=bGVhdmUgYSBjb21tZW50IG9uIGlzc3VlIDY2 +``` + +The image ships with the following environment variables set: + +```shell +GHORG_CONFIG=/config/conf.yaml +GHORG_RECLONE_PATH=/config/reclone.yaml +GHORG_ABSOLUTE_PATH_TO_CLONE_TO=/data +``` + +These can be overriden, if necessary, by including the `-e` flag to the docker run comand, e.g. `-e GHORG_GITHUB_TOKEN=bGVhdmUgYSBjb21tZW50IG9uIGlzc3VlIDY2`. + +#### Persisting Data on the Host + +In order to store data on the host, it is required to bind mount a volume: +- `$HOME/.config/ghorg:/config`: Mounts your config directory inside the container, to access `config.yaml` and `reclone.yaml`. +- `$HOME/repositories:/data`: Mounts your local data directory inside the container, where repos will be downloaded by default. + +```shell +docker run --rm \ + -e GHORG_GITHUB_TOKEN=bGVhdmUgYSBjb21tZW50IG9uIGlzc3VlIDY2 \ + -v $HOME/.config/ghorg:/config `# optional` \ + -v $HOME/repositories:/data \ + ghcr.io/gabrie30/ghorg:latest \ + clone kubernetes --match-regex=^sig +``` + +> Note: Altering `GHORG_ABSOLUTE_PATH_TO_CLONE_TO` will require changing the mount location from `/data` to the new location inside the container. + +A shell alias might make this more practical: + +```shell +alias ghorg="docker run --rm -v $HOME/.config/ghorg:/config -v $HOME/repositories:/data ghcr.io/gabrie30/ghorg:latest" + +# Using the alias: creates and cleans up the container +ghorg clone kubernetes --match-regex=^sig +``` + +### Prebuilt Binaries + +See [latest release](https://github.com/gabrie30/ghorg/releases/latest) to download directly for + +- Mac (Darwin) +- Windows +- Linux + +If you don't know which to choose its likely going to be the x86_64 version for your operating system. + ## SCM Provider Setup > Note: if you are running into issues, read the troubleshooting and known issues section below @@ -173,25 +230,6 @@ $ ghorg examples gitlab $ ghorg exmaples github ``` -### With Docker - -> This is only recommended for testing due to resource constraints - -1. Clone repo then `cd ghorg` -1. Build the image `docker build . -t ghorg-docker` -1. Run in docker - -```bash -# using your local ghorg configuration file, cloning in container -docker run -v $HOME/.config/ghorg:/config ghorg-docker clone kubernetes - -# using flags, cloning in container -docker run ghorg-docker clone kubernetes --token=bGVhdmUgYSBjb21tZW50IG9uIGlzc3VlIDY2 - -# using flags, cloning to your machine -docker run -v $HOME/ghorg/:/data ghorg-docker clone kubernetes --token=bGVhdmUgYSBjb21tZW50IG9uIGlzc3VlIDY2 --output-dir=cloned-from-docker -``` - ## Changing Clone Directories 1. By default ghorg will clone the org or user repos into a directory like `$HOME/ghorg/org`. If you want to clone the org to a different directory use the `--path` flag or set `GHORG_ABSOLUTE_PATH_TO_CLONE_TO` in your ghorg conf. **This value must be an absolute path**. For example if you wanted to clone the kubernetes org to `/tmp/ghorg` you would run the following command.