canonical / rockcraft

Tool to create OCI Images using the language from Snapcraft and Charmcraft.
GNU General Public License v3.0
35 stars 43 forks source link

global environment not being registered for CRAFT_PROJECT_VERSION #333

Closed cjdcordeiro closed 1 year ago

cjdcordeiro commented 1 year ago

Bug Description

The CRAFT_PROJECT_VERSION depends on the rockcraft.yaml's version field and thus should be registered after the YAML is loaded.

This should happen in

https://github.com/canonical/rockcraft/blob/bdf64517487be2fac8fa28ddfe3919e08f9b3bde/rockcraft/lifecycle.py#L70

however, the callback does not seem to be working and this environment variable never gets loaded.

To Reproduce

Run rockcraft --verbosity debug on https://github.com/jnsgruk/zinc-k8s-operator/blob/main/rockcraft.yaml (originally reported by @jnsgruk in https://chat.charmhub.io/charmhub/pl/h9qytisgti8s3pu6yhe1pq8hpy).

Environment

Version: 0+git.bdf6451

rockcraft.yaml

name: zinc
version: 0.4.7
license: Apache-2.0

base: bare
build_base: ubuntu:22.04
platforms:
  amd64:

summary: Zinc is a lightweight search engine and alternative to Elasticsearch.
description: |
  Zinc is a search engine that does full text indexing. It is a lightweight alternative to
  elasticsearch and runs in less than 100 MB of RAM. It uses bluge as the underlying indexing
  library.

  It is very simple and easy to operate as opposed to elasticsearch which requires a couple dozen
  knobs to understand and tune.

  It is a drop-in replacement for elasticsearch if you are just ingesting data using APIs and
  searching using kibana (Kibana is not supported with zinc. Zinc provides its own UI).

run_user: _daemon_

environment:
  ZINC_DATA_PATH: /var/lib/zincsearch
  ZINC_PROMETHEUS_ENABLE: true
  ZINC_TELEMETRY: false
  ZINC_PROFILER: true

services:
  zinc:
    override: replace
    startup: enabled
    command: /bin/go-runner --log-file=/var/lib/zincsearch/zinc.log --also-stdout=true --redirect-stderr=true /bin/zincsearch

parts:
  zinc:
    plugin: go
    source: https://github.com/zincsearch/zincsearch
    source-type: git
    source-tag: v$CRAFT_PROJECT_VERSION
    build-snaps:
      - go/latest/stable
      - node/18/stable
    build-environment:
      - CGO_ENABLED: 0
      - GOOS: linux
    override-build: |
      COMMIT_HASH="$(git rev-parse HEAD)"
      BUILD_DATE="$(date -u '+%Y-%m-%d_%I:%M:%S%p-GMT')"

      # Build the web ui, which is embedded in the go binary later
      pushd web
      npm install
      npm run build
      popd

      go mod tidy
      go build \
        -ldflags="-s -w
        -X github.com/zincsearch/zincsearch/pkg/meta.Version=${CRAFT_PROJECT_VERSION} \
        -X github.com/zincsearch/zincsearch/pkg/meta.CommitHash=${COMMIT_HASH} \
        -X github.com/zincsearch/zincsearch/pkg/meta.BuildDate=${BUILD_DATE}" \
        -o zincsearch \
        cmd/zincsearch/main.go
    stage-packages:
      - libc6_libs
      - ca-certificates_data
    override-stage: |
      # Create some directories
      mkdir -p "${CRAFT_PART_INSTALL}/bin"

      # Install the zincsearch binary
      install -m 0755 "${CRAFT_PART_BUILD}/zincsearch" "${CRAFT_PART_INSTALL}/bin/zincsearch"

      # Run the default stage hook
      craftctl default

  # This part can be removed once Pebble supports log forwarding
  kube-log-runner:
    plugin: go
    source: https://github.com/kubernetes/release
    source-type: git
    source-tag: v0.15.1
    source-subdir: images/build/go-runner
    build-snaps:
      - go/latest/stable
    build-environment:
      - CGO_ENABLED: 0
      - GOOS: linux

Relevant log output

Failed to pull source: command ['git', 'clone', '--recursive', '--branch', 'v$CRAFT_PROJECT_VERSION', 'https://github.com/zincsearch/zincsearch', '/root/parts/zinc/src'] exited with code 128.
Recommended resolution: Make sure sources are correctly specified.
SamirPS commented 1 year ago

to reproduce it you need to change

parts:
  zinc:
    plugin: go
    source: https://github.com/zincsearch/zincsearch
    source-type: git
    source-tag: v$0.4.7

to

parts:
  zinc:
    plugin: go
    source: https://github.com/zincsearch/zincsearch
    source-type: git
    source-tag: v$CRAFT_PROJECT_VERSION