Use CircleCI version 2.1 at the top of your .circleci/config.yml file.
1
version: 2.1
Add the orbs
stanza below your version, invoking the orb:
1
2
orbs:
jq: circleci/jq@3.0.2
Use jq
elements in your existing workflows and jobs.
Install jq in a CircleCI job
1
2
3
4
5
6
7
8
9
10
11
12
13
14
version: '2.1'
orbs:
jq: circleci/jq@3.0.0
jobs:
build:
docker:
- image: cimg/base:current
steps:
- jq/install:
version: jq-1.7
workflows:
your-workflow:
jobs:
- build
Install jq in a CircleCI job. Supports jq versions 1.3 and newer. Requirements: bash, curl, gpg, grep, sed, sha256sum, tar
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
install_dir | Directory in which to install jq
| No | ~/bin | string |
override | Whether or not to remove a pre-existing version of jq when this version
is installed.
| No | false | boolean |
version | Version of jq to install, defaults to `latest`. If specifying a version other than latest, provide a full release tag, as listed at https://api.github.com/repos/jqlang/jq/releases, e.g., `jq-1.6`.
| No | latest | string |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# This code is licensed from CircleCI to the user under the MIT license.
# See here for details: https://circleci.com/developer/orbs/licensing
version: 2.1
description: |
An orb for working with jq, a command-line JSON parser useful for headless/automated API scripting.
display:
source_url: https://github.com/CircleCI-Public/jq-orb
commands:
install:
description: |
Install jq in a CircleCI job. Supports jq versions 1.3 and newer. Requirements: bash, curl, gpg, grep, sed, sha256sum, tar
parameters:
install_dir:
default: ~/bin
description: |
Directory in which to install jq
type: string
override:
default: false
description: |
Whether or not to remove a pre-existing version of jq when this version
is installed.
type: boolean
version:
default: latest
description: |
Version of jq to install, defaults to `latest`. If specifying a version other than latest, provide a full release tag, as listed at https://api.github.com/repos/jqlang/jq/releases, e.g., `jq-1.6`.
type: string
steps:
- run:
command: |
#!/bin/sh
#shellcheck disable=SC1090,SC3028
# Quietly try to make the install directory.
JQ_STR_VERSION="$(echo "${JQ_STR_VERSION}" | circleci env subst)"
JQ_EVAL_INSTALL_DIR="$(eval echo "${JQ_EVAL_INSTALL_DIR}")"
mkdir -p "${JQ_EVAL_INSTALL_DIR}"
# Selectively export the SUDO command, depending if we have permission
# for a directory and whether we're running alpine.
if grep "Alpine" /etc/issue > /dev/null 2>&1; then # Check if we're root
if [ "$ID" = 0 ]; then export SUDO="sudo"; else export SUDO=""; fi
else
if [ "$EUID" = 0 ]; then export SUDO=""; else export SUDO="sudo"; fi
fi
# If our first mkdir didn't succeed, we needed to run as sudo.
if [ ! -w "${JQ_EVAL_INSTALL_DIR}" ]; then
$SUDO mkdir -p "${JQ_EVAL_INSTALL_DIR}"
fi
echo "export PATH=\$PATH:\"${JQ_EVAL_INSTALL_DIR}\"" >> "$BASH_ENV"
. "$BASH_ENV"
# check if jq needs to be installed
if command -v jq >> /dev/null 2>&1; then
echo "jq is already installed..."
if [ "${JQ_BOOL_OVERRIDE}" -eq 1 ]; then
echo "removing it."
$SUDO rm -f "$(command -v jq)"
else
echo "ignoring install request."
exit 0
fi
fi
# Set jq version
if [ "${JQ_STR_VERSION}" = "latest" ]; then
JQ_VERSION=$(wget -q --server-response -O /dev/null "https://github.com/jqlang/jq/releases/latest" 2>&1 | awk '/^ Location: /{print $2}' | sed 's:.*/::')
echo "Latest version of jq is $JQ_VERSION"
else
JQ_VERSION="${JQ_STR_VERSION}"
fi
# extract version number
JQ_VERSION_NUMBER_STRING=$(echo "${JQ_VERSION}" | sed -E 's/-/ /')
JQ_VERSION_NUMBER="$(echo "$JQ_VERSION_NUMBER_STRING" | awk '{print $2}')"
# Set binary download URL for specified version
# handle mac version
if uname -a | grep Darwin > /dev/null 2>&1; then
JQ_BINARY_URL="https://github.com/jqlang/jq/releases/download/${JQ_VERSION}/jq-macos-arm64"
else
# linux version
if uname -m | grep x86_64 > /dev/null 2>&1; then
JQ_BINARY_URL="https://github.com/jqlang/jq/releases/download/${JQ_VERSION}/jq-linux64"
else
JQ_BINARY_URL="https://github.com/jqlang/jq/releases/download/${JQ_VERSION}/jq-linux-arm64"
fi
fi
jqBinary="jq-$PLATFORM"
if [ -d "$JQ_VERSION/sig" ]; then
# import jq sigs
if uname -a | grep Darwin > /dev/null 2>&1; then
HOMEBREW_NO_AUTO_UPDATE=1 brew install gnupg coreutils
PLATFORM=macos-arm64
else
if grep "Alpine" /etc/issue > /dev/null 2>&1; then
$SUDO apk add gnupg > /dev/null 2>&1
elif uname -m | grep x86_64 > /dev/null 2>&1; then
PLATFORM=linux64
else
PLATFORM=linux-arm64
fi
fi
gpg --import "$JQ_VERSION/sig/jq-release.key" > /dev/null
wget -q -O "$JQ_VERSION/sig/v$JQ_VERSION_NUMBER/jq-$PLATFORM" \
--tries=3 --retry-connrefused "$JQ_BINARY_URL"
# verify sha256sum, sig, install
gpg --verify "$JQ_VERSION/sig/v$JQ_VERSION_NUMBER/jq-$PLATFORM.asc"
cd "$JQ_VERSION/sig/v$JQ_VERSION_NUMBER" || exit
grep "jq-$PLATFORM" "sha256sum.txt" > tmp_checksum.txt
if grep "jq-$PLATFORM" "sha256sum.txt" -eq 0; then
sha256sum -c tmp_checksum.txt
status=$?
rm tmp_checksum.txt
if [ $status -eq 0 ]; then
jqBinary="$JQ_VERSION/sig/v$JQ_VERSION_NUMBER/jq-$PLATFORM"
else
echo "Checksum verification failed. Please check checksum"
exit 1
fi
else
exit 1
fi
cd - >/dev/null || exit
else
wget -O "$jqBinary" -q --tries=3 "$JQ_BINARY_URL"
fi
$SUDO mv "$jqBinary" "${JQ_EVAL_INSTALL_DIR}"/jq
$SUDO chmod +x "${JQ_EVAL_INSTALL_DIR}"/jq
# cleanup
[ -d "./$JQ_VERSION" ] && rm -rf "./$JQ_VERSION"
# verify version
echo "jq has been installed to $(which jq)"
echo "jq version:"
jq --version
environment:
JQ_BOOL_OVERRIDE: << parameters.override >>
JQ_EVAL_INSTALL_DIR: << parameters.install_dir >>
JQ_STR_VERSION: << parameters.version >>
name: Install jq
examples:
install:
description: |
Install jq in a CircleCI job
usage:
version: "2.1"
orbs:
jq: circleci/jq@3.0.0
jobs:
build:
docker:
- image: cimg/base:current
steps:
- jq/install:
version: jq-1.7
workflows:
your-workflow:
jobs:
- build