github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/website/content/docs/job-specification/artifact.mdx (about) 1 --- 2 layout: docs 3 page_title: artifact Stanza - Job Specification 4 sidebar_title: artifact 5 description: |- 6 The "artifact" stanza instructs Nomad to fetch and unpack a remote resource, 7 such as a file, tarball, or binary, and permits downloading artifacts from a 8 variety of locations using a URL as the input source. 9 --- 10 11 # `artifact` Stanza 12 13 <Placement groups={['job', 'group', 'task', 'artifact']} /> 14 15 The `artifact` stanza instructs Nomad to fetch and unpack a remote resource, 16 such as a file, tarball, or binary. Nomad downloads artifacts using the popular 17 [`go-getter`][go-getter] library, which permits downloading artifacts from a 18 variety of locations using a URL as the input source. 19 20 ```hcl 21 job "docs" { 22 group "example" { 23 task "server" { 24 artifact { 25 source = "https://example.com/file.tar.gz" 26 destination = "local/some-directory" 27 options { 28 checksum = "md5:df6a4178aec9fbdc1d6d7e3634d1bc33" 29 } 30 } 31 } 32 } 33 } 34 ``` 35 36 Nomad supports downloading `http`, `https`, `git`, `hg` and `S3` artifacts. If 37 these artifacts are archived (`zip`, `tgz`, `bz2`, `xz`), they are 38 automatically unarchived before the starting the task. 39 40 ## `artifact` Parameters 41 42 - `destination` `(string: "local/")` - Specifies the directory path to 43 download the artifact, relative to the root of the [task's working 44 directory]. If omitted, the default value is to place the artifact in 45 `local/`. The destination is treated as a directory unless `mode` is set to 46 `file`. Source files will be downloaded into that directory path. For more 47 details on how the `destination` interacts with task drivers, see the 48 [Filesystem internals] documentation. 49 50 - `mode` `(string: "any")` - One of `any`, `file`, or `dir`. If set to `file` 51 the `destination` must be a file, not a directory. By default the 52 `destination` will be `local/<filename>`. 53 54 - `options` `(map<string|string>: nil)` - Specifies configuration parameters to 55 fetch the artifact. The key-value pairs map directly to parameters appended to 56 the supplied `source` URL. Please see the [`go-getter` 57 documentation][go-getter] for a complete list of options and examples. 58 59 - `headers` `(map<string|string>: nil)` - Specifies HTTP headers to set when 60 fetching the artifact using `http` or `https` protocol. Please see the 61 [`go-getter` headers documentation][go-getter-headers] for more information. 62 63 - `source` `(string: <required>)` - Specifies the URL of the artifact to download. 64 See [`go-getter`][go-getter] for details. 65 66 ## `artifact` Examples 67 68 The following examples only show the `artifact` stanzas. Remember that the 69 `artifact` stanza is only valid in the placements listed above. 70 71 ### Download File 72 73 This example downloads the artifact from the provided URL and places it in 74 `local/file.txt`. The `local/` path is relative to the [task's working 75 directory]. 76 77 ```hcl 78 artifact { 79 source = "https://example.com/file.txt" 80 } 81 ``` 82 83 To set HTTP headers in the request for the source the optional `headers` field 84 can be configured. 85 86 ```hcl 87 artifact { 88 source = "https://example.com/file.txt" 89 90 headers { 91 User-Agent = "nomad-[${NOMAD_JOB_ID}]-[${NOMAD_GROUP_NAME}]-[${NOMAD_TASK_NAME}]" 92 X-Nomad-Alloc = "${NOMAD_ALLOC_ID}" 93 } 94 } 95 ``` 96 97 To use HTTP basic authentication, preprend the username and password to the 98 hostname in the URL. All special characters, including the username and 99 password, must be URL encoded. For example, for a username `exampleUser` and 100 the password `pass/word!`: 101 102 ```hcl 103 artifact { 104 source = "https://exampleUser:pass%2Fword%21@example.com/file.txt" 105 } 106 ``` 107 108 ### Download using git 109 110 This example downloads the artifact from the provided GitHub URL and places it at 111 `local/repo`, as specified by the optional `destination` parameter. 112 113 ```hcl 114 artifact { 115 source = "git::https://github.com/example/nomad-examples" 116 destination = "local/repo" 117 } 118 ``` 119 120 To download from private repo, sshkey needs to be set. The key must be 121 base64-encoded string. On Linux, you can run `base64 -w0 <file>` to encode the 122 file. Or use [HCL2](https://www.nomadproject.io/docs/job-specification/hcl2) 123 expressions to read and encode the key from a file on your machine: 124 125 ```hcl 126 artifact { 127 source = "git@github.com:example/nomad-examples" 128 destination = "local/repo" 129 options { 130 sshkey = "${base64encode(file(pathexpand("~/.ssh/id_rsa")))}" 131 } 132 } 133 ``` 134 135 To clone specific refs or at a specific depth, use the `ref` and `depth` 136 options: 137 138 ```hcl 139 artifact { 140 source = "git::https://github.com/example/nomad-examples" 141 destination = "local/repo" 142 options { 143 ref = "main" 144 depth = 1 145 } 146 } 147 ``` 148 149 ### Download and Unarchive 150 151 This example downloads and unarchives the result in `local/file`. Because the 152 source URL is an archive extension, Nomad will automatically decompress it: 153 154 ```hcl 155 artifact { 156 source = "https://example.com/file.tar.gz" 157 } 158 ``` 159 160 To disable automatic unarchiving, set the `archive` option to false: 161 162 ```hcl 163 artifact { 164 source = "https://example.com/file.tar.gz" 165 options { 166 archive = false 167 } 168 } 169 ``` 170 171 ### Download and Verify Checksums 172 173 This example downloads an artifact and verifies the resulting artifact's 174 checksum before proceeding. If the checksum is invalid, an error will be 175 returned. 176 177 ```hcl 178 artifact { 179 source = "https://example.com/file.zip" 180 181 options { 182 checksum = "md5:df6a4178aec9fbdc1d6d7e3634d1bc33" 183 } 184 } 185 ``` 186 187 ### Download from an S3-compatible Bucket 188 189 These examples download artifacts from Amazon S3. There are several different 190 types of [S3 bucket addressing][s3-bucket-addr] and [S3 region-specific 191 endpoints][s3-region-endpoints]. As of Nomad 0.6 non-Amazon S3-compatible 192 endpoints like [Minio] are supported, but you must explicitly set the "s3::" 193 prefix. 194 195 This example uses path-based notation on a publicly-accessible bucket: 196 197 ```hcl 198 artifact { 199 source = "https://my-bucket-example.s3-us-west-2.amazonaws.com/my_app.tar.gz" 200 } 201 ``` 202 203 If a bucket requires authentication, you can avoid the use of credentials by 204 using [EC2 IAM instance profiles][iam-instance-profiles]. If this is not possible, 205 credentials may be supplied via the `options` parameter: 206 207 ```hcl 208 artifact { 209 options { 210 aws_access_key_id = "<id>" 211 aws_access_key_secret = "<secret>" 212 aws_access_token = "<token>" 213 } 214 } 215 ``` 216 217 To force the S3-specific syntax, use the `s3::` prefix: 218 219 ```hcl 220 artifact { 221 source = "s3::https://my-bucket-example.s3-eu-west-1.amazonaws.com/my_app.tar.gz" 222 } 223 ``` 224 225 Alternatively you can use virtual hosted style: 226 227 ```hcl 228 artifact { 229 source = "https://my-bucket-example.s3-eu-west-1.amazonaws.com/my_app.tar.gz" 230 } 231 ``` 232 233 [go-getter]: https://github.com/hashicorp/go-getter 'HashiCorp go-getter Library' 234 [go-getter-headers]: https://github.com/hashicorp/go-getter#headers 'HashiCorp go-getter Headers' 235 [minio]: https://www.minio.io/ 236 [s3-bucket-addr]: http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingBucket.html#access-bucket-intro 'Amazon S3 Bucket Addressing' 237 [s3-region-endpoints]: http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region 'Amazon S3 Region Endpoints' 238 [iam-instance-profiles]: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html 'EC2 IAM instance profiles' 239 [task's working directory]: /docs/runtime/environment#task-directories 'Task Directories' 240 [filesystem internals]: /docs/internals/filesystem#templates-artifacts-and-dispatch-payloads