Simplecontainer CI/CD #240
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Simplecontainer CI/CD | |
| on: | |
| push: | |
| branches-ignore: | |
| - main | |
| paths-ignore: | |
| - 'README.md' | |
| - 'scripts/**' | |
| - 'LICENSE' | |
| - '.github/resources/**' | |
| - '.gitignore' | |
| - '.github/**' | |
| workflow_dispatch: | |
| inputs: | |
| component: | |
| description: 'Component to release' | |
| required: true | |
| default: 'all' | |
| type: choice | |
| options: | |
| - all | |
| - smr | |
| - smrctl | |
| test_name: | |
| description: 'Optional test name to run' | |
| required: false | |
| type: string | |
| test_flags: | |
| description: 'Optional test flags' | |
| required: false | |
| type: string | |
| jobs: | |
| test: | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| go-version: [ '1.22.x' ] | |
| os: [ 'linux' ] | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| token: ${{ secrets.BOT_USER_PAT }} | |
| show-progress: false | |
| - name: Setup Go ${{ matrix.go-version }} | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version: ${{ matrix.go-version }} | |
| - name: Test code | |
| run: go test -tags=unit ./... -coverprofile=coverage.out | |
| - name: SonarCloud Scan | |
| uses: SonarSource/sonarcloud-github-action@v2 | |
| env: | |
| SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | |
| build: | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| go-version: [ '1.22.x' ] | |
| platform: [ 'amd64', 'arm64' ] | |
| os: [ 'linux' ] | |
| component: ['smr','smrctl'] | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| token: ${{ secrets.BOT_USER_PAT }} | |
| show-progress: false | |
| - name: Setup Go ${{ matrix.go-version }} | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version: ${{ matrix.go-version }} | |
| - name: Display Go version | |
| run: go version | |
| - name: Bump versions | |
| env: | |
| COMMIT_MSG: ${{ github.event.head_commit.message }} | |
| run: | | |
| VERSION_SMR=$(cat cmd/smr/version) | |
| VERSION_SMRCTL=$(cat cmd/smrctl/version) | |
| VERSION_SMR=$(.github/scripts/version.sh "$VERSION_SMR" "$COMMIT_MSG") | |
| VERSION_SMRCTL=$(.github/scripts/version.sh "$VERSION_SMRCTL" "$COMMIT_MSG") | |
| echo "$VERSION_SMR" > cmd/smr/version.tmp | |
| echo "$VERSION_SMRCTL" > cmd/smrctl/version.tmp | |
| mv cmd/smr/version.tmp cmd/smr/version | |
| mv cmd/smrctl/version.tmp cmd/smrctl/version | |
| - name: Build ${{ matrix.component }} for ${{ matrix.os }} on ${{ matrix.platform }} | |
| if: matrix.component == 'smr' || matrix.component == 'smrctl' || github.event.inputs.component == 'all' || github.event_name != 'workflow_dispatch' | |
| working-directory: "cmd/${{matrix.component}}" | |
| run: | | |
| VERSION=$(cat version) | |
| CGO_ENABLED=0 GOOS=${{ matrix.os }} GOARCH=${{ matrix.platform }} go build -ldflags "-s -w -X main.version=$VERSION" -o ${{matrix.component}}-${{ matrix.os }}-${{ matrix.platform }} | |
| chmod +x ${{ matrix.component }}-${{ matrix.os }}-${{ matrix.platform }} | |
| - name: Upload smr artifacts | |
| if: matrix.component == 'smr' || matrix.component == 'smrctl' || github.event.inputs.component == 'all' || github.event_name != 'workflow_dispatch' | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: ${{ matrix.component }}-${{ matrix.os}}-${{ matrix.platform }} | |
| path: cmd/${{ matrix.component }}/${{ matrix.component }}-${{ matrix.os }}-${{ matrix.platform }} | |
| set-matrix: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| matrix: ${{ steps.generate.outputs.matrix }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| token: ${{ secrets.BOT_USER_PAT }} | |
| show-progress: false | |
| - name: Generate dynamic matrix | |
| id: generate | |
| run: | | |
| GO_VERSIONS='["1.22.x"]' | |
| PLATFORMS='["amd64"]' | |
| OS_LIST='["linux"]' | |
| TESTS_DIRS=$(find pkg/tests/e2e -type f -name '*_test.go' -exec dirname {} \; | sort -u) | |
| matrix='[]' | |
| for dir in $TESTS_DIRS; do | |
| name=$(basename "$dir") | |
| for go in $(echo $GO_VERSIONS | jq -r '.[]'); do | |
| for os in $(echo $OS_LIST | jq -r '.[]'); do | |
| for arch in $(echo $PLATFORMS | jq -r '.[]'); do | |
| matrix=$(echo "$matrix" | jq \ | |
| --arg dir "$dir" \ | |
| --arg name "$name" \ | |
| --arg tags "e2e" \ | |
| --arg go "$go" \ | |
| --arg os "$os" \ | |
| --arg arch "$arch" \ | |
| '. += [{ | |
| directory: $dir, | |
| name: $name, | |
| tags: $tags, | |
| "go-version": $go, | |
| os: $os, | |
| platform: $arch | |
| }]') | |
| done | |
| done | |
| done | |
| done | |
| echo "$matrix" | jq | |
| echo "matrix=$(echo "$matrix" | jq -c)" >> "$GITHUB_OUTPUT" | |
| test-integration: | |
| name: Run ${{ matrix.name }} test | |
| runs-on: ubuntu-latest | |
| needs: [ set-matrix, test, build ] | |
| strategy: | |
| matrix: | |
| include: ${{ fromJSON(needs.set-matrix.outputs.matrix) }} | |
| outputs: | |
| image_name: ${{ steps.image_info.outputs.image_name }} | |
| image_tag: ${{ steps.image_info.outputs.image_tag }} | |
| steps: | |
| - name: Uninstall pre-installed Docker | |
| run: | | |
| for pkg in docker docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove "$pkg"*; done | |
| - name: Install Docker 28.0.3 | |
| run: | | |
| curl -fsSL https://get.docker.com -o get-docker.sh | |
| VERSION=28.0.3 sh get-docker.sh | |
| sudo chown $USER /var/run/docker.sock | |
| - name: Verify Docker installation | |
| run: | | |
| docker version | |
| docker version --format '{{.Server.APIVersion}}' | |
| - uses: actions/checkout@v4 | |
| with: | |
| token: ${{ secrets.BOT_USER_PAT }} | |
| show-progress: false | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| with: | |
| driver: docker | |
| - name: Generate image info | |
| id: image_info | |
| run: | | |
| IMAGE_NAME="simplecontainer/smr" | |
| IMAGE_TAG="build-${{ github.run_id }}" | |
| echo "image_name=$IMAGE_NAME" >> $GITHUB_OUTPUT | |
| echo "image_tag=$IMAGE_TAG" >> $GITHUB_OUTPUT | |
| - name: Download smr-${{ matrix.os }}-${{ matrix.platform }} | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: smr-${{ matrix.os }}-${{ matrix.platform }} | |
| path: ./smr-${{ matrix.os }}-${{ matrix.platform }} | |
| - name: Download smrctl-${{ matrix.os }}-${{ matrix.platform }} | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: smrctl-${{ matrix.os }}-${{ matrix.platform }} | |
| path: ./smrctl-${{ matrix.os }}-${{ matrix.platform }} | |
| - name: Prepare binary | |
| run: | | |
| mv ./smr-${{ matrix.os }}-${{ matrix.platform }}/smr-${{ matrix.os }}-${{ matrix.platform }} ./smr-${{ matrix.os }}-${{ matrix.platform }}/smr | |
| mv ./smrctl-${{ matrix.os }}-${{ matrix.platform }}/smrctl-${{ matrix.os }}-${{ matrix.platform }} ./smrctl-${{ matrix.os }}-${{ matrix.platform }}/smrctl | |
| chmod +x ./smr-${{ matrix.os }}-${{ matrix.platform }}/smr | |
| chmod +x ./smrctl-${{ matrix.os }}-${{ matrix.platform }}/smrctl | |
| - name: Build Docker image for testing | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| file: docker/Dockerfile | |
| tags: ${{ steps.image_info.outputs.image_name }}:${{ steps.image_info.outputs.image_tag }} | |
| load: true | |
| build-args: | | |
| TARGETOS=${{ matrix.os }} | |
| TARGETARCH=${{ matrix.platform }} | |
| - name: Clone examples repository for tests | |
| uses: actions/checkout@v4 | |
| with: | |
| repository: simplecontainer/examples | |
| path: examples | |
| - name: Setup Go ${{ matrix.go-version }} | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version: ${{ matrix.go-version }} | |
| - name: Test code | |
| run: | | |
| go test -v -tags=${{ matrix.tags }} ./${{ matrix.directory }}/... \ | |
| -image $IMAGE -tag $TAG \ | |
| -binary smr-${{ matrix.os }}-${{ matrix.platform }}/smr \ | |
| -examples examples \ | |
| -binaryctl smrctl-${{ matrix.os }}-${{ matrix.platform }}/smrctl | |
| env: | |
| IMAGE: ${{ steps.image_info.outputs.image_name }} | |
| TAG: ${{ steps.image_info.outputs.image_tag }} | |
| prepare-release: | |
| if: startsWith(github.ref, 'refs/tags/') || github.event_name == 'workflow_dispatch' | |
| runs-on: ubuntu-latest | |
| needs: [ test-integration ] | |
| outputs: | |
| tag_smr: ${{ steps.generate_tag.outputs.tag_smr }} | |
| tag_ctl: ${{ steps.generate_tag.outputs.tag_ctl }} | |
| matrix: ${{ steps.generate_tag.outputs.matrix }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| token: ${{ secrets.BOT_USER_PAT }} | |
| show-progress: false | |
| - name: Generate tag name for workflow dispatch | |
| id: generate_tag | |
| env: | |
| COMMIT_MSG: ${{ github.event.head_commit.message }} | |
| run: | | |
| COMPONENT="${{ github.event.inputs.component || 'all' }}" | |
| VERSION="" | |
| if [[ "$COMPONENT" == "all" ]]; then | |
| VERSION_SMR=$(cat cmd/smr/version) | |
| VERSION_CTL=$(cat cmd/smrctl/version) | |
| elif [[ "$COMPONENT" == "smrctl" ]]; then | |
| VERSION_CTL=$(cat cmd/smrctl/version) | |
| elif [[ "$COMPONENT" == "smr" ]]; then | |
| VERSION_SMR=$(cat cmd/smr/version) | |
| fi | |
| VERSION_SMR=$(.github/scripts/version.sh "$VERSION_SMR" "$COMMIT_MSG") | |
| VERSION_CTL=$(.github/scripts/version.sh "$VERSION_CTL" "$COMMIT_MSG") | |
| VERSION_PKG=$(.github/scripts/version.sh "$(cat version)" "$COMMIT_MSG") | |
| TAG_CTL="" | |
| TAG_SMR="" | |
| if [[ "$COMPONENT" == "all" && "${{ github.event_name }}" == "workflow_dispatch" ]]; then | |
| TAG_CTL="${VERSION_CTL}-smrctl" | |
| TAG_SMR="${VERSION_SMR}-smr" | |
| elif [[ "$COMPONENT" == "smrctl" ]]; then | |
| TAG_CTL="${VERSION_CTL}-smrctl" | |
| elif [[ "$COMPONENT" == "smr" ]]; then | |
| TAG_SMR="${VERSION_SMR}-smrctl" | |
| fi | |
| TAG_PKG=$VERSION_PKG | |
| echo "Generated tags: $TAG_CTL $TAG_SMR" | |
| echo "tag_smr=$TAG_SMR" >> $GITHUB_OUTPUT | |
| echo "tag_ctl=$TAG_CTL" >> $GITHUB_OUTPUT | |
| if [[ -n "$TAG_SMR" && -n "$TAG_CTL" ]]; then | |
| MATRIX=$(printf '[{"tag": "%s", "component":"smr", "versionFile": "cmd/smr/version"}, {"tag": "%s", "component":"smrctl", "versionFile": "cmd/smrctl/version"}, {"tag": "%s", "component":"", "versionFile": "version"}]' "$TAG_SMR" "$TAG_CTL" "$TAG_PKG") | |
| elif [[ -n "$TAG_SMR" ]]; then | |
| MATRIX=$(printf '[{"tag": "%s", "component":"smr", "versionFile": "cmd/smr/version"}, {"tag": "%s", "component":"", "versionFile": "version"}]' "$TAG_SMR" "$TAG_PKG") | |
| elif [[ -n "$TAG_CTL" ]]; then | |
| MATRIX=$(printf '[{"tag": "%s", "component":"smrctl", "versionFile": "cmd/smrctl/version"}, {"tag": "%s", "component":"", "versionFile": "version"}]' "$TAG_CTL" "$TAG_PKG") | |
| fi | |
| echo "$MATRIX" | jq -c | |
| echo "matrix=$(echo "$MATRIX" | jq -c)" >> $GITHUB_OUTPUT | |
| release: | |
| if: github.event_name == 'workflow_dispatch' | |
| runs-on: ubuntu-latest | |
| needs: [ prepare-release ] | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| token: ${{ secrets.BOT_USER_PAT }} | |
| show-progress: false | |
| - name: Parse matrix and run sequentially | |
| env: | |
| COMMIT_MSG: ${{ github.event.head_commit.message }} | |
| run: | | |
| matrix=$(echo '${{ needs.prepare-release.outputs.matrix }}' | jq -c '.[]') | |
| for item in $matrix; do | |
| COMPONENT=$(echo $item | jq -r '.component') | |
| TAG=$(echo $item | jq -r '.tag') | |
| FILE=$(echo $item | jq -r '.versionFile') | |
| echo "Processing $COMPONENT with tag $TAG" | |
| VERSION=$(cat $FILE) | |
| VERSION=$(.github/scripts/version.sh "$VERSION" "$COMMIT_MSG") | |
| echo "$VERSION" > $FILE | |
| git config user.name "actions" | |
| git config user.email "actions-user@github.com" | |
| git add $FILE | |
| git commit -m "Automated version tracking for $TAG" || echo "No changes to commit" | |
| git pull | |
| git tag "$TAG" | |
| git push origin main --tags | |
| done |